random-api-go/router/router.go
wood chen 1fc1069ec1 refactor(docker, config): streamline directory structure and enhance configuration management
- Removed unnecessary volume mounts for public and logs in docker-compose.yml.
- Updated Dockerfile to create necessary directories under /root/data.
- Modified start.sh to copy files to the new public directory location.
- Enhanced config.go to create default configuration and directory structure if not present.
- Adjusted router.go to serve static files from the new public directory path.
2024-11-30 23:34:45 +08:00

41 lines
727 B
Go

package router
import (
"net/http"
"random-api-go/middleware"
)
type Router struct {
mux *http.ServeMux
}
type Handler interface {
Setup(r *Router)
}
func New() *Router {
return &Router{
mux: http.NewServeMux(),
}
}
func (r *Router) Setup(h Handler) {
// 静态文件服务
fileServer := http.FileServer(http.Dir("/root/data/public"))
r.mux.Handle("/", middleware.Chain(
middleware.Recovery,
middleware.MetricsMiddleware,
)(fileServer))
// 设置API路由
h.Setup(r)
}
func (r *Router) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
r.mux.HandleFunc(pattern, handler)
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.mux.ServeHTTP(w, req)
}