forked from goffee/core
add documentation
This commit is contained in:
parent
1b23363f6f
commit
695f1f57ba
8 changed files with 116 additions and 14 deletions
13
router.go
13
router.go
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
package core
|
||||
|
||||
// Route defines an HTTP route with a method, path, controller function, and optional hooks for customization.
|
||||
type Route struct {
|
||||
Method string
|
||||
Path string
|
||||
|
|
@ -12,12 +13,15 @@ type Route struct {
|
|||
Hooks []Hook
|
||||
}
|
||||
|
||||
// Router represents a structure for storing and managing routes in the application.
|
||||
type Router struct {
|
||||
Routes []Route
|
||||
}
|
||||
|
||||
// router is a global instance of the Router struct used to manage and resolve application routes.
|
||||
var router *Router
|
||||
|
||||
// NewRouter initializes and returns a new Router instance with an empty slice of routes.
|
||||
func NewRouter() *Router {
|
||||
router = &Router{
|
||||
[]Route{},
|
||||
|
|
@ -25,10 +29,12 @@ func NewRouter() *Router {
|
|||
return router
|
||||
}
|
||||
|
||||
// ResolveRouter returns the singleton instance of the Router. It initializes and manages HTTP routes configuration.
|
||||
func ResolveRouter() *Router {
|
||||
return router
|
||||
}
|
||||
|
||||
// Get registers a GET route with the specified path, controller, and optional hooks, returning the updated Router.
|
||||
func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router {
|
||||
r.Routes = append(r.Routes, Route{
|
||||
Method: GET,
|
||||
|
|
@ -39,6 +45,7 @@ func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router
|
|||
return r
|
||||
}
|
||||
|
||||
// Post registers a route with the HTTP POST method, associating it with the given path, controller, and optional hooks.
|
||||
func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router {
|
||||
r.Routes = append(r.Routes, Route{
|
||||
Method: POST,
|
||||
|
|
@ -49,6 +56,7 @@ func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router
|
|||
return r
|
||||
}
|
||||
|
||||
// Delete registers a DELETE HTTP method route with the specified path, controller, and optional hooks.
|
||||
func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router {
|
||||
r.Routes = append(r.Routes, Route{
|
||||
Method: DELETE,
|
||||
|
|
@ -59,6 +67,7 @@ func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Rout
|
|||
return r
|
||||
}
|
||||
|
||||
// Patch registers a new route with the HTTP PATCH method, a specified path, a controller, and optional hooks.
|
||||
func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router {
|
||||
r.Routes = append(r.Routes, Route{
|
||||
Method: PATCH,
|
||||
|
|
@ -69,6 +78,7 @@ func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Route
|
|||
return r
|
||||
}
|
||||
|
||||
// Put registers a new route with the HTTP PUT method, associating it to the given path, controller, and optional hooks.
|
||||
func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router {
|
||||
r.Routes = append(r.Routes, Route{
|
||||
Method: PUT,
|
||||
|
|
@ -79,6 +89,7 @@ func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router
|
|||
return r
|
||||
}
|
||||
|
||||
// Options registers a new route with the OPTIONS HTTP method, a specified path, controller, and optional hooks.
|
||||
func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router {
|
||||
r.Routes = append(r.Routes, Route{
|
||||
Method: OPTIONS,
|
||||
|
|
@ -89,6 +100,7 @@ func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Rou
|
|||
return r
|
||||
}
|
||||
|
||||
// Head registers a route with the HTTP HEAD method, associates it with a path, controller, and optional hooks.
|
||||
func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router {
|
||||
r.Routes = append(r.Routes, Route{
|
||||
Method: HEAD,
|
||||
|
|
@ -99,6 +111,7 @@ func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router
|
|||
return r
|
||||
}
|
||||
|
||||
// GetRoutes returns a slice of Route objects currently registered within the Router.
|
||||
func (r *Router) GetRoutes() []Route {
|
||||
return r.Routes
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue