putting web from main into this branch

This commit is contained in:
viljarb
2026-02-18 20:52:50 +01:00
parent 77dd155299
commit f00bf0a684
37 changed files with 3558 additions and 0 deletions

59
pkg/web/server/routes.go Normal file
View File

@@ -0,0 +1,59 @@
package server
import (
"embed"
"net/http"
"strings"
"time"
"github.com/LazyBachelor/LazyPM/pkg/web/handler"
"github.com/NYTimes/gziphandler"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/cors"
)
func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(cors.AllowAll().Handler)
r.Use(middleware.CleanPath)
r.Use(handler.HTMXMiddleware)
r.Use(handler.ServicesMiddleware(s.Services))
s.handleAssets(r, assets)
r.Get("/", handler.IndexHandler)
r.Get("/status", handler.HandleTaskStatus)
r.Route("/issues", func(r chi.Router) {
r.Get("/", handler.ListIssues)
r.Post("/", handler.CreateIssue)
r.Route("/{id}", func(r chi.Router) {
r.Use(handler.IssueCtx)
r.Get("/", handler.GetIssue)
r.Patch("/", handler.UpdateIssue)
r.Delete("/", handler.DeleteIssue)
})
})
return gziphandler.GzipHandler(r)
}
func (s *Server) handleAssets(r chi.Router, assets embed.FS) {
fileServer := http.FileServer(http.Dir("pkg/web/assets"))
r.Handle("/assets/*",
http.StripPrefix("/assets/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
fileServer.ServeHTTP(w, r)
})))
r.Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache")
http.ServeContent(w, r, "robots.txt", time.Now(), strings.NewReader("User-agent: *\nAllow: /"))
})
}

32
pkg/web/server/server.go Normal file
View File

@@ -0,0 +1,32 @@
package server
import (
"embed"
"net/http"
"time"
"github.com/LazyBachelor/LazyPM/internal/service"
)
type Server struct {
Address string
Assets embed.FS
Services *service.Services
}
// NewServer creates and configures a new HTTP server instance.
func NewServer(props Server) *http.Server {
if props.Address == "" {
props.Address = ":8080"
}
handler := props.RegisterRoutes(props.Assets)
return &http.Server{
Addr: props.Address,
Handler: handler,
IdleTimeout: time.Minute,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
}