Skip to content

Commit

Permalink
重构至 PagesController
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Aug 16, 2022
1 parent 84c454b commit f0996f8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
27 changes: 27 additions & 0 deletions 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, "<h1>Hello, 欢迎来到 goblog!</h1>")
}

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

// NotFound 404 页面
func (*PagesController) NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "<h1>请求页面未找到 :(</h1><p>如有疑惑,请联系我们。</p>")
}
21 changes: 0 additions & 21 deletions main.go
Expand Up @@ -50,21 +50,6 @@ func initDB() {
logger.LogError(err)
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<h1>Hello, 欢迎来到 goblog!</h1>")
}

func aboutHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "此博客是用以记录编程笔记,如您有反馈或建议,请联系 "+
"<a href=\"mailto:summer@example.com\">summer@example.com</a>")
}

func notFoundHandler(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "<h1>请求页面未找到 :(</h1><p>如有疑惑,请联系我们。</p>")
}

// Article 对应一条文章数据
type Article struct {
Title, Body string
Expand Down Expand Up @@ -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")
Expand All @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions pkg/route/router.go
@@ -1,6 +1,7 @@
package route

import (
"goblog/routes"
"net/http"

"github.com/gorilla/mux"
Expand All @@ -12,6 +13,7 @@ var Router *mux.Router
// Initialize 初始化路由
func Initialize() {
Router = mux.NewRouter()
routes.RegisterWebRoutes(Router)
}

// Name2URL 通过路由名称来获取 URL
Expand Down
18 changes: 18 additions & 0 deletions 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")
}

0 comments on commit f0996f8

Please sign in to comment.