104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
// 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
|
|
Controller Controller
|
|
Middlewares []Middleware
|
|
}
|
|
|
|
type Router struct {
|
|
Routes []Route
|
|
}
|
|
|
|
var router *Router
|
|
|
|
func NewRouter() *Router {
|
|
router = &Router{
|
|
[]Route{},
|
|
}
|
|
return router
|
|
}
|
|
|
|
func ResolveRouter() *Router {
|
|
return router
|
|
}
|
|
|
|
func (r *Router) Get(path string, controller Controller, middlewares ...Middleware) *Router {
|
|
r.Routes = append(r.Routes, Route{
|
|
Method: GET,
|
|
Path: path,
|
|
Controller: controller,
|
|
Middlewares: middlewares,
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *Router) Post(path string, controller Controller, middlewares ...Middleware) *Router {
|
|
r.Routes = append(r.Routes, Route{
|
|
Method: POST,
|
|
Path: path,
|
|
Controller: controller,
|
|
Middlewares: middlewares,
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *Router) Delete(path string, controller Controller, middlewares ...Middleware) *Router {
|
|
r.Routes = append(r.Routes, Route{
|
|
Method: DELETE,
|
|
Path: path,
|
|
Controller: controller,
|
|
Middlewares: middlewares,
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *Router) Patch(path string, controller Controller, middlewares ...Middleware) *Router {
|
|
r.Routes = append(r.Routes, Route{
|
|
Method: PATCH,
|
|
Path: path,
|
|
Controller: controller,
|
|
Middlewares: middlewares,
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *Router) Put(path string, controller Controller, middlewares ...Middleware) *Router {
|
|
r.Routes = append(r.Routes, Route{
|
|
Method: PUT,
|
|
Path: path,
|
|
Controller: controller,
|
|
Middlewares: middlewares,
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *Router) Options(path string, controller Controller, middlewares ...Middleware) *Router {
|
|
r.Routes = append(r.Routes, Route{
|
|
Method: OPTIONS,
|
|
Path: path,
|
|
Controller: controller,
|
|
Middlewares: middlewares,
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *Router) Head(path string, controller Controller, middlewares ...Middleware) *Router {
|
|
r.Routes = append(r.Routes, Route{
|
|
Method: HEAD,
|
|
Path: path,
|
|
Controller: controller,
|
|
Middlewares: middlewares,
|
|
})
|
|
return r
|
|
}
|
|
|
|
func (r *Router) GetRoutes() []Route {
|
|
return r.Routes
|
|
}
|