Jobs & Workflows
https://armen.surge.sh
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
/* |
|
------------------------------------------------------------------------------------------------------------------------ |
|
####### router ####### Copyright (c) 2021 mls-361 ################################################## MIT License ####### |
|
------------------------------------------------------------------------------------------------------------------------ |
|
*/ |
|
|
|
package router |
|
|
|
import ( |
|
"net/http" |
|
|
|
"forge.chapril.org/mls-361/jsonapi" |
|
"github.com/julienschmidt/httprouter" |
|
|
|
"forge.chapril.org/armen/armen/internal/components" |
|
) |
|
|
|
type router struct { |
|
mux *httprouter.Router |
|
} |
|
|
|
func newRouter() *router { |
|
return &router{ |
|
mux: httprouter.New(), |
|
} |
|
} |
|
|
|
// Build AFAIRE. |
|
func Build(_ *components.Components) components.Router { |
|
cr := newRouter() |
|
|
|
cr.mux.PanicHandler = func(rw http.ResponseWriter, _ *http.Request, _ interface{}) { |
|
//AFINIR: logger |
|
rw.WriteHeader(http.StatusInternalServerError) |
|
} |
|
|
|
return cr |
|
} |
|
|
|
// Handler AFAIRE. |
|
func (cr *router) Handler() http.Handler { |
|
return cr.mux |
|
} |
|
|
|
func (cr *router) handle(method string, path string, handler jsonapi.Handler) { |
|
cr.mux.Handle( |
|
method, |
|
path, |
|
func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
|
handler.Serve(jsonapi.NewRequest(r, w)) |
|
}, |
|
) |
|
} |
|
|
|
// Get AFAIRE. |
|
func (cr *router) Get(path string, handler jsonapi.Handler) { |
|
cr.handle(http.MethodGet, path, handler) |
|
} |
|
|
|
// Post AFAIRE. |
|
func (cr *router) Post(path string, handler jsonapi.Handler) { |
|
cr.handle(http.MethodPost, path, handler) |
|
} |
|
|
|
/* |
|
######################################################################################################## @(°_°)@ ####### |
|
*/
|
|
|