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.
70 lines
1.8 KiB
70 lines
1.8 KiB
/* |
|
------------------------------------------------------------------------------------------------------------------------ |
|
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License ####### |
|
------------------------------------------------------------------------------------------------------------------------ |
|
*/ |
|
|
|
package api |
|
|
|
import ( |
|
"net/http" |
|
|
|
"forge.chapril.org/losyme/kong/context" |
|
"forge.chapril.org/losyme/kong/middleware" |
|
"forge.chapril.org/losyme/kong/middleware/json" |
|
"forge.chapril.org/losyme/kong/router" |
|
) |
|
|
|
type API struct { |
|
*Config |
|
} |
|
|
|
func New(cfg *Config) (*API, error) { |
|
if err := cfg.validate(); err != nil { |
|
return nil, err |
|
} |
|
|
|
api := &API{cfg} |
|
|
|
api.setup() |
|
|
|
return api, nil |
|
} |
|
|
|
func (api *API) setup() { |
|
api.Router.MethodNotAllowed = json.MethodNotAllowed(api.Logger) |
|
api.Router.NotFound = json.NotFound(api.Logger) |
|
api.Router.OnRecover = json.OnRecover(api.Logger) |
|
|
|
api.Router.Use( |
|
middleware.RequestID(), |
|
middleware.Logger(api.Logger), |
|
json.OnError(api.Logger), |
|
) |
|
|
|
api.Router.Group("/api", func(g *router.Group) { // api............................................................. |
|
g.Get("/status", api.status) |
|
g.Get("/dashboard", api.dashboard) |
|
|
|
g.New("/jobs", func(g2 *router.Group) { // api/jobs.............................................................. |
|
g2.Post("", api.createJob) |
|
}) |
|
}) |
|
} |
|
|
|
func (api *API) status(c *context.Context) error { |
|
return c.JSON(http.StatusNoContent, nil) |
|
} |
|
|
|
func (api *API) dashboard(c *context.Context) error { |
|
stats, err := api.Model.Storage.Dashboard() |
|
if err != nil { |
|
return err |
|
} |
|
|
|
return c.JSON(http.StatusOK, stats) |
|
} |
|
|
|
/* |
|
######################################################################################################## @(°_°)@ ####### |
|
*/
|
|
|