mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 03:27:02 -07:00
Updated server to use OpenCHAMI middleware
This commit is contained in:
parent
008fabaa78
commit
fbf39b15b5
1 changed files with 18 additions and 6 deletions
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
configurator "github.com/OpenCHAMI/configurator/pkg"
|
configurator "github.com/OpenCHAMI/configurator/pkg"
|
||||||
|
|
@ -14,7 +15,12 @@ import (
|
||||||
"github.com/OpenCHAMI/jwtauth/v5"
|
"github.com/OpenCHAMI/jwtauth/v5"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
openchami_authenticator "github.com/openchami/chi-middleware/auth"
|
||||||
|
openchami_logger "github.com/openchami/chi-middleware/log"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -77,6 +83,10 @@ func (s *Server) Serve() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup logger
|
||||||
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||||
|
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
|
||||||
// create new go-chi router with its routes
|
// create new go-chi router with its routes
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
router.Use(middleware.RequestID)
|
router.Use(middleware.RequestID)
|
||||||
|
|
@ -85,11 +95,12 @@ func (s *Server) Serve() error {
|
||||||
router.Use(middleware.Recoverer)
|
router.Use(middleware.Recoverer)
|
||||||
router.Use(middleware.StripSlashes)
|
router.Use(middleware.StripSlashes)
|
||||||
router.Use(middleware.Timeout(60 * time.Second))
|
router.Use(middleware.Timeout(60 * time.Second))
|
||||||
|
router.Use(openchami_logger.OpenCHAMILogger(logger))
|
||||||
if s.Config.Server.Jwks.Uri != "" {
|
if s.Config.Server.Jwks.Uri != "" {
|
||||||
router.Group(func(r chi.Router) {
|
router.Group(func(r chi.Router) {
|
||||||
r.Use(
|
r.Use(
|
||||||
jwtauth.Verifier(tokenAuth),
|
jwtauth.Verifier(tokenAuth),
|
||||||
jwtauth.Authenticator(tokenAuth),
|
openchami_authenticator.AuthenticatorWithRequiredClaims(tokenAuth, []string{"sub", "iss", "aud"}),
|
||||||
)
|
)
|
||||||
|
|
||||||
// protected routes if using auth
|
// protected routes if using auth
|
||||||
|
|
@ -108,6 +119,7 @@ func (s *Server) Serve() error {
|
||||||
return s.ListenAndServe()
|
return s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: implement a way to shut the server down
|
||||||
func (s *Server) Close() {
|
func (s *Server) Close() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -119,14 +131,14 @@ func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
||||||
// get all of the expect query URL params and validate
|
// get all of the expect query URL params and validate
|
||||||
s.GeneratorParams.Target = r.URL.Query().Get("target")
|
s.GeneratorParams.Target = r.URL.Query().Get("target")
|
||||||
if s.GeneratorParams.Target == "" {
|
if s.GeneratorParams.Target == "" {
|
||||||
writeErrorResponse(w, "no targets supplied")
|
writeErrorResponse(w, "must specify a target")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate a new config file from supplied params
|
// generate a new config file from supplied params
|
||||||
outputs, err := generator.GenerateWithTarget(s.Config, s.GeneratorParams)
|
outputs, err := generator.GenerateWithTarget(s.Config, s.GeneratorParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErrorResponse(w, "failed to generate config: %v", err)
|
writeErrorResponse(w, "failed to generate file: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,12 +146,12 @@ func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
||||||
tmp := generator.ConvertContentsToString(outputs)
|
tmp := generator.ConvertContentsToString(outputs)
|
||||||
b, err := json.Marshal(tmp)
|
b, err := json.Marshal(tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErrorResponse(w, "failed to marshal output: %v", err)
|
writeErrorResponse(w, "failed to marshal output: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = w.Write(b)
|
_, err = w.Write(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErrorResponse(w, "failed to write response: %v", err)
|
writeErrorResponse(w, "failed to write response: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +163,7 @@ func (s *Server) Generate(w http.ResponseWriter, r *http.Request) {
|
||||||
func (s *Server) ManageTemplates(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ManageTemplates(w http.ResponseWriter, r *http.Request) {
|
||||||
_, err := w.Write([]byte("this is not implemented yet"))
|
_, err := w.Write([]byte("this is not implemented yet"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErrorResponse(w, "failed to write response: %v", err)
|
writeErrorResponse(w, "failed to write response: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue