latest
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
2023-01-28 19:05:02 -08:00
parent 3f49d528e7
commit 49709ad3d2
18 changed files with 224 additions and 180 deletions

View File

@@ -1,12 +0,0 @@
package handlers
import (
"fmt"
"net/http"
)
func (s *Handlers) HandleLogin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello login")
}
}

View File

@@ -1,17 +1,16 @@
package handlers
import (
"html/template"
"net/http"
"git.eeqj.de/sneak/gohttpserver/templates"
)
func (s *Handlers) HandleIndex() http.HandlerFunc {
indexTemplate := template.Must(template.New("index").Parse(templates.MustString("index.html")))
t := templates.GetParsed()
return func(w http.ResponseWriter, r *http.Request) {
err := indexTemplate.ExecuteTemplate(w, "index", nil)
err := t.ExecuteTemplate(w, "index.html", nil)
if err != nil {
s.log.Error().Err(err).Msg("")
http.Error(w, http.StatusText(500), 500)

View File

@@ -0,0 +1,19 @@
package handlers
import (
"net/http"
"git.eeqj.de/sneak/gohttpserver/templates"
)
func (s *Handlers) HandleLoginGET() http.HandlerFunc {
t := templates.GetParsed()
return func(w http.ResponseWriter, r *http.Request) {
err := t.ExecuteTemplate(w, "login.html", nil)
if err != nil {
s.log.Error().Err(err).Msg("")
http.Error(w, http.StatusText(500), 500)
}
}
}

View File

@@ -69,7 +69,7 @@ func (lrw *loggingResponseWriter) WriteHeader(code int) {
// type Middleware func(http.Handler) http.Handler
// this returns a Middleware that is designed to do every request through the
// mux, note the signature:
func (s *Middleware) LoggingMiddleware() func(http.Handler) http.Handler {
func (s *Middleware) Logging() func(http.Handler) http.Handler {
// FIXME this should use https://github.com/google/go-cloud/blob/master/server/requestlog/requestlog.go
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -97,7 +97,7 @@ func (s *Middleware) LoggingMiddleware() func(http.Handler) http.Handler {
}
}
func (s *Middleware) CORSMiddleware() func(http.Handler) http.Handler {
func (s *Middleware) CORS() func(http.Handler) http.Handler {
return cors.Handler(cors.Options{
// CHANGEME! these are defaults, change them to suit your needs or
// read from environment/viper.
@@ -112,7 +112,7 @@ func (s *Middleware) CORSMiddleware() func(http.Handler) http.Handler {
})
}
func (s *Middleware) AuthMiddleware() func(http.Handler) http.Handler {
func (s *Middleware) Auth() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// CHANGEME you'll want to change this to do stuff.
@@ -122,7 +122,7 @@ func (s *Middleware) AuthMiddleware() func(http.Handler) http.Handler {
}
}
func (s *Middleware) MetricsMiddleware() func(http.Handler) http.Handler {
func (s *Middleware) Metrics() func(http.Handler) http.Handler {
mdlw := ghmm.New(ghmm.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})
@@ -131,7 +131,7 @@ func (s *Middleware) MetricsMiddleware() func(http.Handler) http.Handler {
}
}
func (s *Middleware) MetricsAuthMiddleware() func(http.Handler) http.Handler {
func (s *Middleware) MetricsAuth() func(http.Handler) http.Handler {
return basicauth.New(
"metrics",
map[string][]string{

View File

@@ -7,7 +7,7 @@ import (
)
func (s *Server) serveUntilShutdown() {
listenAddr := fmt.Sprintf(":%d", s.port)
listenAddr := fmt.Sprintf(":%d", s.params.Config.Port)
s.httpServer = &http.Server{
Addr: listenAddr,
ReadTimeout: 10 * time.Second,

View File

@@ -23,16 +23,16 @@ func (s *Server) SetupRoutes() {
s.router.Use(middleware.Recoverer)
s.router.Use(middleware.RequestID)
s.router.Use(s.mw.LoggingMiddleware())
s.router.Use(s.mw.Logging())
// add metrics middleware only if we can serve them behind auth
if viper.GetString("METRICS_USERNAME") != "" {
s.router.Use(s.mw.MetricsMiddleware())
s.router.Use(s.mw.Metrics())
}
// set up CORS headers. you'll probably want to configure that
// in middlewares.go.
s.router.Use(s.mw.CORSMiddleware())
s.router.Use(s.mw.CORS())
// CHANGEME to suit your needs, or pull from config.
// timeout for request context; your handlers must finish within
@@ -68,12 +68,21 @@ func (s *Server) SetupRoutes() {
// if you want to use a general purpose middleware (http.Handler
// wrapper) on a specific HandleFunc route, you need to take the
// .ServeHTTP of the http.Handler to get its HandleFunc, viz:
authMiddleware := s.mw.AuthMiddleware()
auth := s.mw.Auth()
s.router.Get(
"/login",
authMiddleware(s.h.HandleLogin()).ServeHTTP,
auth(s.h.HandleLoginGET()).ServeHTTP,
)
s.router.Get(
"/signup",
auth(s.h.HandleSignupGET()).ServeHTTP,
)
s.router.Post(
"/signup",
auth(s.h.HandleSignupPOST()).ServeHTTP,
)
// route that panics for testing
// CHANGEME remove this
s.router.Get(
@@ -89,7 +98,7 @@ func (s *Server) SetupRoutes() {
// set up authenticated /metrics route:
if viper.GetString("METRICS_USERNAME") != "" {
s.router.Group(func(r chi.Router) {
r.Use(s.mw.MetricsAuthMiddleware())
r.Use(s.mw.MetricsAuth())
r.Get("/metrics", http.HandlerFunc(promhttp.Handler().ServeHTTP))
})
}

View File

@@ -64,7 +64,7 @@ func New(lc fx.Lifecycle, params ServerParams) (*Server, error) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
s.startupTime = time.Now()
s.Run() // background FIXME
go s.Run() // background FIXME
return nil
},
OnStop: func(ctx context.Context) error {