@@ -35,7 +35,7 @@ import (
35
35
const Version = "3.0.0-beta.4"
36
36
37
37
// Handler defines a function to serve HTTP requests.
38
- type Handler = func (ctx Ctx ) error
38
+ type Handler [ TCtx CtxGeneric [ TCtx ]] = func (ctx TCtx ) error
39
39
40
40
// Map is a shortcut for map[string]any, useful for JSON returns
41
41
type Map map [string ]any
@@ -78,7 +78,7 @@ type Storage interface {
78
78
// return c.Status(code).SendString(err.Error())
79
79
// }
80
80
// app := fiber.New(cfg)
81
- type ErrorHandler = func (Ctx , error ) error
81
+ type ErrorHandler [ TCtx CtxGeneric [ TCtx ]] = func (TCtx , error ) error
82
82
83
83
// Error represents an error that occurred while handling a request.
84
84
type Error struct {
@@ -97,29 +97,29 @@ type App[TCtx CtxGeneric[TCtx]] struct {
97
97
// Converts byte slice to a string
98
98
getString func (b []byte ) string
99
99
// Hooks
100
- hooks * Hooks
100
+ hooks * Hooks [ TCtx ]
101
101
// Latest route & group
102
- latestRoute * Route
102
+ latestRoute * Route [ TCtx ]
103
103
// newCtxFunc
104
104
newCtxFunc func (app * App [TCtx ]) CustomCtx [TCtx ]
105
105
// TLS handler
106
106
tlsHandler * TLSHandler
107
107
// Mount fields
108
- mountFields * mountFields
108
+ mountFields * mountFields [ TCtx ]
109
109
// Route stack divided by HTTP methods
110
- stack [][]* Route
110
+ stack [][]* Route [ TCtx ]
111
111
// Route stack divided by HTTP methods and route prefixes
112
- treeStack []map [string ][]* Route
112
+ treeStack []map [string ][]* Route [ TCtx ]
113
113
// custom binders
114
114
customBinders []CustomBinder
115
115
// customConstraints is a list of external constraints
116
116
customConstraints []CustomConstraint
117
117
// sendfiles stores configurations for handling ctx.SendFile operations
118
118
sendfiles []* sendFileStore
119
119
// App config
120
- config Config
120
+ config Config [ TCtx ]
121
121
// Indicates if the value was explicitly configured
122
- configured Config
122
+ configured Config [ TCtx ]
123
123
// sendfilesMutex is a mutex used for sendfile operations
124
124
sendfilesMutex sync.RWMutex
125
125
mutex sync.Mutex
@@ -132,7 +132,7 @@ type App[TCtx CtxGeneric[TCtx]] struct {
132
132
}
133
133
134
134
// Config is a struct holding the server settings.
135
- type Config struct { //nolint:govet // Aligning the struct fields is not necessary. betteralign:ignore
135
+ type Config [ TCtx CtxGeneric [ TCtx ]] struct { //nolint:govet // Aligning the struct fields is not necessary. betteralign:ignore
136
136
// Enables the "Server: value" HTTP header.
137
137
//
138
138
// Default: ""
@@ -250,7 +250,7 @@ type Config struct { //nolint:govet // Aligning the struct fields is not necessa
250
250
// ErrorHandler is executed when an error is returned from fiber.Handler.
251
251
//
252
252
// Default: DefaultErrorHandler
253
- ErrorHandler ErrorHandler `json:"-"`
253
+ ErrorHandler ErrorHandler [ TCtx ] `json:"-"`
254
254
255
255
// When set to true, disables keep-alive connections.
256
256
// The server will close incoming connections after sending the first response to client.
@@ -470,7 +470,7 @@ var DefaultMethods = []string{
470
470
}
471
471
472
472
// DefaultErrorHandler that process return errors from handlers
473
- func DefaultErrorHandler (c Ctx , err error ) error {
473
+ func DefaultErrorHandler [ TCtx CtxGeneric [ TCtx ]] (c TCtx , err error ) error {
474
474
code := StatusInternalServerError
475
475
var e * Error
476
476
if errors .As (err , & e ) {
@@ -490,7 +490,7 @@ func DefaultErrorHandler(c Ctx, err error) error {
490
490
// Prefork: true,
491
491
// ServerHeader: "Fiber",
492
492
// })
493
- func New (config ... Config ) * App [* DefaultCtx ] {
493
+ func New (config ... Config [ * DefaultCtx ] ) * App [* DefaultCtx ] {
494
494
app := newApp [* DefaultCtx ](config ... )
495
495
496
496
// Init app
@@ -517,7 +517,7 @@ func New(config ...Config) *App[*DefaultCtx] {
517
517
// Prefork: true,
518
518
// ServerHeader: "Fiber",
519
519
// })
520
- func NewWithCustomCtx [TCtx CtxGeneric [TCtx ]](newCtxFunc func (app * App [TCtx ]) CustomCtx [TCtx ], config ... Config ) * App [TCtx ] {
520
+ func NewWithCustomCtx [TCtx CtxGeneric [TCtx ]](newCtxFunc func (app * App [TCtx ]) CustomCtx [TCtx ], config ... Config [ TCtx ] ) * App [TCtx ] {
521
521
app := newApp [TCtx ](config ... )
522
522
523
523
// Set newCtxFunc
@@ -530,14 +530,14 @@ func NewWithCustomCtx[TCtx CtxGeneric[TCtx]](newCtxFunc func(app *App[TCtx]) Cus
530
530
}
531
531
532
532
// newApp creates a new Fiber named instance.
533
- func newApp [TCtx CtxGeneric [TCtx ]](config ... Config ) * App [TCtx ] {
533
+ func newApp [TCtx CtxGeneric [TCtx ]](config ... Config [ TCtx ] ) * App [TCtx ] {
534
534
// Create a new app
535
535
app := & App [TCtx ]{
536
536
// Create config
537
- config : Config {},
537
+ config : Config [ TCtx ] {},
538
538
getBytes : utils .UnsafeBytes ,
539
539
getString : utils .UnsafeString ,
540
- latestRoute : & Route {},
540
+ latestRoute : & Route [ TCtx ] {},
541
541
customBinders : []CustomBinder {},
542
542
sendfiles : []* sendFileStore {},
543
543
}
@@ -550,7 +550,7 @@ func newApp[TCtx CtxGeneric[TCtx]](config ...Config) *App[TCtx] {
550
550
}
551
551
552
552
// Define hooks
553
- app .hooks = newHooks (app )
553
+ app .hooks = newHooks [ TCtx ] (app )
554
554
555
555
// Define mountFields
556
556
app .mountFields = newMountFields (app )
@@ -589,7 +589,7 @@ func newApp[TCtx CtxGeneric[TCtx]](config ...Config) *App[TCtx] {
589
589
}
590
590
591
591
if app .config .ErrorHandler == nil {
592
- app .config .ErrorHandler = DefaultErrorHandler
592
+ app .config .ErrorHandler = DefaultErrorHandler [ TCtx ]
593
593
}
594
594
595
595
if app .config .JSONEncoder == nil {
@@ -620,8 +620,8 @@ func newApp[TCtx CtxGeneric[TCtx]](config ...Config) *App[TCtx] {
620
620
}
621
621
622
622
// Create router stack
623
- app .stack = make ([][]* Route , len (app .config .RequestMethods ))
624
- app .treeStack = make ([]map [string ][]* Route , len (app .config .RequestMethods ))
623
+ app .stack = make ([][]* Route [ TCtx ] , len (app .config .RequestMethods ))
624
+ app .treeStack = make ([]map [string ][]* Route [ TCtx ] , len (app .config .RequestMethods ))
625
625
626
626
// Override colors
627
627
app .config .ColorScheme = defaultColors (app .config .ColorScheme )
@@ -669,7 +669,7 @@ func (app *App[TCtx]) SetTLSHandler(tlsHandler *TLSHandler) {
669
669
}
670
670
671
671
// Name Assign name to specific route.
672
- func (app * App [TCtx ]) Name (name string ) Router {
672
+ func (app * App [TCtx ]) Name (name string ) Router [ TCtx ] {
673
673
app .mutex .Lock ()
674
674
defer app .mutex .Unlock ()
675
675
@@ -695,7 +695,7 @@ func (app *App[TCtx]) Name(name string) Router {
695
695
}
696
696
697
697
// GetRoute Get route by name
698
- func (app * App [TCtx ]) GetRoute (name string ) Route {
698
+ func (app * App [TCtx ]) GetRoute (name string ) Route [ TCtx ] {
699
699
for _ , routes := range app .stack {
700
700
for _ , route := range routes {
701
701
if route .Name == name {
@@ -704,12 +704,12 @@ func (app *App[TCtx]) GetRoute(name string) Route {
704
704
}
705
705
}
706
706
707
- return Route {}
707
+ return Route [ TCtx ] {}
708
708
}
709
709
710
710
// GetRoutes Get all routes. When filterUseOption equal to true, it will filter the routes registered by the middleware.
711
- func (app * App [TCtx ]) GetRoutes (filterUseOption ... bool ) []Route {
712
- var rs []Route
711
+ func (app * App [TCtx ]) GetRoutes (filterUseOption ... bool ) []Route [ TCtx ] {
712
+ var rs []Route [ TCtx ]
713
713
var filterUse bool
714
714
if len (filterUseOption ) != 0 {
715
715
filterUse = filterUseOption [0 ]
@@ -746,11 +746,11 @@ func (app *App[TCtx]) GetRoutes(filterUseOption ...bool) []Route {
746
746
// app.Use("/mounted-path", subApp)
747
747
//
748
748
// This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...
749
- func (app * App [TCtx ]) Use (args ... any ) Router {
749
+ func (app * App [TCtx ]) Use (args ... any ) Router [ TCtx ] {
750
750
var prefix string
751
751
var subApp * App [TCtx ]
752
752
var prefixes []string
753
- var handlers []Handler
753
+ var handlers []Handler [ TCtx ]
754
754
755
755
for i := 0 ; i < len (args ); i ++ {
756
756
switch arg := args [i ].(type ) {
@@ -760,7 +760,7 @@ func (app *App[TCtx]) Use(args ...any) Router {
760
760
subApp = arg
761
761
case []string :
762
762
prefixes = arg
763
- case Handler :
763
+ case Handler [ TCtx ] :
764
764
handlers = append (handlers , arg )
765
765
default :
766
766
panic (fmt .Sprintf ("use: invalid handler %v\n " , reflect .TypeOf (arg )))
@@ -785,75 +785,75 @@ func (app *App[TCtx]) Use(args ...any) Router {
785
785
786
786
// Get registers a route for GET methods that requests a representation
787
787
// of the specified resource. Requests using GET should only retrieve data.
788
- func (app * App [TCtx ]) Get (path string , handler Handler , middleware ... Handler ) Router {
788
+ func (app * App [TCtx ]) Get (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
789
789
return app .Add ([]string {MethodGet }, path , handler , middleware ... )
790
790
}
791
791
792
792
// Head registers a route for HEAD methods that asks for a response identical
793
793
// to that of a GET request, but without the response body.
794
- func (app * App [TCtx ]) Head (path string , handler Handler , middleware ... Handler ) Router {
794
+ func (app * App [TCtx ]) Head (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
795
795
return app .Add ([]string {MethodHead }, path , handler , middleware ... )
796
796
}
797
797
798
798
// Post registers a route for POST methods that is used to submit an entity to the
799
799
// specified resource, often causing a change in state or side effects on the server.
800
- func (app * App [TCtx ]) Post (path string , handler Handler , middleware ... Handler ) Router {
800
+ func (app * App [TCtx ]) Post (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
801
801
return app .Add ([]string {MethodPost }, path , handler , middleware ... )
802
802
}
803
803
804
804
// Put registers a route for PUT methods that replaces all current representations
805
805
// of the target resource with the request payload.
806
- func (app * App [TCtx ]) Put (path string , handler Handler , middleware ... Handler ) Router {
806
+ func (app * App [TCtx ]) Put (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
807
807
return app .Add ([]string {MethodPut }, path , handler , middleware ... )
808
808
}
809
809
810
810
// Delete registers a route for DELETE methods that deletes the specified resource.
811
- func (app * App [TCtx ]) Delete (path string , handler Handler , middleware ... Handler ) Router {
811
+ func (app * App [TCtx ]) Delete (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
812
812
return app .Add ([]string {MethodDelete }, path , handler , middleware ... )
813
813
}
814
814
815
815
// Connect registers a route for CONNECT methods that establishes a tunnel to the
816
816
// server identified by the target resource.
817
- func (app * App [TCtx ]) Connect (path string , handler Handler , middleware ... Handler ) Router {
817
+ func (app * App [TCtx ]) Connect (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
818
818
return app .Add ([]string {MethodConnect }, path , handler , middleware ... )
819
819
}
820
820
821
821
// Options registers a route for OPTIONS methods that is used to describe the
822
822
// communication options for the target resource.
823
- func (app * App [TCtx ]) Options (path string , handler Handler , middleware ... Handler ) Router {
823
+ func (app * App [TCtx ]) Options (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
824
824
return app .Add ([]string {MethodOptions }, path , handler , middleware ... )
825
825
}
826
826
827
827
// Trace registers a route for TRACE methods that performs a message loop-back
828
828
// test along the path to the target resource.
829
- func (app * App [TCtx ]) Trace (path string , handler Handler , middleware ... Handler ) Router {
829
+ func (app * App [TCtx ]) Trace (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
830
830
return app .Add ([]string {MethodTrace }, path , handler , middleware ... )
831
831
}
832
832
833
833
// Patch registers a route for PATCH methods that is used to apply partial
834
834
// modifications to a resource.
835
- func (app * App [TCtx ]) Patch (path string , handler Handler , middleware ... Handler ) Router {
835
+ func (app * App [TCtx ]) Patch (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
836
836
return app .Add ([]string {MethodPatch }, path , handler , middleware ... )
837
837
}
838
838
839
839
// Add allows you to specify multiple HTTP methods to register a route.
840
- func (app * App [TCtx ]) Add (methods []string , path string , handler Handler , middleware ... Handler ) Router {
840
+ func (app * App [TCtx ]) Add (methods []string , path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
841
841
app .register (methods , path , nil , handler , middleware ... )
842
842
843
843
return app
844
844
}
845
845
846
846
// All will register the handler on all HTTP methods
847
- func (app * App [TCtx ]) All (path string , handler Handler , middleware ... Handler ) Router {
847
+ func (app * App [TCtx ]) All (path string , handler Handler [ TCtx ] , middleware ... Handler [ TCtx ] ) Router [ TCtx ] {
848
848
return app .Add (app .config .RequestMethods , path , handler , middleware ... )
849
849
}
850
850
851
851
// Group is used for Routes with common prefix to define a new sub-router with optional middleware.
852
852
//
853
853
// api := app.Group("/api")
854
854
// api.Get("/users", handler)
855
- func (app * App [TCtx ]) Group (prefix string , handlers ... Handler ) Router {
856
- grp := & Group {Prefix : prefix , app : app }
855
+ func (app * App [TCtx ]) Group (prefix string , handlers ... Handler [ TCtx ] ) Router [ TCtx ] {
856
+ grp := & Group [ TCtx ] {Prefix : prefix , app : app }
857
857
if len (handlers ) > 0 {
858
858
app .register ([]string {methodUse }, prefix , grp , nil , handlers ... )
859
859
}
@@ -866,9 +866,9 @@ func (app *App[TCtx]) Group(prefix string, handlers ...Handler) Router {
866
866
867
867
// Route is used to define routes with a common prefix inside the common function.
868
868
// Uses Group method to define new sub-router.
869
- func (app * App [TCtx ]) Route (path string ) Register {
869
+ func (app * App [TCtx ]) Route (path string ) Register [ TCtx ] {
870
870
// Create new route
871
- route := & Registering {app : app , path : path }
871
+ route := & Registering [ TCtx ] {app : app , path : path }
872
872
873
873
return route
874
874
}
@@ -891,7 +891,7 @@ func NewError(code int, message ...string) *Error {
891
891
}
892
892
893
893
// Config returns the app config as value ( read-only ).
894
- func (app * App [TCtx ]) Config () Config {
894
+ func (app * App [TCtx ]) Config () Config [ TCtx ] {
895
895
return app .config
896
896
}
897
897
@@ -900,14 +900,11 @@ func (app *App[TCtx]) Handler() fasthttp.RequestHandler { //revive:disable-line:
900
900
// prepare the server for the start
901
901
app .startupProcess ()
902
902
903
- if app .newCtxFunc != nil {
904
- return app .customRequestHandler
905
- }
906
- return app .defaultRequestHandler
903
+ return app .requestHandler
907
904
}
908
905
909
906
// Stack returns the raw router stack.
910
- func (app * App [TCtx ]) Stack () [][]* Route {
907
+ func (app * App [TCtx ]) Stack () [][]* Route [ TCtx ] {
911
908
return app .stack
912
909
}
913
910
@@ -964,7 +961,7 @@ func (app *App[TCtx]) Server() *fasthttp.Server {
964
961
}
965
962
966
963
// Hooks returns the hook struct to register hooks.
967
- func (app * App [TCtx ]) Hooks () * Hooks {
964
+ func (app * App [TCtx ]) Hooks () * Hooks [ TCtx ] {
968
965
return app .hooks
969
966
}
970
967
@@ -1094,11 +1091,7 @@ func (app *App[TCtx]) init() *App[TCtx] {
1094
1091
}
1095
1092
1096
1093
// fasthttp server settings
1097
- if app .newCtxFunc != nil {
1098
- app .server .Handler = app .customRequestHandler
1099
- } else {
1100
- app .server .Handler = app .defaultRequestHandler
1101
- }
1094
+ app .server .Handler = app .requestHandler
1102
1095
app .server .Name = app .config .ServerHeader
1103
1096
app .server .Concurrency = app .config .Concurrency
1104
1097
app .server .NoDefaultDate = app .config .DisableDefaultDate
@@ -1127,9 +1120,9 @@ func (app *App[TCtx]) init() *App[TCtx] {
1127
1120
// sub fibers by their prefixes and if it finds a match, it uses that
1128
1121
// error handler. Otherwise it uses the configured error handler for
1129
1122
// the app, which if not set is the DefaultErrorHandler.
1130
- func (app * App [TCtx ]) ErrorHandler (ctx CtxGeneric [ TCtx ] , err error ) error {
1123
+ func (app * App [TCtx ]) ErrorHandler (ctx TCtx , err error ) error {
1131
1124
var (
1132
- mountedErrHandler ErrorHandler
1125
+ mountedErrHandler ErrorHandler [ TCtx ]
1133
1126
mountedPrefixParts int
1134
1127
)
1135
1128
0 commit comments