u
This commit is contained in:
1 parent
7c8e2906bc
commit
72a1a9184a
25 files changed
+744
-125
No files matched your search
@@ -0,0 +1,10 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"base-framework/internal/app/handle"
|
||||
"base-framework/pkg/router"
|
||||
)
|
||||
|
||||
func InitTest(r *router.Router) {
|
||||
r.GET("/test", handle.Test)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"base-framework/pkg/router"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Test(c *router.Context) {
|
||||
c.JSON(http.StatusOK, map[string]string{
|
||||
"user_id": c.Param("id"),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"base-framework/internal/app/api"
|
||||
"base-framework/pkg/middleware"
|
||||
"base-framework/pkg/router"
|
||||
)
|
||||
|
||||
func InitAppRouter() *router.Router {
|
||||
r := router.NewRouter()
|
||||
|
||||
r.Use(middleware.Recover())
|
||||
r.Use(middleware.Auth()).ExcludePaths("/login")
|
||||
r.Use(middleware.Logger())
|
||||
|
||||
initApi(r)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func initApi(r *router.Router) {
|
||||
api.InitTest(r)
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package app
|
||||
|
||||
func RunServer() {
|
||||
}
|
||||
Whitespace-only changes.
@@ -1,11 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// LoggingMiddleware logs incoming requests
|
||||
func LoggingMiddleware(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("Request received: %s %s\n", r.Method, r.URL.Path)
|
||||
}
|
||||
Whitespace-only changes.
@@ -1,98 +0,0 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HandlerFunc defines the signature of a handler function
|
||||
type HandlerFunc func(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// Router struct holds all the routes, middlewares, and groups
|
||||
type Router struct {
|
||||
routes map[string]map[string]HandlerFunc
|
||||
middleware []HandlerFunc
|
||||
groups map[string]*Group
|
||||
}
|
||||
|
||||
// NewRouter creates a new Router instance
|
||||
func NewRouter() *Router {
|
||||
return &Router{
|
||||
routes: make(map[string]map[string]HandlerFunc),
|
||||
groups: make(map[string]*Group),
|
||||
}
|
||||
}
|
||||
|
||||
// Use adds a global middleware to the router
|
||||
func (r *Router) Use(middleware HandlerFunc) {
|
||||
r.middleware = append(r.middleware, middleware)
|
||||
}
|
||||
|
||||
// Handle registers a route with a method and a handler
|
||||
func (r *Router) Handle(method, path string, handler HandlerFunc) {
|
||||
if _, exists := r.routes[method]; !exists {
|
||||
r.routes[method] = make(map[string]HandlerFunc)
|
||||
}
|
||||
r.routes[method][path] = handler
|
||||
}
|
||||
|
||||
// Group creates a new group with a prefix
|
||||
func (r *Router) Group(prefix string) *Group {
|
||||
group := &Group{
|
||||
router: r,
|
||||
prefix: prefix,
|
||||
}
|
||||
r.groups[prefix] = group
|
||||
return group
|
||||
}
|
||||
|
||||
// Group struct represents a group of routes with a common prefix
|
||||
type Group struct {
|
||||
router *Router
|
||||
prefix string
|
||||
middleware []HandlerFunc
|
||||
}
|
||||
|
||||
// Use adds a middleware to a group
|
||||
func (g *Group) Use(middleware HandlerFunc) {
|
||||
g.middleware = append(g.middleware, middleware)
|
||||
}
|
||||
|
||||
// Handle registers a route in the group
|
||||
func (g *Group) Handle(method, path string, handler HandlerFunc) {
|
||||
fullPath := g.prefix + path
|
||||
|
||||
// Apply group middleware first
|
||||
for _, m := range g.middleware {
|
||||
g.router.Use(m)
|
||||
}
|
||||
|
||||
// Register the route
|
||||
g.router.Handle(method, fullPath, handler)
|
||||
}
|
||||
|
||||
// Get registers a GET route in the group
|
||||
func (g *Group) Get(path string, handler HandlerFunc) {
|
||||
g.Handle("GET", path, handler)
|
||||
}
|
||||
|
||||
// Post registers a POST route in the group
|
||||
func (g *Group) Post(path string, handler HandlerFunc) {
|
||||
g.Handle("POST", path, handler)
|
||||
}
|
||||
|
||||
// ServeHTTP is the main entry point for handling HTTP requests
|
||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// Apply global middlewares
|
||||
for _, m := range r.middleware {
|
||||
m(w, req)
|
||||
}
|
||||
|
||||
// Route matching
|
||||
if handlers, ok := r.routes[req.Method]; ok {
|
||||
if handler, ok := handlers[req.URL.Path]; ok {
|
||||
handler(w, req)
|
||||
return
|
||||
}
|
||||
}
|
||||
http.NotFound(w, req)
|
||||
}
|
||||
Reference in new issue
Block a user