77 "io/ioutil"
88 "os"
99 "path/filepath"
10- "regexp"
11- "strconv"
10+ "strings"
1211 "sync"
1312 "time"
1413
@@ -66,29 +65,74 @@ func (c *Context) Args() *fasthttp.Args {
6665
6766// CONTEXT STORE -->
6867
69- func (c * Context ) Set (key , value string ) {
68+ func (c * Context ) CSet (key , value string ) {
7069 c .lock .Lock ()
7170 defer c .lock .Unlock ()
7271
7372 if c .store == nil {
7473 c .store = make (Map )
7574 }
7675
77- c .store [key ] = value
76+ if pool , ok := c .gosvelt .storePool .Get ().(Map ); ok {
77+ pool [key ] = value
78+ c .store = pool
79+
80+ } else {
81+ c .store [key ] = value
82+ }
7883}
7984
80- func (c * Context ) Get (key string ) string {
85+ func (c * Context ) CGet (key string ) string {
8186 c .lock .RLock ()
8287 defer c .lock .RUnlock ()
8388
84- return c .store [key ].(string )
89+ if c .store == nil {
90+ return ""
91+ }
92+
93+ if pool , ok := c .gosvelt .storePool .Get ().(Map ); ok {
94+ value , ok := pool [key ]
95+ if ok {
96+ return value .(string )
97+ }
98+
99+ value = c .store [key ]
100+ pool [key ] = value
101+ c .store = pool
102+
103+ return value .(string )
104+ }
105+
106+ if value , ok := c .store [key ]; ok {
107+ return value .(string )
108+ }
109+
110+ return ""
111+ }
112+
113+ func (c * Context ) CDel (key string ) {
114+ c .lock .Lock ()
115+ defer c .lock .Unlock ()
116+
117+ if c .store == nil {
118+ return
119+ }
120+
121+ if pool , ok := c .gosvelt .storePool .Get ().(Map ); ok {
122+ delete (pool , key )
123+ c .store = pool
124+
125+ } else {
126+ delete (c .store , key )
127+ }
85128}
86129
87- func (c * Context ) CacheReset () {
130+ func (c * Context ) CReset () {
88131 c .lock .Lock ()
89132 defer c .lock .Unlock ()
90133
91134 c .store = make (Map )
135+ c .gosvelt .storePool .Put (c .store )
92136}
93137
94138// CONTEXT RESPONSES -->
@@ -115,54 +159,45 @@ func (c *Context) Html(code int, t string, args ...any) error {
115159
116160 switch args [0 ].(type ) {
117161 case string :
118- // find pattern &{x} and replace it with an given arg
119- // where x is an string
120- re := regexp .MustCompile (`&{(\d+)}` )
121- output = re .ReplaceAllStringFunc (t , func (match string ) string {
122- digitStr := re .FindStringSubmatch (match )[1 ]
123- digit , _ := strconv .Atoi (digitStr )
124-
125- if digit >= 1 && digit <= len (args ) {
126- // convert []any into []string
127- strArgs := make ([]string , len (args ))
128- for i , v := range args {
129- strArgs [i ] = v .(string )
130- }
131- return strArgs [digit - 1 ]
132- }
133-
134- return match
135- })
162+ // create a map of placeholders to values
163+ placeholders := make (map [string ]string )
164+ for i , arg := range args [1 :] {
165+ placeholders [fmt .Sprintf ("%d" , i + 1 )] = fmt .Sprint (arg )
166+ }
167+
168+ // replace placeholders in the template with values
169+ for placeholder , value := range placeholders {
170+ t = strings .ReplaceAll (t , fmt .Sprintf ("&{%s}" , placeholder ), value )
171+ }
172+
173+ output = t
136174
137175 case Map :
138- // find pattern &{x} and replace it with an given arg
139- // where x is an string without ""
140- re := regexp . MustCompile ( `&{(\w+)}` )
141- output = re . ReplaceAllStringFunc ( t , func ( match string ) string {
142- key := re . FindStringSubmatch ( match )[ 1 ]
176+ // create a map of placeholders to values
177+ placeholders := make ( map [ string ] string )
178+ for key , value := range args [ 0 ].( Map ) {
179+ placeholders [ fmt . Sprintf ( "&{%s}" , key )] = fmt . Sprint ( value )
180+ }
143181
144- if value , ok := args [0 ].(Map )[key ]; ok {
145- if strValue , ok := value .(string ); ok {
146- return strValue
147- }
148- }
182+ // replace placeholders in the template with values
183+ for placeholder , value := range placeholders {
184+ t = strings .ReplaceAll (t , placeholder , value )
185+ }
149186
150- return match
151- })
187+ output = t
152188
153189 default :
154- return fmt .Errorf ("args must be ...string or gosvelt.Map" )
190+ return fmt .Errorf ("args must be...string or gosvelt.Map" )
155191 }
156192
157- c .SetCType ("text/html; charset=UTF-8" )
193+ c .SetCType (MTextHtmlUTF8 )
158194
159195 c .SetStatusCode (code )
160196 c .Write ([]byte (output ))
161197
162198 return nil
163199}
164200
165- // todo: fix "Error occurred: not found" but it work
166201func (c * Context ) File (code int , file string , compress ... bool ) error {
167202 fs := & fasthttp.FS {
168203 Root : "" ,
@@ -312,7 +347,7 @@ func (c *Context) Secure() bool {
312347
313348// get the url params with key
314349func (c * Context ) Param (key string ) string {
315- return fmt .Sprintf ("%s" , c .fasthttpCtx .UserValue (key )) // todo: remplace fasthttp context by something else
350+ return fmt .Sprintf ("%s" , c .fasthttpCtx .UserValue (key ))
316351}
317352
318353// add an cookie
@@ -330,7 +365,7 @@ func (c *Context) SetCookie(k, v string, expire time.Time) {
330365
331366// write to client
332367func (c * Context ) Write (body []byte ) {
333- c .fasthttpCtx . Write (body ) // todo: remplace fasthttp context by something else
368+ c .Res (). AppendBody (body ) // todo: remplace fasthttp context by something else
334369}
335370
336371// set response header
@@ -340,10 +375,10 @@ func (c *Context) SetHeader(key, value string) {
340375
341376// set response status code in int
342377func (c * Context ) SetStatusCode (code int ) {
343- c .Res ().SetStatusCode (code )
378+ c .Res ().Header . SetStatusCode (code )
344379}
345380
346381// set response content type
347382func (c * Context ) SetCType (ctype string ) {
348- c .SetHeader ("Content-Type" , ctype )
383+ c .Res (). Header . Set ("Content-Type" , ctype )
349384}
0 commit comments