-
Notifications
You must be signed in to change notification settings - Fork 28
/
admin_view.go
57 lines (49 loc) · 1.26 KB
/
admin_view.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
package view
import (
"net/http"
"text/template"
"github.com/auula/vasedb/clog"
)
const (
// 默认的 HTML 文件文本
loginHtml = "text/template"
dashboardHtml = "text/template"
)
// AdminView 结构体用于存储所有后台模板
type AdminView struct {
Login *template.Template
Dashboard *template.Template
}
// 能被渲染全局管理员
var admin AdminView
func init() {
// 根据 html 文件来构造后台 view 的模版
admin.Login = template.Must(template.
New("login").
Parse(loginHtml))
admin.Dashboard = template.Must(template.
New("dashboard").
Parse(dashboardHtml))
}
func LoginHandler(w http.ResponseWriter, r *http.Request) {
// 使用 Login 渲染登录页面
data := map[string]interface{}{
"Msg": "使用 Login 渲染登录页面",
}
err := admin.Login.Execute(w, data)
if err != nil {
clog.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func DashboardHandler(w http.ResponseWriter, r *http.Request) {
// 使用 Dashboard 渲染仪表盘页面
data := map[string]interface{}{
"Msg": "使用 Dashboard 渲染仪表盘页面",
}
err := admin.Dashboard.Execute(w, data)
if err != nil {
clog.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}