2024-09-12 18:13:16 -04:00
|
|
|
// Copyright 2021 Harran Ali <harran.m@gmail.com>. All rights reserved.
|
|
|
|
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
|
|
|
// Use of this source code is governed by MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Route defines an HTTP route with a method, path, controller function, and optional hooks for customization.
|
2024-09-12 18:13:16 -04:00
|
|
|
type Route struct {
|
2024-09-15 20:19:54 -04:00
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Controller Controller
|
|
|
|
Hooks []Hook
|
2024-09-12 18:13:16 -04:00
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Router represents a structure for storing and managing routes in the application.
|
2024-09-12 18:13:16 -04:00
|
|
|
type Router struct {
|
|
|
|
Routes []Route
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// router is a global instance of the Router struct used to manage and resolve application routes.
|
2024-09-12 18:13:16 -04:00
|
|
|
var router *Router
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// NewRouter initializes and returns a new Router instance with an empty slice of routes.
|
2024-09-12 18:13:16 -04:00
|
|
|
func NewRouter() *Router {
|
|
|
|
router = &Router{
|
|
|
|
[]Route{},
|
|
|
|
}
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// ResolveRouter returns the singleton instance of the Router. It initializes and manages HTTP routes configuration.
|
2024-09-12 18:13:16 -04:00
|
|
|
func ResolveRouter() *Router {
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Get registers a GET route with the specified path, controller, and optional hooks, returning the updated Router.
|
2024-09-15 20:19:54 -04:00
|
|
|
func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router {
|
2024-09-12 18:13:16 -04:00
|
|
|
r.Routes = append(r.Routes, Route{
|
2024-09-15 20:19:54 -04:00
|
|
|
Method: GET,
|
|
|
|
Path: path,
|
|
|
|
Controller: controller,
|
|
|
|
Hooks: hooks,
|
2024-09-12 18:13:16 -04:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Post registers a route with the HTTP POST method, associating it with the given path, controller, and optional hooks.
|
2024-09-15 20:19:54 -04:00
|
|
|
func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router {
|
2024-09-12 18:13:16 -04:00
|
|
|
r.Routes = append(r.Routes, Route{
|
2024-09-15 20:19:54 -04:00
|
|
|
Method: POST,
|
|
|
|
Path: path,
|
|
|
|
Controller: controller,
|
|
|
|
Hooks: hooks,
|
2024-09-12 18:13:16 -04:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Delete registers a DELETE HTTP method route with the specified path, controller, and optional hooks.
|
2024-09-15 20:19:54 -04:00
|
|
|
func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router {
|
2024-09-12 18:13:16 -04:00
|
|
|
r.Routes = append(r.Routes, Route{
|
2024-09-15 20:19:54 -04:00
|
|
|
Method: DELETE,
|
|
|
|
Path: path,
|
|
|
|
Controller: controller,
|
|
|
|
Hooks: hooks,
|
2024-09-12 18:13:16 -04:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Patch registers a new route with the HTTP PATCH method, a specified path, a controller, and optional hooks.
|
2024-09-15 20:19:54 -04:00
|
|
|
func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router {
|
2024-09-12 18:13:16 -04:00
|
|
|
r.Routes = append(r.Routes, Route{
|
2024-09-15 20:19:54 -04:00
|
|
|
Method: PATCH,
|
|
|
|
Path: path,
|
|
|
|
Controller: controller,
|
|
|
|
Hooks: hooks,
|
2024-09-12 18:13:16 -04:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Put registers a new route with the HTTP PUT method, associating it to the given path, controller, and optional hooks.
|
2024-09-15 20:19:54 -04:00
|
|
|
func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router {
|
2024-09-12 18:13:16 -04:00
|
|
|
r.Routes = append(r.Routes, Route{
|
2024-09-15 20:19:54 -04:00
|
|
|
Method: PUT,
|
|
|
|
Path: path,
|
|
|
|
Controller: controller,
|
|
|
|
Hooks: hooks,
|
2024-09-12 18:13:16 -04:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Options registers a new route with the OPTIONS HTTP method, a specified path, controller, and optional hooks.
|
2024-09-15 20:19:54 -04:00
|
|
|
func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router {
|
2024-09-12 18:13:16 -04:00
|
|
|
r.Routes = append(r.Routes, Route{
|
2024-09-15 20:19:54 -04:00
|
|
|
Method: OPTIONS,
|
|
|
|
Path: path,
|
|
|
|
Controller: controller,
|
|
|
|
Hooks: hooks,
|
2024-09-12 18:13:16 -04:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// Head registers a route with the HTTP HEAD method, associates it with a path, controller, and optional hooks.
|
2024-09-15 20:19:54 -04:00
|
|
|
func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router {
|
2024-09-12 18:13:16 -04:00
|
|
|
r.Routes = append(r.Routes, Route{
|
2024-09-15 20:19:54 -04:00
|
|
|
Method: HEAD,
|
|
|
|
Path: path,
|
|
|
|
Controller: controller,
|
|
|
|
Hooks: hooks,
|
2024-09-12 18:13:16 -04:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2025-02-24 09:59:07 -05:00
|
|
|
// GetRoutes returns a slice of Route objects currently registered within the Router.
|
2024-09-12 18:13:16 -04:00
|
|
|
func (r *Router) GetRoutes() []Route {
|
|
|
|
return r.Routes
|
|
|
|
}
|