1
0
Fork 0
forked from goffee/core
core/router.go

105 lines
2.3 KiB
Go
Raw Normal View History

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
type Route struct {
Method string
Path string
2024-09-15 20:00:38 -04:00
Controller Controller
2024-09-12 18:13:16 -04:00
Middlewares []Middleware
}
type Router struct {
Routes []Route
}
var router *Router
func NewRouter() *Router {
router = &Router{
[]Route{},
}
return router
}
func ResolveRouter() *Router {
return router
}
2024-09-15 20:00:38 -04:00
func (r *Router) Get(path string, controller Controller, middlewares ...Middleware) *Router {
2024-09-12 18:13:16 -04:00
r.Routes = append(r.Routes, Route{
Method: GET,
Path: path,
2024-09-15 20:00:38 -04:00
Controller: controller,
2024-09-12 18:13:16 -04:00
Middlewares: middlewares,
})
return r
}
2024-09-15 20:00:38 -04:00
func (r *Router) Post(path string, controller Controller, middlewares ...Middleware) *Router {
2024-09-12 18:13:16 -04:00
r.Routes = append(r.Routes, Route{
Method: POST,
Path: path,
2024-09-15 20:00:38 -04:00
Controller: controller,
2024-09-12 18:13:16 -04:00
Middlewares: middlewares,
})
return r
}
2024-09-15 20:00:38 -04:00
func (r *Router) Delete(path string, controller Controller, middlewares ...Middleware) *Router {
2024-09-12 18:13:16 -04:00
r.Routes = append(r.Routes, Route{
Method: DELETE,
Path: path,
2024-09-15 20:00:38 -04:00
Controller: controller,
2024-09-12 18:13:16 -04:00
Middlewares: middlewares,
})
return r
}
2024-09-15 20:00:38 -04:00
func (r *Router) Patch(path string, controller Controller, middlewares ...Middleware) *Router {
2024-09-12 18:13:16 -04:00
r.Routes = append(r.Routes, Route{
Method: PATCH,
Path: path,
2024-09-15 20:00:38 -04:00
Controller: controller,
2024-09-12 18:13:16 -04:00
Middlewares: middlewares,
})
return r
}
2024-09-15 20:00:38 -04:00
func (r *Router) Put(path string, controller Controller, middlewares ...Middleware) *Router {
2024-09-12 18:13:16 -04:00
r.Routes = append(r.Routes, Route{
Method: PUT,
Path: path,
2024-09-15 20:00:38 -04:00
Controller: controller,
2024-09-12 18:13:16 -04:00
Middlewares: middlewares,
})
return r
}
2024-09-15 20:00:38 -04:00
func (r *Router) Options(path string, controller Controller, middlewares ...Middleware) *Router {
2024-09-12 18:13:16 -04:00
r.Routes = append(r.Routes, Route{
Method: OPTIONS,
Path: path,
2024-09-15 20:00:38 -04:00
Controller: controller,
2024-09-12 18:13:16 -04:00
Middlewares: middlewares,
})
return r
}
2024-09-15 20:00:38 -04:00
func (r *Router) Head(path string, controller Controller, middlewares ...Middleware) *Router {
2024-09-12 18:13:16 -04:00
r.Routes = append(r.Routes, Route{
Method: HEAD,
Path: path,
2024-09-15 20:00:38 -04:00
Controller: controller,
2024-09-12 18:13:16 -04:00
Middlewares: middlewares,
})
return r
}
func (r *Router) GetRoutes() []Route {
return r.Routes
}