mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 13:52:02 +08:00
- 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.
41 lines
727 B
Go
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)
|
|
}
|