-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.