refine architecture
change module name to repo
This commit is contained in:
44
pkg/web/server/routes.go
Normal file
44
pkg/web/server/routes.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/NYTimes/gziphandler"
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
Pattern string
|
||||
Handeler http.Handler
|
||||
}
|
||||
|
||||
func (s *Server) RegisterRoutes(assets embed.FS, routes []Route) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
s.handleAssets(mux, assets)
|
||||
|
||||
for _, route := range routes {
|
||||
mux.Handle(route.Pattern, route.Handeler)
|
||||
}
|
||||
|
||||
handler := cors.Default().Handler(mux)
|
||||
|
||||
return gziphandler.GzipHandler(handler)
|
||||
}
|
||||
|
||||
func (s *Server) handleAssets(mux *http.ServeMux, assets embed.FS) {
|
||||
fileServer := http.FileServer(http.FS(assets))
|
||||
mux.Handle("/assets/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Optionally set long-term caching headers for static assets
|
||||
//w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
fileServer.ServeHTTP(w, r)
|
||||
}))
|
||||
|
||||
mux.HandleFunc("/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: /"))
|
||||
})
|
||||
}
|
||||
30
pkg/web/server/server.go
Normal file
30
pkg/web/server/server.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||
"embed"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Address string
|
||||
Assets embed.FS
|
||||
Routes []Route
|
||||
Services *service.Services
|
||||
}
|
||||
|
||||
// NewServer creates and configures a new HTTP server instance.
|
||||
func NewServer(props Server) *http.Server {
|
||||
if props.Address == "" {
|
||||
props.Address = "localhost:8080"
|
||||
}
|
||||
|
||||
return &http.Server{
|
||||
Addr: props.Address,
|
||||
Handler: props.RegisterRoutes(props.Assets, props.Routes),
|
||||
IdleTimeout: time.Minute,
|
||||
ReadTimeout: time.Second * 10,
|
||||
WriteTimeout: time.Second * 10,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user