Skip to content

Commit

Permalink
add: api handler & view template.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Jan 5, 2024
1 parent 85b7c7b commit 39a6018
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ coverage.out
.idea
testdata
./main.go.text
system_design.md

# Dependency directories (remove the comment below to include it)
# vendor/
1 change: 1 addition & 0 deletions server/api/admin_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package api
57 changes: 57 additions & 0 deletions server/api/admin_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package api

import (
"net/http"
"text/template"

"github.com/auula/vasedb/clog"
)

const (
// 默认的 HTML 文件文本
loginHtml = "text/template"
dashboardHtml = "text/template"
)

// AdminTemplates 结构体用于存储所有后台模板
type AdminTemplates struct {
Login *template.Template
Dashboard *template.Template
}

// 能被渲染全局管理员
var templates AdminTemplates

func init() {
// 根据 html 文件来构造后台 view 的模版
templates.Login = template.Must(template.
New("login").
Parse(loginHtml))
templates.Dashboard = template.Must(template.
New("dashboard").
Parse(dashboardHtml))
}

func LoginHandler(w http.ResponseWriter, r *http.Request) {
// 使用 Login 渲染登录页面
data := map[string]interface{}{
"Msg": "使用 Login 渲染登录页面",
}
err := templates.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 := templates.Dashboard.Execute(w, data)
if err != nil {
clog.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
File renamed without changes.

0 comments on commit 39a6018

Please sign in to comment.