linstrom/web/debug/server.go

64 lines
1.9 KiB
Go

// Package webdebug provides a http server for local debugging.
// The server is explicitly for localhost only as it offers
// a lot more control and access than the public one.
// Additionally, there is no guarantee for functional authentication
// and authorisation of requests.
// Using it can be considered a security risk.
//
// There is no guarantee for API stability. It might change drastically
// across versions or even commits and doesn't need announcements
package webdebug
import (
"context"
"net/http"
webutils "git.mstar.dev/mstar/goutils/http"
webmiddleware "git.mstar.dev/mstar/linstrom/web/public/middleware"
)
type Server struct {
server *http.Server
}
func New(addr string) *Server {
handler := http.NewServeMux()
handler.HandleFunc("GET /non-deleted", getNonDeletedUsers)
handler.HandleFunc("POST /local-user", createLocalUser)
handler.HandleFunc("GET /delete", deleteUser)
handler.HandleFunc("POST /post-as", postAs)
handler.HandleFunc("GET /notes-for", notesFrom)
handler.HandleFunc("GET /import-user", issueUserImport)
handler.HandleFunc("GET /keys-for", returnKeypair)
handler.HandleFunc("GET /import-server", importServerInfo)
handler.HandleFunc("GET /request-follow", kickoffFollow)
handler.HandleFunc("POST /send-as", proxyMessageToTarget)
handler.HandleFunc("GET /replies-to/{id}", inReplyTo)
handler.HandleFunc("POST /fetch", requestAs)
web := http.Server{
Addr: addr,
Handler: webutils.ChainMiddlewares(
handler,
webutils.BuildLoggingMiddleware(
true,
[]string{"/assets"},
map[string]string{"server": "debug"},
),
webmiddleware.AppendFullPathMiddleware,
webmiddleware.TraceRequestInfoMiddleware,
),
}
return &Server{&web}
}
func (s *Server) Start() error {
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
func (s *Server) Stop() error {
return s.server.Shutdown(context.Background())
}