-
Notifications
You must be signed in to change notification settings - Fork 0
/
urls.go
90 lines (77 loc) · 2.96 KB
/
urls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package blog
// 此单元是参照gopher写的,所以文件名和函数名几乎一样
import (
"fmt"
"net/http"
"time"
)
// 回调函数
type HandleFunc func(*Handlder)
// 路由结构体
type Route struct {
URL string // 请求的url地址
HTMLFile string // 渲染的静态文件
Permission PerType // 权限
HandleFunc HandleFunc // 回调函数
}
// 新建带有请求的handler,在这里可以启动数据库等等操作吧
func NewHandler(w http.ResponseWriter, r *http.Request, file string) *Handlder {
return &Handlder{
ResponseWriter: w,
Request: r,
StartTime: time.Now(),
HTMLFile: file,
DB: openDatabase(), // 每次都去打开数据库,不知道好不好,还是一次性打开呢????
}
}
// 纯复制的。。。
func fileHandler(w http.ResponseWriter, req *http.Request) {
url := req.Method + " " + req.URL.Path
fmt.Println(url)
filePath := req.URL.Path[1:]
//http.
http.ServeFile(w, req, filePath)
}
// 自定义404页面
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
// why?
if r.RequestURI == "/favicon.ico" {
return
}
http.Redirect(w, r, "/error", http.StatusFound)
}
// 重定向
func (self *Handlder) Redirect(url string) {
http.Redirect(self.ResponseWriter, self.Request, url, http.StatusFound)
}
// 路由规则表
// 配置种各样的路由规则
var routes = []Route{
{"/", "/blog.html", Everyone, indexHandler},
{"/index", "blog.html", Everyone, indexHandler},
{"/Me", "aboutMe.html", Everyone, aboutMeHandler},
{"/rss", "rss.xml", Everyone, rssHandler},
// editor
//{"/editor", "editor.html", Administrator, editorHandler},
//{"/robots.txt", "robots.txt", Everyone, robotsHandler},
// admin
{"/admin/login", "login.html", Everyone, loginHandler},
{"/error", "404.html", Everyone, errorHandler},
{"/admin", "admin/index.html", Administrator, adminHandler},
{"/admin/index", "admin/index.html", Administrator, adminHandler},
{"/admin/user", "admin/user.html", Administrator, adminHandler},
{"/admin/log", "admin/log.html", Administrator, adminHandler},
{"/admin/articlemgr", "admin/articlemgr.html", Administrator, articleMgrHandler},
{"/admin/form", "admin/form.html", Administrator, adminHandler},
{"/admin/gallery", "admin/gallery.html", Administrator, adminHandler},
{"/admin/logout", "", Administrator, logoutHandler},
{"/admin/upload", "", Administrator, uploadHandler},
{"/admin/savearticle", "", Administrator, saveArticleHandler},
{"/admin/messages", "admin/messages.html", Administrator, messagesHandler},
{"/admin/statistic", "/admin/statistic.html", Administrator, statisticHandler},
{"/article/{id}", "article.html", Everyone, articleHandler},
{"/admin/article/del", "", Administrator, articleDeleteHandler},
{"/admin/article/edit/{id}", "editor.html", Administrator, articleEditHandler},
{"/admin/article/new", "editor.html", Administrator, articleNewHandler},
{"/admin/article/operation", "", Administrator, articleOperationHandler},
}