putting web from main into this branch
This commit is contained in:
59
pkg/web/server/routes.go
Normal file
59
pkg/web/server/routes.go
Normal 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
32
pkg/web/server/server.go
Normal 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user