You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.2 KiB
101 lines
2.2 KiB
package core
|
|
|
|
import (
|
|
"aufs/db"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// 输出的结构
|
|
type ScJson struct {
|
|
Message string `json:"message"`
|
|
Status int `json:"status"`
|
|
Data []db.StServerInfo `json:"data"`
|
|
}
|
|
|
|
// 系统设置
|
|
func Settdb(w http.ResponseWriter, r *http.Request) {
|
|
// 数据库初始化
|
|
db.Init()
|
|
sclst := db.GetlScList()
|
|
//
|
|
scresp := ScJson{
|
|
Message: "success",
|
|
Status: 200,
|
|
Data: sclst,
|
|
}
|
|
uCorsHadler(w, r)
|
|
json.NewEncoder(w).Encode(scresp)
|
|
}
|
|
|
|
// 获取详情
|
|
func Scdetail(w http.ResponseWriter, r *http.Request) {
|
|
// 获取参数
|
|
id := r.URL.Query().Get("id")
|
|
// 转换为整数
|
|
scid, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
// 处理转换错误
|
|
}
|
|
// 数据库初始化
|
|
db.Init()
|
|
// 获取详情
|
|
scinfo := db.GetServerInfo(int16(scid))
|
|
// 输出
|
|
scresp := ScJson{
|
|
Message: "success",
|
|
Status: 200,
|
|
Data: []db.StServerInfo{scinfo},
|
|
}
|
|
uCorsHadler(w, r)
|
|
json.NewEncoder(w).Encode(scresp)
|
|
}
|
|
|
|
// 编辑
|
|
func Scedit(w http.ResponseWriter, r *http.Request) {
|
|
// 使用post方式
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
} else {
|
|
// 设置响应头为JSON格式
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Access-Control-Allow-Origin", "*") // 允许跨域(生产环境需指定具体域名)
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
// 处理预检请求
|
|
if r.Method == http.MethodOptions {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
// 解析表单数据
|
|
r.ParseForm()
|
|
// 接受json 数据
|
|
var scinfo db.StServerInfo
|
|
err := json.NewDecoder(r.Body).Decode(&scinfo)
|
|
if err != nil {
|
|
// 解析失败返回错误信息
|
|
errorResp := map[string]string{"error": "JSON格式错误", "details": err.Error()}
|
|
json.NewEncoder(w).Encode(errorResp)
|
|
return
|
|
}
|
|
defer r.Body.Close()
|
|
// 处理接收的json
|
|
// 数据库初始化
|
|
db.Init()
|
|
// 编辑
|
|
db.UpdateServerInfo(scinfo)
|
|
// 输出
|
|
scresp := ScJson{
|
|
Message: "success",
|
|
Status: 200,
|
|
Data: []db.StServerInfo{scinfo},
|
|
}
|
|
uCorsHadler(w, r)
|
|
json.NewEncoder(w).Encode(scresp)
|
|
|
|
}
|
|
|
|
}
|
|
|