-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpine.go
857 lines (742 loc) · 22.7 KB
/
pine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package pine
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
type Ctx struct {
Server *Server // Reference to *Server
Method string // HTTP method
BaseURI string // HTTP base uri
Request *http.Request // HTTP request
Response *responseWriterWrapper // HTTP response writer
params map[string]string // URL parameters
locals map[interface{}]interface{} // Local variables
indexHandler int // Index of the handler
route *Route // HTTP route
}
type responseWriterWrapper struct {
//we use the standard http package for Pine
http.ResponseWriter
//status code
statusCode int
//body of the response
body []byte
}
type Server struct {
mutex sync.Mutex
//standard http server
server *http.Server
//here you can customize shut down events.
//For future releases, we will add connection pools and
//shutting them down will be here
onShutdown []func()
//logger for errors
errorLog *log.Logger
//configuration for the server
config Config
//an array of registered routes when the server starts
//the route stack is divided by HTTP methods and route prefixes
stack [][]*Route
//middleware stack
middleware []Middleware
}
// Config is a struct holding the server settings.
// TODO: More encoders and decoders coming soon
type Config struct {
//defines the body limit for a request.
// -1 will decline any body size
//
// Default: 5 * 1024 * 1024
// Increase this to accept larger files
BodyLimit int64
// Defines the amount of time allowed to read an incoming request.
// This also includes the body.
//
// Default: 5 Seconds
ReadTimeout time.Duration
// Defines the maximum duration before timing out writes of the response.
// Increase this to allow longer response times.
//
// Default: 5 Seconds
WriteTimeout time.Duration
// Closes incomming connections after sending the first response to client.
// This is useful when you want to close connections after a specific route
//
// Default: false
DisableKeepAlive bool
// This defines the JSON encoder used by Pine for outgoing requests. The default is
// JSONMarshal
//
// Allowing for flexibility in using another json library for encoding
// Default: json.Marshal
JSONEncoder JSONMarshal
// This defines the JSON decoder used by Pine for incoming requests. The default is
// JSONUnmarshal
//
// Allowing for flexibility in using another json library for decoding
// Default: json.Unmarshal
JSONDecoder JSONUnmarshal
// RequestMethods provides customizibility for HTTP methods. You can add/remove methods as you wish.
//
// Optional. Default: DefaultMethods
RequestMethods []string
// UploadPath is the path where uploaded files will be saved
//
// Default: ./uploads
UploadPath string
// StaticPath is the path where static files will be served
//
// Default: "static"
StaticPath string
// ViewPath is the path where view files will be served
//
// Default: "views"
ViewPath string
// Engine is the template engine to use
//
// Default: html
Engine string
// TLSConfig is the configuration for TLS.
TLSConfig TLSConfig
}
// Route is a struct that holds all metadata for each registered handler.
// TODO: More features coming soon
type Route struct {
// Public fields
// HTTP method
Method string `json:"method"`
// Original registered route path
Path string `json:"path"`
// Ctx handlers
Handlers []Handler `json:"-"`
}
// cookie struct that defines the structure of a cookie
type Cookie struct {
//Name of the cookie
//
//Required field
Name string
//what data is stored in the cookie
//
//Required field
Value string
//determines the path in which the cookie is supposed to be used on
//you can set this to "/" so that every request will contain the cookie
Path string
//This allows the browser to associate your cookie with a specific domain
//when set to example.com cookies from example.com will always be sent
//with every request to example.com
Domain string
//Determines the specific time the cookie expires
//Max age is more prefered than expires
Expires time.Time
//Also sets the expiry date and you can use a string here instead
RawExpires string
//Max-Age field of the cookie determines how long the cookie
// stays in the browser before expiring
//if you want the cookies to expire immediately such as when a user logs out
//you can set this to -1
//
//accepts int value which should be the time in milliseconds you want
//the cookie to be stored in the browser
MaxAge int
//A boolean value that determines whether cookies will be sent over https
//or http only.
//
//Default is false and http can also send the cookies
Secure bool
//determines whether requests over http only can send the cookie
HttpOnly bool
//Cookies from the same domain can only be used on the specified domain
//Eg: cookies from app.example.com can only be used by app.example.com
//if you want all domains associated with example.com you can set this to
//*.example.com
//Now both app.example.com or dev.example.com can use the same cookie
//
//Options include the following:
// 0 - SameSite=Lax
// 1 - SameSite=Strict
// 2 - SameSite=None
//It will alwas default to Lax
SameSite SameSite
//All cookie data in string format. You do not need to set this
//Pine can handle it for you
Raw bool
//Pine will also take care of this
Unparsed []string
}
type TLSConfig struct {
ServeTLS bool
CertFile string
KeyFile string
}
type SameSite int
type Handler = func(*Ctx) error
type Middleware func(Handler) Handler
type JSONMarshal func(v interface{}) ([]byte, error)
type JSONUnmarshal func(data []byte, v interface{}) error
const (
DefaultBodyLimit = 5 * 1024 * 1024 //5MB
statusMessageMin = 100
statusMessageMax = 511
queueCapacity = 100
)
// Acceptable methods
// these are the default at the moment, more coming soon
const (
MethodGet = "GET"
MethodPost = "POST"
MethodPut = "PUT"
MethodDelete = "DELETE"
MethodPatch = "PATCH"
MethodHead = "HEAD"
MethodOptions = "OPTIONS"
methodUse = "USE"
)
// Default TLS config
var defaultTLSConfig = TLSConfig{
ServeTLS: false,
CertFile: "",
KeyFile: "",
}
// int equivalent of the mothod
// this is used to speed up route matching instead of trying to match strings
func (server *Server) methodInt(s string) int {
switch s {
case MethodGet:
return 0
case MethodHead:
return 1
case MethodPost:
return 2
case MethodPut:
return 3
case MethodDelete:
return 4
case MethodPatch:
return 5
case MethodOptions:
return 6
default:
return -1
}
}
// Default methods, more coming soon
var DefaultMethods = []string{
MethodGet,
MethodHead,
MethodPost,
MethodPut,
MethodDelete,
MethodPatch,
MethodOptions,
}
// This is called to start a new Pine server
// You can set the configuration as per your requirements
// or you can use the default and let Pine take care of it for you
func New(config ...Config) *Server {
cfg := Config{
BodyLimit: DefaultBodyLimit,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
DisableKeepAlive: false,
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
RequestMethods: DefaultMethods,
TLSConfig: defaultTLSConfig,
UploadPath: "./uploads/",
}
if len(config) > 0 {
//we only use the first config in the array
userConfig := config[0]
// Overwrite the default config with the user config
if userConfig.BodyLimit != 0 {
cfg.BodyLimit = userConfig.BodyLimit
}
if userConfig.ReadTimeout != 0 {
cfg.ReadTimeout = userConfig.ReadTimeout
}
if userConfig.WriteTimeout != 0 {
cfg.WriteTimeout = userConfig.WriteTimeout
}
if userConfig.DisableKeepAlive {
cfg.DisableKeepAlive = userConfig.DisableKeepAlive
}
if userConfig.JSONEncoder != nil {
cfg.JSONEncoder = userConfig.JSONEncoder
}
if userConfig.JSONDecoder != nil {
cfg.JSONDecoder = userConfig.JSONDecoder
}
if userConfig.RequestMethods != nil {
cfg.RequestMethods = userConfig.RequestMethods
}
if userConfig.RequestMethods != nil {
cfg.RequestMethods = userConfig.RequestMethods
}
if userConfig.TLSConfig.ServeTLS {
cfg.TLSConfig = userConfig.TLSConfig
}
if userConfig.UploadPath != "" {
cfg.UploadPath = userConfig.UploadPath
}
}
server := &Server{
config: cfg,
stack: make([][]*Route, len(cfg.RequestMethods)),
errorLog: log.New(os.Stderr, "ERROR: ", log.LstdFlags),
}
return server
}
// This method is called to register routes and their respective methods
// it also accepts handlers in case you want to use specific middlewares for specific routes
func (server *Server) AddRoute(method, path string, handlers ...Handler) {
server.mutex.Lock()
defer server.mutex.Unlock()
methodIndex := server.methodInt(method)
if methodIndex == -1 {
server.errorLog.Printf("Invalid HTTP method: %s", method)
return
}
route := &Route{
Method: method,
Path: path,
Handlers: handlers,
}
server.applyMiddleware(route)
server.stack[methodIndex] = append(server.stack[methodIndex], route)
}
func (server *Server) Get(path string, handlers ...Handler) {
server.AddRoute(MethodGet, path, handlers...)
}
func (server *Server) Post(path string, handlers ...Handler) {
server.AddRoute(MethodPost, path, handlers...)
}
func (server *Server) Put(path string, handlers ...Handler) {
server.AddRoute(MethodPut, path, handlers...)
}
func (server *Server) Patch(path string, handlers ...Handler) {
server.AddRoute(MethodPatch, path, handlers...)
}
func (server *Server) Delete(path string, handlers ...Handler) {
server.AddRoute(MethodDelete, path, handlers...)
}
func (server *Server) Options(path string, handlers ...Handler) {
server.AddRoute(MethodOptions, path, handlers...)
}
// Called to start the server after creating a new server
//
// You can put this in a go routine to handle graceful shut downs
// You can check out an example on https://github/BryanMwangi/pine/Examples/RunningInGoRoutine/main.go
func (server *Server) Start(address string) error {
httpServer := &http.Server{
Addr: address,
ReadTimeout: server.config.ReadTimeout,
WriteTimeout: server.config.WriteTimeout,
Handler: server,
}
server.server = httpServer
server.server.SetKeepAlivesEnabled(!server.config.DisableKeepAlive)
//certfile and keyfile are needed to handle https connections
//if the certfile and keyfile are not empty strings the server panic
if server.config.TLSConfig.ServeTLS {
if server.config.TLSConfig.CertFile == "" || server.config.TLSConfig.KeyFile == "" {
panic("certfile and keyfile are required to serve https")
}
return httpServer.ListenAndServeTLS(server.config.TLSConfig.CertFile, server.config.TLSConfig.KeyFile)
}
return httpServer.ListenAndServe()
}
// This is used to split the path into smaller chunks
// useful for getting queries and paramaters on specific paths
func splitPath(path string) []string {
return strings.Split(strings.Trim(path, "/"), "/")
}
// this is called on start up so that the server knows how to match routes and methods
func matchRoute(routePath, requestPath string) (bool, map[string]string) {
if routePath == requestPath {
return true, make(map[string]string)
}
// Example for a single parameter (e.g., "/user/:id")
// multiple parameters in dynamic routes can also be used
// for example /user/:id/record/:recordId
if len(routePath) > 0 && routePath[0] == '/' && len(requestPath) > 0 && requestPath[0] == '/' {
routeSegments := splitPath(routePath)
requestSegments := splitPath(requestPath)
if len(routeSegments) == len(requestSegments) {
params := make(map[string]string)
for i, segment := range routeSegments {
if segment[0] == ':' {
params[segment[1:]] = requestSegments[i]
} else if segment != requestSegments[i] {
return false, nil
}
}
return true, params
}
}
return false, nil
}
func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wrappedWriter := &responseWriterWrapper{ResponseWriter: w}
ctx := &Ctx{
Server: server,
Method: r.Method,
BaseURI: r.URL.Path,
Request: r,
Response: wrappedWriter,
params: make(map[string]string),
}
var matchedRoute *Route
for _, routes := range server.stack {
for _, route := range routes {
if matched, params := matchRoute(route.Path, r.URL.Path); matched {
matchedRoute = route
ctx.params = params
ctx.route = route
break
}
}
if matchedRoute != nil {
break
}
}
if matchedRoute != nil {
// for CORS we need to check if the method if OPTIONS and we pass the request
// to the first handler in the stack
// TODO: not just the first handler but all handlers except the last handler
// as middlewares are considered handlers.
if r.Method == MethodOptions {
matchedRoute.Handlers[0](ctx)
return
}
server.limitMaxRequestBodySize(w, r)
// Proceed to check if the method matches the method in the route
if matchedRoute.Method != r.Method {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
// Call the handlers for the matched route
for _, handler := range matchedRoute.Handlers {
err := handler(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
return
}
http.NotFound(w, r)
}
func (server *Server) limitMaxRequestBodySize(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, server.config.BodyLimit)
r.ParseMultipartForm(server.config.BodyLimit)
}
// Use method is for specifying middleware to be used on specific routes
// for example you could have an authentication middleware that checks for cookies with
// every request to authenticate the user request
func (server *Server) Use(middleware Middleware) {
server.middleware = append(server.middleware, middleware)
for _, routes := range server.stack {
for _, route := range routes {
server.applyMiddleware(route)
}
}
}
// apply middleware to the handler
func (server *Server) applyMiddleware(route *Route) {
for k, handler := range route.Handlers {
wrappedHandler := handler
for i := len(server.middleware) - 1; i >= 0; i-- {
wrappedHandler = server.middleware[i](wrappedHandler)
}
route.Handlers[k] = wrappedHandler
}
}
// Context returns the context of the request
// (This is the same as c.Request.Context()) as it returns a http.Request.Context()
func (c *Ctx) Context() context.Context {
return c.Request.Context()
}
// Next is used to execute the next handler in the stack
// This is useful when you want to execute the next handler in the stack
// but you want to do some additional work before executing the next handler
// for example, you can use this to authenticate the user
func (c *Ctx) Next() error {
if c.route == nil {
return fmt.Errorf("no route set for this context")
}
// Increment handler index
c.indexHandler++
// Check if we have more handlers to execute
if c.indexHandler >= len(c.route.Handlers) {
return fmt.Errorf("no more handlers to execute")
}
// Execute the next handler
return nil
}
// This is used to set cookies with the response
// you can set more than one cookie
// for example, a session token and a refresh token by calling this once
//
// Make sure the structure of your cookie meets the Cookie structure to avoid errors
func (c *Ctx) SetCookie(cookies ...Cookie) *Ctx {
existing := c.Response.Header().Get("Set-Cookie")
for _, cookie := range cookies {
var cookieHeader string
cookieHeader = cookie.Name + "=" + cookie.Value
if cookie.Path != "" {
cookieHeader += "; Path=" + cookie.Path
}
if cookie.Domain != "" {
cookieHeader += "; Domain=" + cookie.Domain
}
if !cookie.Expires.IsZero() {
cookieHeader += "; Expires=" + cookie.Expires.UTC().Format(http.TimeFormat)
}
if cookie.MaxAge > 0 {
cookieHeader += "; Max-Age=" + strconv.Itoa(cookie.MaxAge)
}
if cookie.Secure {
cookieHeader += "; Secure"
}
if cookie.HttpOnly {
cookieHeader += "; HttpOnly"
}
if cookie.SameSite != 0 {
cookieHeader += "; SameSite=" + sameSiteToString(cookie.SameSite)
}
if existing != "" {
existing += ", "
}
existing += cookieHeader
}
// Set all cookies
c.Response.Header().Set("Set-Cookie", existing)
return c
}
func sameSiteToString(s SameSite) string {
switch s {
case 0:
return "Lax"
case 1:
return "Strict"
case 2:
return "None"
default:
return "Lax"
}
}
// Used to read cookies with every request
// This is particularly useful for middlewares
func (c *Ctx) ReadCookie(name string) (*Cookie, error) {
cookieHeader := c.Request.Header.Get("Cookie")
if cookieHeader == "" {
return nil, nil // No cookies set
}
cookies := parseCookies(cookieHeader)
if cookie, ok := cookies[name]; ok {
return &cookie, nil
}
return nil, nil // Cookie not found
}
// parseCookies parses cookies from the Cookie header.
func parseCookies(cookieHeader string) map[string]Cookie {
cookies := make(map[string]Cookie)
pairs := strings.Split(cookieHeader, "; ")
for _, pair := range pairs {
if strings.Contains(pair, "=") {
parts := strings.SplitN(pair, "=", 2)
name := parts[0]
value := parts[1]
cookies[name] = Cookie{
Name: name,
Value: value,
}
}
}
return cookies
}
// This function is used to delete cookies
// You can pass multiple names of cookies to be deleted at once
func (c *Ctx) DeleteCookie(names ...string) *Ctx {
cookies := []Cookie{}
for _, name := range names {
cookie := Cookie{
Name: name,
Value: "",
Path: "/",
MaxAge: -1,
Secure: true,
HttpOnly: true,
SameSite: 0,
}
cookies = append(cookies, cookie)
}
err := c.SetCookie(cookies...)
if err != nil {
return err
}
return nil
}
// This is used to retrieve the header value specified by the key
// This is particularly useful when you want to retrieve specific headers
// from a request such as the Authorization header
func (c *Ctx) Header(key string) string {
return c.Request.Header.Get(key)
}
// Retrieves the IP address of the client
//
// If you notice the IP address is sometimes different from the actual IP address
// please open an issue and we will look into it
func (c *Ctx) IP() string {
IPAddress := c.Request.Header.Get("X-Real-Ip")
if IPAddress == "" {
IPAddress = c.Request.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
IPAddress = c.Request.RemoteAddr
}
return IPAddress
}
// This can be used to set the local values of a request
// This is particulary useful when unpacking data from a cookie
// Eg: You can parse a JWT token and decode the data inside it
// Then you can simply pack this data into the locals value of your request
// by doing c.Locals("key", data)
// Now whenever a request is made with that cookie if you set up your middleware
// to unpack the data in the locals field of your request you can access this data
// in your route
// Example:
// app.Get("/helloYou", authmiddleware(func(c *pine.Ctx) error {
// user:=c.Locals("key")
// return c.SendString("hello"+ user.name)
// }))
func (c *Ctx) Locals(key string, value ...interface{}) interface{} {
if len(value) == 0 {
return c.locals[key]
}
// Set the value
if c.locals == nil {
c.locals = make(map[interface{}]interface{})
}
c.locals[key] = value[0]
return value[0]
}
// used to extract params from a specified request
// Eg: app.Get("/hello/:user",func(c *Pine.Ctx)error{
// user:=c.Params("user")
//
// return c.SendString("hello"+user)
// })
func (c *Ctx) Params(key string) string {
return c.params[key]
}
// Same as Params above but saves you the time of converting a string params to an
// int type and you can extract the int value directly
// returns the int and error if any
// you can use the error to send back http.StatusBadRequest or 400 to the user
// if the user send a params that is not an int type
func (c *Ctx) ParamsInt(key string) (int, error) {
value, err := strconv.Atoi(c.params[key])
if err != nil {
return 0, fmt.Errorf("failed to convert parameter %s to int", err)
}
return value, nil
}
// used to obtain queries from requests
// EG: a user could send the request http://localhost:3000/hello?user=pine
// you can extract the user value by calling c.Query("user")
func (c *Ctx) Query(key string) string {
return c.Request.URL.Query().Get(key)
}
// JSON writes a JSON response
// If you would like to set the status code of the response, you can pass it as the second argument
//
// If you notice using c.Status(http.StatusOk).JSON(...json_payload) is not working
// properly, you can simply use c.JSON(...json_payload) without specifying the status
// Default status code is 200
func (c *Ctx) JSON(data interface{}, status ...int) error {
raw, err := c.Server.config.JSONEncoder(data)
if err != nil {
return err
}
c.Response.Header().Set("Content-Type", "application/json")
if len(status) > 0 {
c.Response.WriteHeader(status[0])
} else {
c.Response.WriteHeader(http.StatusOK)
}
c.Response.Write(raw)
return nil
}
// /You can use this to set the staus of a response
// Eg: c.Status(http.StatusOk) or c.Status(200)
//
// Does not work well with c.JSON(...) as the response will be sent as plain text
func (c *Ctx) Status(status int) *Ctx {
c.Response.WriteHeader(status)
return c
}
func (c *Ctx) Set(key string, val interface{}) *Ctx {
c.Response.SetHeader(key, fmt.Sprint(val))
return c
}
// SendString sends a string as the response
// Default status code is 200
func (c *Ctx) SendString(body string) error {
c.Response.Write([]byte(body))
return nil
}
// You can send just the status message here
func StatusMessage(status int) string {
if status < statusMessageMin || status > statusMessageMax {
return ""
}
return http.StatusText(status)
}
// SendStatus sends a status code as the response
// Does not send any body
// Does not accept additional methods like c.Status(200).JSON(...)
func (c *Ctx) SendStatus(status int) error {
c.Response.WriteHeader(status)
// Only set status body when there is no response body
if c.Response.statusCode == status && c.Response.BodyLen() == 0 {
return c.SendString(http.StatusText(status))
}
return nil
}
func (server *Server) ServeShutDown(ctx context.Context, hooks ...func()) error {
if server == nil {
return fmt.Errorf("shutdown: server is not running")
}
server.onShutdown = append(server.onShutdown, hooks...)
for _, hook := range server.onShutdown {
hook()
}
return server.server.Shutdown(ctx)
}
func (rw *responseWriterWrapper) WriteHeader(statusCode int) {
if rw.statusCode == 0 {
rw.statusCode = statusCode
rw.ResponseWriter.WriteHeader(statusCode)
}
}
func (rw *responseWriterWrapper) SetHeader(key, val string) {
rw.ResponseWriter.Header().Set(key, val)
}
func (rw *responseWriterWrapper) Write(data []byte) (int, error) {
rw.body = append(rw.body, data...)
return rw.ResponseWriter.Write(data)
}
func (rw *responseWriterWrapper) BodyLen() int {
return len(rw.body)
}