diff --git a/app/http/controllers/pages_controller.go b/app/http/controllers/pages_controller.go new file mode 100644 index 0000000..f95847f --- /dev/null +++ b/app/http/controllers/pages_controller.go @@ -0,0 +1,27 @@ +package controllers + +import ( + "fmt" + "net/http" +) + +// PagesController 处理静态页面 +type PagesController struct { +} + +// Home 首页 +func (*PagesController) Home(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "

Hello, 欢迎来到 goblog!

") +} + +// About 关于我们页面 +func (*PagesController) About(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "此博客是用以记录编程笔记,如您有反馈或建议,请联系 "+ + "summer@example.com") +} + +// NotFound 404 页面 +func (*PagesController) NotFound(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + fmt.Fprint(w, "

请求页面未找到 :(

如有疑惑,请联系我们。

") +} diff --git a/main.go b/main.go index 94705ca..da28bd7 100644 --- a/main.go +++ b/main.go @@ -50,21 +50,6 @@ func initDB() { logger.LogError(err) } -func homeHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "

Hello, 欢迎来到 goblog!

") -} - -func aboutHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "此博客是用以记录编程笔记,如您有反馈或建议,请联系 "+ - "summer@example.com") -} - -func notFoundHandler(w http.ResponseWriter, r *http.Request) { - - w.WriteHeader(http.StatusNotFound) - fmt.Fprint(w, "

请求页面未找到 :(

如有疑惑,请联系我们。

") -} - // Article 对应一条文章数据 type Article struct { Title, Body string @@ -505,9 +490,6 @@ func main() { route.Initialize() router = route.Router - router.HandleFunc("/", homeHandler).Methods("GET").Name("home") - router.HandleFunc("/about", aboutHandler).Methods("GET").Name("about") - router.HandleFunc("/articles/{id:[0-9]+}", articlesShowHandler).Methods("GET").Name("articles.show") router.HandleFunc("/articles", articlesIndexHandler).Methods("GET").Name("articles.index") router.HandleFunc("/articles", articlesStoreHandler).Methods("POST").Name("articles.store") @@ -516,9 +498,6 @@ func main() { router.HandleFunc("/articles/{id:[0-9]+}", articlesUpdateHandler).Methods("POST").Name("articles.update") router.HandleFunc("/articles/{id:[0-9]+}/delete", articlesDeleteHandler).Methods("POST").Name("articles.delete") - // 自定义 404 页面 - router.NotFoundHandler = http.HandlerFunc(notFoundHandler) - // 中间件:强制内容类型为 HTML router.Use(forceHTMLMiddleware) diff --git a/pkg/route/router.go b/pkg/route/router.go index 062050c..591adc2 100644 --- a/pkg/route/router.go +++ b/pkg/route/router.go @@ -1,6 +1,7 @@ package route import ( + "goblog/routes" "net/http" "github.com/gorilla/mux" @@ -12,6 +13,7 @@ var Router *mux.Router // Initialize 初始化路由 func Initialize() { Router = mux.NewRouter() + routes.RegisterWebRoutes(Router) } // Name2URL 通过路由名称来获取 URL diff --git a/routes/web.go b/routes/web.go new file mode 100644 index 0000000..2bfd276 --- /dev/null +++ b/routes/web.go @@ -0,0 +1,18 @@ +package routes + +import ( + "goblog/app/http/controllers" + "net/http" + + "github.com/gorilla/mux" +) + +// RegisterWebRoutes 注册网页相关路由 +func RegisterWebRoutes(r *mux.Router) { + + // 静态页面 + pc := new(controllers.PagesController) + r.NotFoundHandler = http.HandlerFunc(pc.NotFound) + r.HandleFunc("/", pc.Home).Methods("GET").Name("home") + r.HandleFunc("/about", pc.About).Methods("GET").Name("about") +}