Browse Source

En cours de développement

master
mls-361 1 month ago
parent
commit
2cd28fe101
  1. 2
      go.mod
  2. 4
      go.sum
  3. 29
      internal/api/api.go
  4. 5
      internal/components/components.go
  5. 35
      internal/middleware/trace.go
  6. 19
      internal/router/router.go
  7. 5
      internal/server/server.go

2
go.mod

@ -11,7 +11,7 @@ require (
forge.chapril.org/mls-361/application v0.0.0-20210505210056-fb6d8a49172f
forge.chapril.org/mls-361/crypto v0.0.0-20210505210121-8b90bff2c6d0
forge.chapril.org/mls-361/errors v0.0.0-20210505205959-6bf0da3026e8
forge.chapril.org/mls-361/jsonapi v0.0.0-20210506164608-b4dc891f8da5
forge.chapril.org/mls-361/jsonapi v0.0.0-20210507160901-9f2b144536a3
forge.chapril.org/mls-361/logger v0.0.0-20210505210208-50f76d793833
forge.chapril.org/mls-361/uuid v0.0.0-20210411123640-49d47af267ff
github.com/julienschmidt/httprouter v1.3.0

4
go.sum

@ -20,8 +20,8 @@ forge.chapril.org/mls-361/errors v0.0.0-20210505205959-6bf0da3026e8 h1:AUS2mIV1t
forge.chapril.org/mls-361/errors v0.0.0-20210505205959-6bf0da3026e8/go.mod h1:lp680bNqiwsJyXCCeOoW2jyoq/x7jv4HAgurHqNiVhw=
forge.chapril.org/mls-361/fqdn v0.0.0-20210505210026-544124883654 h1:VZpYvM5/PdtNQgmguSxtoD9sMlRI+/ndvyPNCSScTsA=
forge.chapril.org/mls-361/fqdn v0.0.0-20210505210026-544124883654/go.mod h1:WwEQJZi7shs5QbHJU/qcOWtv8xmkuEoIgX5nKypwXSg=
forge.chapril.org/mls-361/jsonapi v0.0.0-20210506164608-b4dc891f8da5 h1:twdFRv8G7lxT4WCqprfHtnDGpBuccIi5jKikQbjQEzY=
forge.chapril.org/mls-361/jsonapi v0.0.0-20210506164608-b4dc891f8da5/go.mod h1:Gehbj8OeDiaN+zoNoWyfLneb8zxKhBH6dAz41q43Qaw=
forge.chapril.org/mls-361/jsonapi v0.0.0-20210507160901-9f2b144536a3 h1:HS8Cy+0w+eqqIl6dNIpGF8N6pquhbVbPx+5IuGxwQ7Y=
forge.chapril.org/mls-361/jsonapi v0.0.0-20210507160901-9f2b144536a3/go.mod h1:Gehbj8OeDiaN+zoNoWyfLneb8zxKhBH6dAz41q43Qaw=
forge.chapril.org/mls-361/kvfmt v0.0.0-20210505202529-c3d4b541e862 h1:QnY1J/F2CZpk0sOzjxx0ZA1dXoFyTpW4fNuiGQoyZ5s=
forge.chapril.org/mls-361/kvfmt v0.0.0-20210505202529-c3d4b541e862/go.mod h1:bv44R0CAd8lQV4ub1hjLE3kWYwpfsW4Ro9zRnD4YOMU=
forge.chapril.org/mls-361/logger v0.0.0-20210505210208-50f76d793833 h1:DQot7mdUNb43d7yXxEUp8R+rDXLPFb3Wf1O00L7Er3s=

29
internal/api/api.go

@ -8,14 +8,15 @@ package api
import (
"errors"
"expvar"
"net/http"
"forge.chapril.org/armen/jw"
"forge.chapril.org/armen/model"
"forge.chapril.org/mls-361/jsonapi"
"forge.chapril.org/mls-361/uuid"
"forge.chapril.org/armen/armen/internal/components"
"forge.chapril.org/armen/armen/internal/middleware"
)
const (
@ -29,47 +30,51 @@ type api struct {
func newAPI(ccs *components.Components) *api {
return &api{
logger: ccs.Logger,
logger: ccs.Logger.NewLogger(uuid.New(), "api"),
model: ccs.Model,
}
}
func (api *api) createJob(rw http.ResponseWriter, r *http.Request) {
func (api *api) createJob(r *jsonapi.Request) {
job := new(jw.Job)
if jsonapi.Decode(rw, r, _maxBodySize, job) != nil {
if r.Decode(_maxBodySize, job) != nil {
return
}
job, err := api.model.CreateJob(job)
if err != nil {
if errors.Is(err, model.ErrValidation) {
jsonapi.BadRequest(rw, err)
r.BadRequest(err)
} else {
jsonapi.InternalServerError(rw, err)
r.InternalServerError(err)
}
return
}
if job == nil {
rw.WriteHeader(http.StatusOK)
r.RenderOK()
return
}
jsonapi.Render(rw, r, http.StatusCreated, job, api.logger)
r.RenderJSON(http.StatusCreated, job, api.logger)
}
func (api *api) middleware(hf jsonapi.HandlerFunc) jsonapi.Handler {
return middleware.Trace(hf, api.logger)
}
func Build(ccs *components.Components) {
api := newAPI(ccs)
router := ccs.Router
router.Get("/debug", expvar.Handler())
router.Get("/status", http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusOK)
//router.Get("/debug", expvar.Handler()) //AFINIR
router.Get("/status", api.middleware(func(r *jsonapi.Request) {
r.RenderOK()
}))
router.Post("/job/create", http.HandlerFunc(api.createJob))
router.Post("/job/create", api.middleware(api.createJob))
}
/*

5
internal/components/components.go

@ -10,6 +10,7 @@ import (
"net/http"
"forge.chapril.org/armen/jw"
"forge.chapril.org/mls-361/jsonapi"
"forge.chapril.org/mls-361/logger"
"forge.chapril.org/mls-361/uuid"
@ -49,8 +50,8 @@ type Model interface {
// Router AFAIRE.
type Router interface {
Handler() http.Handler
Get(path string, handler http.Handler)
Post(path string, handler http.Handler)
Get(path string, handler jsonapi.Handler)
Post(path string, handler jsonapi.Handler)
}
// Server AFAIRE.

35
internal/middleware/trace.go

@ -7,51 +7,28 @@
package middleware
import (
"net/http"
"forge.chapril.org/mls-361/jsonapi"
"forge.chapril.org/mls-361/uuid"
"forge.chapril.org/armen/armen/internal/components"
)
type responseWriter struct {
http.ResponseWriter
status int
}
func newResponseWriter(w http.ResponseWriter) *responseWriter {
return &responseWriter{
ResponseWriter: w,
status: 200,
}
}
func (rw *responseWriter) WriteHeader(status int) {
rw.status = status
rw.ResponseWriter.WriteHeader(status)
}
// Trace AFAIRE.
func Trace(next http.Handler, logger components.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := uuid.New()
rw := newResponseWriter(w)
func Trace(next jsonapi.Handler, logger components.Logger) jsonapi.Handler {
return jsonapi.HandlerFunc(func(r *jsonapi.Request) {
logger.Trace( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Request",
"id", id,
"id", r.ID(),
"from", r.RemoteAddr,
"method", r.Method,
"uri", r.URL.RequestURI(),
)
next.ServeHTTP(rw, jsonapi.NewRequestWithID(r, id))
next.Serve(r)
logger.Trace( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Response",
"id", id,
"status", rw.status,
"id", r.ID(),
"status", r.Status(),
)
})
}

19
internal/router/router.go

@ -9,6 +9,7 @@ package router
import (
"net/http"
"forge.chapril.org/mls-361/jsonapi"
"github.com/julienschmidt/httprouter"
"forge.chapril.org/armen/armen/internal/components"
@ -41,14 +42,24 @@ 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 http.Handler) {
cr.mux.Handler(http.MethodGet, path, handler)
func (cr *router) Get(path string, handler jsonapi.Handler) {
cr.handle(http.MethodGet, path, handler)
}
// Post AFAIRE.
func (cr *router) Post(path string, handler http.Handler) {
cr.mux.Handler(http.MethodPost, path, handler)
func (cr *router) Post(path string, handler jsonapi.Handler) {
cr.handle(http.MethodPost, path, handler)
}
/*

5
internal/server/server.go

@ -16,7 +16,6 @@ import (
"forge.chapril.org/mls-361/uuid"
"forge.chapril.org/armen/armen/internal/components"
"forge.chapril.org/armen/armen/internal/middleware"
)
type server struct {
@ -34,11 +33,9 @@ func Build(ccs *components.Components) (components.Server, error) {
cs := newServer(ccs.Logger.NewLogger(uuid.New(), "server"))
cfg := ccs.Config.Server()
handler := middleware.Trace(ccs.Router.Handler(), ccs.Logger)
cs.server = &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: handler,
Handler: ccs.Router.Handler(),
ErrorLog: cs.logger.NewStdLogger("error", "", log.Llongfile),
IdleTimeout: 1 * time.Minute,
ReadTimeout: 5 * time.Second,

Loading…
Cancel
Save