fixup
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// Package handlers 實作 Nestly 的 HTTP handlers 與路由。
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
|
||||
"nestly/internal/auth"
|
||||
"nestly/internal/models"
|
||||
"nestly/internal/templates"
|
||||
"nestly/web"
|
||||
)
|
||||
|
||||
// Dependencies 集中所有 handler 依賴的週邊設施。
|
||||
type Dependencies struct {
|
||||
Templates *templates.Engine
|
||||
Accounts *models.AccountStore
|
||||
Sessions *auth.SessionManager
|
||||
}
|
||||
|
||||
// NewRouter 建立應用程式的完整路由與中介軟體。
|
||||
func NewRouter(deps Dependencies) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
|
||||
// 靜態資源由內嵌 FS 提供,部署不依賴磁碟上的 web/ 目錄。
|
||||
staticFS, err := fs.Sub(web.StaticFS, "static")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
||||
|
||||
home := &HomeHandler{deps: deps}
|
||||
authh := &AuthHandler{deps: deps}
|
||||
|
||||
r.Get("/", home.Show)
|
||||
r.Get("/login", authh.Login)
|
||||
r.Post("/login", authh.DoLogin)
|
||||
r.Post("/logout", authh.Logout)
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user