core/router.go

105 lines
2.1 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 {
2024-09-15 20:19:54 -04:00
Method string
Path string
Controller Controller
Hooks []Hook
2024-09-12 18:13:16 -04:00
}
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: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
}
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
}
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
}
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
}
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
}
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
}
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
}
func (r *Router) GetRoutes() []Route {
return r.Routes
}