-
Notifications
You must be signed in to change notification settings - Fork 1
/
ksmux.go
1103 lines (992 loc) · 28.8 KB
/
ksmux.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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ksmux
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/pprof"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"time"
"github.com/kamalshkeir/kmap"
"github.com/kamalshkeir/ksmux/ws"
"github.com/kamalshkeir/lg"
"golang.org/x/crypto/acme/autocert"
)
var allrouters = kmap.New[string, *Router]()
func UpgradeConnection(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*ws.Conn, error) {
return ws.DefaultUpgraderKSMUX.Upgrade(w, r, responseHeader)
}
// Handler is a function that can be registered to a route to handle HTTP
// requests. Like http.HandlerFunc, but has a third parameter for the values of
// wildcards (path variables).
type Handler func(c *Context)
type GroupRouter struct {
*Router
Group string
midws []func(Handler) Handler
}
// Use chain handler middlewares
func (gr *GroupRouter) Use(middlewares ...func(Handler) Handler) {
gr.midws = append(gr.midws, middlewares...)
}
// Group create group path
func (router *Router) Group(prefix string) *GroupRouter {
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
prefix = strings.TrimSuffix(prefix, "/")
return &GroupRouter{
Router: router,
Group: prefix,
}
}
// Use chain global router middlewares
func (router *Router) Use(midws ...func(http.Handler) http.Handler) {
if len(router.middlewares) == 0 {
router.middlewares = midws
} else {
router.middlewares = append(router.middlewares, midws...)
}
}
type RouterConfig struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
GlobalOPTIONS http.Handler
NotFound http.Handler
MethodNotAllowed http.Handler
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
globalAllowed string
SaveMatchedPath bool
RedirectTrailingSlash bool
RedirectFixedPath bool
HandleMethodNotAllowed bool
HandleOPTIONS bool
}
type state struct {
withDocs bool
corsEnabled bool
swagFound bool
generateSwaggerJson bool
generateGoComments bool
docsPatterns []*Route
}
// Router is a http.Handler which can be used to dispatch requests to different
// handler functions via configurable routes
type Router struct {
Server *http.Server
AutoCertManager *autocert.Manager
Config *Config
RouterConfig *RouterConfig
state *state
trees map[string]*node
middlewares []func(http.Handler) http.Handler
proxies *kmap.SafeMap[string, http.Handler]
paramsPool sync.Pool
secure bool
maxParams uint16
}
type Config struct {
Address string
Domain string
SubDomains []string
CertPath string
CertKeyPath string
MediaDir string
isTls bool
host string
port string
onShutdown []func() error
}
// New returns a new initialized Router.
// Path auto-correction, including trailing slashes, is enabled by default.
func New(config ...Config) *Router {
router := &Router{
RouterConfig: &RouterConfig{
ReadTimeout: 5 * time.Second,
WriteTimeout: 20 * time.Second,
IdleTimeout: 20 * time.Second,
RedirectTrailingSlash: true,
RedirectFixedPath: true,
HandleMethodNotAllowed: true,
HandleOPTIONS: true,
},
Config: &Config{
Address: "localhost:9313",
MediaDir: "media",
host: "localhost",
port: "9313",
},
state: &state{
generateGoComments: true,
docsPatterns: []*Route{},
},
}
if len(config) > 0 {
router.Config = &config[0]
if router.Config.Address != "" {
host, port, err := net.SplitHostPort(router.Config.Address)
if err == nil {
if host != "" {
router.Config.host = host
} else {
router.Config.host = "localhost"
}
if port != "" {
if port == "443" {
router.secure = true
}
router.Config.port = port
}
router.Config.Address = router.Config.host + ":" + router.Config.port
} else {
lg.ErrorC("Address not valid")
return router
}
}
if router.Config.CertPath != "" {
router.Config.isTls = true
if router.Config.Domain != "" {
router.Config.Address = router.Config.Domain
router.Config.Domain = ""
}
host, port, err := net.SplitHostPort(router.Config.Address)
if err == nil {
if host != "" {
router.Config.host = host
}
if port != "" {
if port == "443" {
router.secure = true
}
router.Config.port = port
}
if host == "" && port == "" {
router.Config.port = "443"
router.secure = true
}
} else {
lg.ErrorC("Address not valid")
return router
}
}
if router.Config.Domain != "" {
router.Config.isTls = true
router.secure = true
host, port, _ := net.SplitHostPort(router.Config.Address)
if host == "" && port == "" {
router.Config.port = "443"
}
if port != "" {
router.Config.port = port
} else {
port = "443"
err := checkDomain(router.Config.Domain)
if err == nil {
router.Config.Address = router.Config.Domain
}
}
}
}
if router.Config.Address != "" {
allrouters.Set(router.Config.Address, router)
} else if router.Config.Domain != "" {
allrouters.Set(router.Config.Domain, router)
}
return router
}
func GetRouters() *kmap.SafeMap[string, *Router] {
return allrouters
}
func GetRouter(addrOrDomain string) (*Router, bool) {
return allrouters.Get(addrOrDomain)
}
func GetFirstRouter() *Router {
if firstRouter != nil {
return firstRouter
}
allrouters.Range(func(_ string, value *Router) bool {
firstRouter = value
return false
})
return firstRouter
}
func (router *Router) IsTls() bool {
return router.Config.isTls
}
func (router *Router) Address() string {
return router.Config.Address
}
func (router *Router) Host() string {
return router.Config.host
}
func (router *Router) Port() string {
return router.Config.port
}
// Run HTTP server on address
func (router *Router) Run() {
router.initServer(router.Config.Address)
// Create a done channel to handle shutdown
serverDone := make(chan struct{})
// Listen and serve
go func() {
if err := router.Server.ListenAndServe(); err != http.ErrServerClosed {
lg.Error("Unable to shutdown the server", "err", err)
os.Exit(1)
} else {
lg.Info("Server Off")
}
close(serverDone)
}()
if router.state.generateSwaggerJson {
DocsGeneralDefaults.Host = router.Config.Address
for i := len(router.state.docsPatterns) - 1; i >= 0; i-- {
route := router.state.docsPatterns[i]
if route.Docs == nil || !route.Docs.Triggered {
router.state.docsPatterns = append(router.state.docsPatterns[:i], router.state.docsPatterns[i+1:]...)
}
}
if router.state.generateGoComments {
GenerateGoDocsComments()
}
GenerateJsonDocs()
OnDocsGenerationReady()
}
lg.Printfs("mgrunning on http://%s\n", router.Config.Address)
// Wait for interrupt signal
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
<-s
// Create a deadline for shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Run any registered shutdown handlers
for _, sh := range router.Config.onShutdown {
if err := sh(); err != nil {
lg.Error("shutdown handler error:", "err", err)
}
}
// Attempt graceful shutdown
if err := router.Server.Shutdown(ctx); err != nil {
lg.Error("shutdown error:", "err", err)
}
// Wait for server to finish
<-serverDone
}
// RunTLS HTTPS server using certificates
func (router *Router) RunTLS() {
router.initServer(router.Config.Address)
router.Server.TLSConfig.MinVersion = tls.VersionTLS12
router.Server.TLSConfig.NextProtos = append([]string{"h2", "http/1.1"}, router.Server.TLSConfig.NextProtos...)
// Create a done channel to handle shutdown
serverDone := make(chan struct{})
go func() {
lg.Printfs("mgrunning on https://%s\n", router.Config.Address)
if err := router.Server.ListenAndServeTLS(router.Config.CertPath, router.Config.CertKeyPath); err != http.ErrServerClosed {
lg.Error("Unable to shutdown the server", "err", err)
os.Exit(1)
} else {
lg.Info("Server Off")
}
close(serverDone)
}()
if router.state.generateSwaggerJson {
DocsGeneralDefaults.Host = router.Config.Address
for i := len(router.state.docsPatterns) - 1; i >= 0; i-- {
route := router.state.docsPatterns[i]
if route.Docs == nil || !route.Docs.Triggered {
router.state.docsPatterns = append(router.state.docsPatterns[:i], router.state.docsPatterns[i+1:]...)
}
}
if router.state.generateGoComments {
GenerateGoDocsComments()
}
GenerateJsonDocs()
OnDocsGenerationReady()
}
// Wait for interrupt signal
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
<-s
// Create a deadline for shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Run any registered shutdown handlers
for _, sh := range router.Config.onShutdown {
if err := sh(); err != nil {
lg.Error("shutdown handler error:", "err", err)
}
}
// Attempt graceful shutdown
if err := router.Server.Shutdown(ctx); err != nil {
lg.Error("shutdown error:", "err", err)
}
// Wait for server to finish
<-serverDone
}
// RunAutoTLS HTTPS server generate certificates and handle renew
func (router *Router) RunAutoTLS() {
if router.proxies.Len() > 0 {
if len(router.Config.SubDomains) != router.proxies.Len() {
prs := router.proxies.Keys()
for _, pr := range prs {
found := false
for _, d := range router.Config.SubDomains {
if d == pr {
found = true
break
}
}
if !found {
router.Config.SubDomains = append(router.Config.SubDomains, pr)
}
}
}
}
if router.AutoCertManager == nil {
certManager, tlsconf := router.CreateServerCerts(router.Config.Domain, router.Config.SubDomains...)
if certManager == nil || tlsconf == nil {
lg.Fatal("unable to create tlsconfig")
return
}
router.AutoCertManager = certManager
}
router.initAutoServer(router.AutoCertManager.TLSConfig())
// Create a done channel to handle shutdown
serverDone := make(chan struct{})
// Start HTTP challenge server
go http.ListenAndServe(":80", router.AutoCertManager.HTTPHandler(nil))
go func() {
lg.Printfs("mgrunning on https://%s , subdomains: %v\n", router.Config.Domain, router.Config.SubDomains)
if err := router.Server.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
lg.Error("Unable to run the server", "err", err)
os.Exit(1)
} else {
lg.Printfs("grServer Off !\n")
}
close(serverDone)
}()
if router.state.generateSwaggerJson {
DocsGeneralDefaults.Host = router.Config.Domain
for i := len(router.state.docsPatterns) - 1; i >= 0; i-- {
route := router.state.docsPatterns[i]
if route.Docs == nil || !route.Docs.Triggered {
router.state.docsPatterns = append(router.state.docsPatterns[:i], router.state.docsPatterns[i+1:]...)
}
}
if router.state.generateGoComments {
GenerateGoDocsComments()
}
GenerateJsonDocs()
OnDocsGenerationReady()
}
// Wait for interrupt signal
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
<-s
// Create a deadline for shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Run any registered shutdown handlers
for _, sh := range router.Config.onShutdown {
if err := sh(); err != nil {
lg.Error("shutdown handler error:", "err", err)
}
}
// Attempt graceful shutdown
if err := router.Server.Shutdown(ctx); err != nil {
lg.Error("shutdown error:", "err", err)
}
// Wait for server to finish
<-serverDone
}
// Get is a shortcut for router.Handle(http.MethodGet, path, handler)
func (r *Router) Get(path string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return r.Handle(http.MethodGet, path, handler, origines...)
}
func (gr *GroupRouter) Get(pattern string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
var h Handler
if len(gr.midws) > 0 {
for i := range gr.midws {
if i == 0 {
h = gr.midws[0](handler)
} else {
h = gr.midws[i](h)
}
}
} else {
h = handler
}
return gr.Router.Handle(http.MethodGet, gr.Group+pattern, handler, origines...)
}
// Head is a shortcut for router.Handle(http.MethodHead, path, handler)
func (r *Router) Head(path string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return r.Handle(http.MethodHead, path, handler, origines...)
}
func (gr *GroupRouter) Head(pattern string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
var h Handler
if len(gr.midws) > 0 {
for i := range gr.midws {
if i == 0 {
h = gr.midws[0](handler)
} else {
h = gr.midws[i](h)
}
}
} else {
h = handler
}
return gr.Router.Handle(http.MethodHead, gr.Group+pattern, handler, origines...)
}
// Options is a shortcut for router.Handle(http.MethodOptions, path, handler)
func (r *Router) Options(path string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return r.Handle(http.MethodOptions, path, handler, origines...)
}
func (gr *GroupRouter) Options(pattern string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
var h Handler
if len(gr.midws) > 0 {
for i := range gr.midws {
if i == 0 {
h = gr.midws[0](handler)
} else {
h = gr.midws[i](h)
}
}
} else {
h = handler
}
return gr.Router.Handle(http.MethodOptions, gr.Group+pattern, handler, origines...)
}
// Post is a shortcut for router.Handle(http.MethodPost, path, handler)
func (r *Router) Post(path string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return r.Handle(http.MethodPost, path, handler, origines...)
}
func (gr *GroupRouter) Post(pattern string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
var h Handler
if len(gr.midws) > 0 {
for i := range gr.midws {
if i == 0 {
h = gr.midws[0](handler)
} else {
h = gr.midws[i](h)
}
}
} else {
h = handler
}
return gr.Router.Handle(http.MethodPost, gr.Group+pattern, handler, origines...)
}
// Put is a shortcut for router.Handle(http.MethodPut, path, handler)
func (r *Router) Put(path string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return r.Handle(http.MethodPut, path, handler, origines...)
}
func (gr *GroupRouter) Put(pattern string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
var h Handler
if len(gr.midws) > 0 {
for i := range gr.midws {
if i == 0 {
h = gr.midws[0](handler)
} else {
h = gr.midws[i](h)
}
}
} else {
h = handler
}
return gr.Router.Handle(http.MethodPut, gr.Group+pattern, handler, origines...)
}
// Patch is a shortcut for router.Handle(http.MethodPatch, path, handler)
func (r *Router) Patch(path string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return r.Handle(http.MethodPatch, path, handler, origines...)
}
func (gr *GroupRouter) Patch(pattern string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
var h Handler
if len(gr.midws) > 0 {
for i := range gr.midws {
if i == 0 {
h = gr.midws[0](handler)
} else {
h = gr.midws[i](h)
}
}
} else {
h = handler
}
return gr.Router.Handle(http.MethodPatch, gr.Group+pattern, handler, origines...)
}
// Delete is a shortcut for router.Handle(http.MethodDelete, path, handler)
func (r *Router) Delete(path string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return r.Handle(http.MethodDelete, path, handler, origines...)
}
func (gr *GroupRouter) Delete(pattern string, handler Handler, origines ...string) *Route {
if !strings.HasPrefix(pattern, "/") {
pattern = "/" + pattern
}
var h Handler
if len(gr.midws) > 0 {
for i := range gr.midws {
if i == 0 {
h = gr.midws[0](handler)
} else {
h = gr.midws[i](h)
}
}
} else {
h = handler
}
return gr.Router.Handle(http.MethodDelete, gr.Group+pattern, handler, origines...)
}
// Handle registers a new request handler with the given path and method.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (r *Router) Handle(method, path string, handler Handler, origines ...string) *Route {
varsCount := uint16(0)
if method == "" {
lg.Error("method cannot be empty", "path", path)
return nil
}
if handler == nil {
lg.Error("missing handler", "path", path)
return nil
}
if r.RouterConfig.SaveMatchedPath {
varsCount++
handler = r.saveMatchedRoutePath(path, handler)
}
route := Route{}
route.Method = method
route.Pattern = path
route.Handler = handler
if r.state.corsEnabled && len(origines) > 0 {
route.Origines = strings.Join(origines, ",")
}
if r.state.withDocs && !strings.Contains(path, "*") && method != "WS" && method != "SSE" {
d := &DocsRoute{
Pattern: path,
Summary: "A " + method + " request on " + path,
Description: "A " + method + " request on " + path,
Method: strings.ToLower(method),
Accept: "json",
Produce: "json",
Params: []string{},
}
route.Docs = d
r.state.docsPatterns = append(r.state.docsPatterns, &route)
}
if r.trees == nil {
r.trees = make(map[string]*node)
}
root := r.trees[method]
if root == nil {
root = new(node)
r.trees[method] = root
r.RouterConfig.globalAllowed = r.allowed("*", "")
}
root.addPath(path, handler, origines...)
// Update maxParams
if paramsCount := countParams(path); paramsCount+varsCount > r.maxParams {
r.maxParams = paramsCount + varsCount
}
// Lazy-init paramsPool alloc func
if r.paramsPool.New == nil && r.maxParams > 0 {
r.paramsPool.New = func() interface{} {
ps := make(Params, 0, r.maxParams)
return &ps
}
}
return &route
}
// HandlerFunc is an adapter which allows the usage of an http.HandlerFunc as a
// request handler.
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc, origines ...string) *Route {
return r.Handle(method, path, func(c *Context) {
if len(c.Params) > 0 {
ctx := c.Request.Context()
ctx = context.WithValue(ctx, ctxKey, c.Params)
c.Request = c.Request.WithContext(ctx)
}
handler.ServeHTTP(c.ResponseWriter, c.Request)
}, origines...)
}
func (handler Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := contextPool.Get().(*Context)
ctx.ResponseWriter = w
ctx.Request = r
ctx.Params = GetParamsFromCtx(r.Context())
handler(ctx)
ctx.reset()
contextPool.Put(ctx)
}
// ServeFiles serves files from the given file system root.
// The path must end with "/*filepath", files are then served from the local
// path /defined/root/dir/*filepath.
// For example if root is "/etc" and *filepath is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a http.FileServer is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// To use the operating system's file system implementation,
// use http.Dir:
//
// router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
func (r *Router) ServeFiles(path string, root http.FileSystem) {
if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
lg.Error("path must end with /*filepath in path", "path", path)
return
}
fileServer := http.FileServer(root)
r.Get(path, func(c *Context) {
c.Request.URL.Path = c.Param("filepath")
fileServer.ServeHTTP(c.ResponseWriter, c.Request)
})
}
// WithPprof enable std library pprof at /debug/pprof, prefix default to 'debug'
func (router *Router) WithPprof(path ...string) {
if len(path) > 0 && strings.Contains(path[0], "/") {
path[0] = strings.TrimPrefix(path[0], "/")
path[0] = strings.TrimSuffix(path[0], "/")
} else {
path = append(path, "debug")
}
handler := func(c *Context) {
ty := c.Param("type")
ty = strings.TrimPrefix(ty, "/")
ty = strings.TrimSuffix(ty, "/")
switch ty {
case "pprof", "":
pprof.Index(c.ResponseWriter, c.Request)
return
case "profile":
pprof.Profile(c.ResponseWriter, c.Request)
return
default:
pprof.Handler(ty).ServeHTTP(c.ResponseWriter, c.Request)
return
}
}
router.Get("/"+path[0]+"/:type", handler)
}
// WithMetrics take prometheus handler and serve metrics on path or default /metrics
func (router *Router) WithMetrics(httpHandler http.Handler, path ...string) {
if len(path) > 0 && strings.Contains(path[0], "/") {
path[0] = strings.TrimPrefix(path[0], "/")
} else {
path = append(path, "metrics")
}
router.Get("/"+path[0], func(c *Context) {
httpHandler.ServeHTTP(c.ResponseWriter, c.Request)
})
}
// WithDocs check and install swagger, and generate json and go docs at the end , after the server run, you can use ksmux.OnDocsGenerationReady()
// genGoDocs default to true if genJsonDocs
func (router *Router) WithDocs(genJsonDocs bool, genGoDocs ...bool) *Router {
router.state.withDocs = true
router.state.generateSwaggerJson = genJsonDocs
if len(genGoDocs) > 0 && !genGoDocs[0] {
router.state.generateGoComments = false
}
if !router.state.swagFound && genJsonDocs {
err := CheckAndInstallSwagger()
if lg.CheckError(err) {
return router
}
}
return router
}
// EnableDomainCheck enable only the domain check from ksmux methods Get,Post,... (does not add cors middleware)
func (router *Router) EnableDomainCheck() {
if !router.state.corsEnabled {
router.state.corsEnabled = true
}
}
// ServeHTTP makes the router implement the http.Handler interface.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r.RouterConfig.PanicHandler != nil {
defer r.recv(w, req)
}
if r.secure {
if v := os.Getenv("SSL_MODE"); v != "" && v == "dev" {
w.Header().Set("Strict-Transport-Security", "max-age=0; includeSubDomains")
} else {
w.Header().Set("Strict-Transport-Security", "max-age=15768000 ; includeSubDomains")
}
}
path := req.URL.Path
hasTrailingSlash := len(path) > 1 && path[len(path)-1] == '/'
if root := r.trees[req.Method]; root != nil {
if handler, ps, _, origines := root.getHandler(path, r.getParams); handler != nil {
if r.state.corsEnabled && origines != "" && !strings.Contains(origines, "*") && !strings.Contains(origines, req.Header.Get("Origin")) {
http.Error(w,
http.StatusText(http.StatusForbidden),
http.StatusForbidden,
)
return
}
c := contextPool.Get().(*Context)
c.ResponseWriter = w
c.Request = req
defer contextPool.Put(c)
if ps != nil {
c.Params = *ps
handler(c)
r.putParams(ps)
c.reset()
} else {
handler(c)
c.reset()
}
return
} else if r.RouterConfig.RedirectTrailingSlash && hasTrailingSlash {
// Try without trailing slash
pathWithoutSlash := path[:len(path)-1]
if handler, _, _, _ := root.getHandler(pathWithoutSlash, nil); handler != nil {
code := http.StatusMovedPermanently // 301 for GET requests
if req.Method != http.MethodGet {
code = http.StatusPermanentRedirect // 308 for all other methods
}
req.URL.Path = pathWithoutSlash
http.Redirect(w, req, req.URL.String(), code)
return
}
}
}
// Handle OPTIONS
if req.Method == http.MethodOptions && r.RouterConfig.HandleOPTIONS {
if allow := r.allowed(path, http.MethodOptions); allow != "" {
w.Header().Set("Allow", allow)
if r.RouterConfig.GlobalOPTIONS != nil {
r.RouterConfig.GlobalOPTIONS.ServeHTTP(w, req)
}
return
}
} else if r.RouterConfig.HandleMethodNotAllowed {
if allow := r.allowed(path, req.Method); allow != "" {
w.Header().Set("Allow", allow)
if r.RouterConfig.MethodNotAllowed != nil {
r.RouterConfig.MethodNotAllowed.ServeHTTP(w, req)
} else {
http.Error(w,
http.StatusText(http.StatusMethodNotAllowed),
http.StatusMethodNotAllowed,
)
}
return
}
}
// Handle 404
if r.RouterConfig.NotFound != nil {
r.RouterConfig.NotFound.ServeHTTP(w, req)
} else {
http.NotFound(w, req)
}
}
func (router *Router) initServer(addr string) {
var h http.Handler
if len(router.middlewares) > 0 {
for i := range router.middlewares {
if i == 0 {
h = router.middlewares[0](router)
} else {
h = router.middlewares[i](h)
}
}
} else {
h = router
}
server := http.Server{
Addr: addr,
Handler: h,
ReadTimeout: router.RouterConfig.ReadTimeout,
WriteTimeout: router.RouterConfig.WriteTimeout,
IdleTimeout: router.RouterConfig.IdleTimeout,
}
router.Server = &server
}
func (router *Router) initAutoServer(tlsconf *tls.Config) {
var h http.Handler
if len(router.middlewares) > 0 {
for i := range router.middlewares {
if i == 0 {
h = router.middlewares[0](router)
} else {
h = router.middlewares[i](h)
}
}
} else {
h = router
}
// Setup Server
server := http.Server{
Addr: ":" + router.Config.port,
Handler: h,
ReadTimeout: router.RouterConfig.ReadTimeout,
WriteTimeout: router.RouterConfig.WriteTimeout,
IdleTimeout: router.RouterConfig.IdleTimeout,
TLSConfig: tlsconf,
}
router.Server = &server
}
func (r *Router) PrintTree() {
fmt.Println("Router Tree:")
for method, root := range r.trees {
fmt.Printf("\n[%s]\n", method)
printNode(root, "", true)
}
}
func printNode(n *node, prefix string, isLast bool) {
// Choose the symbols for the tree structure
nodePrefix := "└── "
childPrefix := " "
if !isLast {
nodePrefix = "├── "
childPrefix = "│ "
}
// Print current node
handlerStr := ""
if n.handler != nil {
handlerStr = " [handler]"
}
paramStr := ""
if n.paramInfo != nil && len(n.paramInfo.names) > 0 {
paramStr = " " + fmt.Sprint(n.paramInfo.names)
}
if n.nType == catchAll {
// Remove the * from path for catchAll
cleanPath := strings.TrimPrefix(n.path, "*")