Browse Source

En cours de développement

master
mls-361 9 months ago
parent
commit
6e26f59758
  1. 21
      internal/api/api.go
  2. 24
      internal/api/middleware.go
  3. 6
      internal/components/components.go
  4. 20
      internal/router/router.go

21
internal/api/api.go

@ -17,7 +17,6 @@ import (
"forge.chapril.org/mls-361/uuid"
"forge.chapril.org/armen/armen/internal/components"
"forge.chapril.org/armen/armen/internal/middleware"
)
const _maxBodySize = 1024 * 32
@ -37,15 +36,15 @@ func newAPI(ccs *components.Components) *api {
}
func (api *api) debug(hf jsonapi.HandlerFunc) jsonapi.Handler {
return middleware.Debug(
middleware.Authentification(hf, api.crypto),
return mwDebug(
mwAuth(hf, api.crypto),
api.logger,
)
}
func (api *api) trace(hf jsonapi.HandlerFunc) jsonapi.Handler {
return middleware.Trace(
middleware.Authentification(hf, api.crypto),
return mwTrace(
mwAuth(hf, api.crypto),
api.logger,
)
}
@ -168,13 +167,13 @@ func Build(ccs *components.Components) {
api := newAPI(ccs)
router := ccs.Router
router.PostJSON("/api/job/create", api.debug(api.createJob))
router.GetJSON("/api/job/next/:namespace", api.trace(api.nextJob))
router.PutJSON("/api/job/update", api.debug(api.updateJob))
router.PostJSON("/api/workflow/create", api.debug(api.createWorkflow))
router.POST("/api/job/create", api.debug(api.createJob))
router.GET("/api/job/next/:namespace", api.trace(api.nextJob))
router.PUT("/api/job/update", api.debug(api.updateJob))
router.POST("/api/workflow/create", api.debug(api.createWorkflow))
router.GetJSON("/api/admin/logger/list", api.debug(api.adminListLogger))
router.PutJSON("/api/admin/logger/:logger/setlevel/:level", api.debug(api.adminSetLoggerLevel))
router.GET("/api/admin/logger/list", api.debug(api.adminListLogger))
router.PUT("/api/admin/logger/:logger/setlevel/:level", api.debug(api.adminSetLoggerLevel))
}
/*

24
internal/middleware/middleware.go → internal/api/middleware.go

@ -1,13 +1,14 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### middleware ####### Copyright (c) 2021 mls-361 ############################################## MIT License #######
####### api ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package middleware
package api
import (
"net/http"
"strings"
"forge.chapril.org/mls-361/jsonapi"
@ -18,13 +19,12 @@ func setHeaderWWWAuthenticate(r *jsonapi.Request) {
r.SetHeader("WWW-Authenticate", `Basic realm="Give username and password"`)
}
// Authentification AFAIRE.
func Authentification(next jsonapi.Handler, crypto components.Crypto) jsonapi.Handler {
func mwAuth(next jsonapi.Handler, crypto components.Crypto) jsonapi.Handler {
return jsonapi.HandlerFunc(func(r *jsonapi.Request) {
username, password, ok := r.BasicAuth()
if !ok {
setHeaderWWWAuthenticate(r)
r.RenderError(http.StatusUnauthorized, "No basic auth present")
r.RenderError(http.StatusUnauthorized, "no basic auth present")
return
}
@ -32,7 +32,13 @@ func Authentification(next jsonapi.Handler, crypto components.Crypto) jsonapi.Ha
if err != nil || value != username {
setHeaderWWWAuthenticate(r)
r.RenderError(http.StatusUnauthorized, "Invalid username or password")
r.RenderError(http.StatusUnauthorized, "invalid username or password")
return
}
if strings.HasPrefix(r.URL.RequestURI(), "/api/admin/") && username != "cli" {
status := http.StatusUnauthorized
r.RenderError(status, http.StatusText(status))
return
}
@ -40,8 +46,7 @@ func Authentification(next jsonapi.Handler, crypto components.Crypto) jsonapi.Ha
})
}
// Debug AFAIRE.
func Debug(next jsonapi.Handler, logger components.Logger) jsonapi.Handler {
func mwDebug(next jsonapi.Handler, logger components.Logger) jsonapi.Handler {
return jsonapi.HandlerFunc(func(r *jsonapi.Request) {
logger.Debug( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Request",
@ -61,8 +66,7 @@ func Debug(next jsonapi.Handler, logger components.Logger) jsonapi.Handler {
})
}
// Trace AFAIRE.
func Trace(next jsonapi.Handler, logger components.Logger) jsonapi.Handler {
func mwTrace(next jsonapi.Handler, logger components.Logger) jsonapi.Handler {
return jsonapi.HandlerFunc(func(r *jsonapi.Request) {
logger.Trace( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Request",

6
internal/components/components.go

@ -58,9 +58,9 @@ type Router interface {
Handler() http.Handler
Get(path string, handler http.Handler)
ServeFiles(path string, root http.FileSystem)
GetJSON(path string, handler jsonapi.Handler)
PostJSON(path string, handler jsonapi.Handler)
PutJSON(path string, handler jsonapi.Handler)
GET(path string, handler jsonapi.Handler)
POST(path string, handler jsonapi.Handler)
PUT(path string, handler jsonapi.Handler)
}
// Server AFAIRE.

20
internal/router/router.go

@ -69,7 +69,7 @@ func (cr *router) ServeFiles(path string, root http.FileSystem) {
cr.mux.ServeFiles(path, root)
}
func (cr *router) handleJSON(method string, path string, handler jsonapi.Handler) {
func (cr *router) handleJAH(method string, path string, handler jsonapi.Handler) {
cr.mux.Handle(
method,
path,
@ -89,19 +89,19 @@ func (cr *router) handleJSON(method string, path string, handler jsonapi.Handler
)
}
// GetJSON AFAIRE.
func (cr *router) GetJSON(path string, handler jsonapi.Handler) {
cr.handleJSON(http.MethodGet, path, handler)
// GET AFAIRE.
func (cr *router) GET(path string, handler jsonapi.Handler) {
cr.handleJAH(http.MethodGet, path, handler)
}
// PostJSON AFAIRE.
func (cr *router) PostJSON(path string, handler jsonapi.Handler) {
cr.handleJSON(http.MethodPost, path, handler)
// POST AFAIRE.
func (cr *router) POST(path string, handler jsonapi.Handler) {
cr.handleJAH(http.MethodPost, path, handler)
}
// PutJSON AFAIRE.
func (cr *router) PutJSON(path string, handler jsonapi.Handler) {
cr.handleJSON(http.MethodPut, path, handler)
// PUT AFAIRE.
func (cr *router) PUT(path string, handler jsonapi.Handler) {
cr.handleJAH(http.MethodPut, path, handler)
}
/*

Loading…
Cancel
Save