Files
2024-11-18 23:36:12 +08:00

108 lines
2.8 KiB
Go
Executable File

package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
var webIp = ""
var webPort = "2250"
var webUrl = ""
// 构建数据库条目结构体
type PwListRow_Mysql struct {
PID int `db:"pwId"` //条目ID
AN string `db:"appName"` //应用名称
AUrl string `db:"appUrl"` //应用网页地址
AUN string `db:"appUserName"` //用户名
APW string `db:"appPassword"` //密码
OT string `db:"originTime"` //添加日期
}
// 构建数据库条目
type PwListRow_Json struct {
PID int `json:"pwId"` //条目ID
AN string `json:"appName"` //应用名称
AUrl string `json:"appUrl"` //应用网页地址
AUN string `json:"appUserName"` //用户名
APW string `json:"appPassword"` //密码
OT string `json:"originTime"` //添加日期
MD5 string `json:"MD5"` //用户MD5
}
// 获取网页模板
func load_Template() *template.Template {
result := template.New("templates")
template.Must(result.ParseGlob("res/html/*.html"))
return result
}
func main() {
//设置监听端口
if webIp == "" {
webIp = "localhost"
}
server := http.Server{
Addr: webIp + ":" + webPort,
}
webUrl = "http://" + webIp + ":" + webPort + "/config"
fmt.Println("[网页服务器] 网页服务已开启:" + webUrl)
//加载网页模板
template := load_Template()
//欢迎页面
http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) {
//读取form字段
t, _ := template.ParseFiles("res/html/layout.html", "res/html/config.html")
if t != nil {
err := t.ExecuteTemplate(w, "layout", "配置")
if err != nil {
log.Fatalln(err.Error())
}
} else {
w.WriteHeader(http.StatusNotFound)
}
})
//扫码登陆页面
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
//读取form字段
t, _ := template.ParseFiles("res/html/layout.html", "res/html/login.html")
if t != nil {
err := t.ExecuteTemplate(w, "layout", "扫码登录")
if err != nil {
log.Fatalln(err.Error())
}
} else {
w.WriteHeader(http.StatusNotFound)
}
})
//主页面
http.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) {
//读取form字段
t, _ := template.ParseFiles("res/html/layout.html", "res/html/home.html")
if t != nil {
err := t.ExecuteTemplate(w, "layout", "主页")
if err != nil {
log.Fatalln(err.Error())
}
} else {
w.WriteHeader(http.StatusNotFound)
}
})
//加载静态资源文件
http.Handle("/res/css/", http.FileServer(http.Dir("")))
http.Handle("/res/img/", http.FileServer(http.Dir("")))
http.Handle("/res/js/", http.FileServer(http.Dir("")))
http.Handle("/res/json/", http.FileServer(http.Dir("")))
//启动服务
server.ListenAndServe()
//使用宝塔服务器反向代理实现 域名访问
}