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.
138 lines
2.9 KiB
138 lines
2.9 KiB
package core
|
|
|
|
import (
|
|
"aufs/db"
|
|
"aufs/proto/pb"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// 输出的结构
|
|
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.GetAllServerInfo()
|
|
//
|
|
scresp := ScJson{
|
|
Message: "success",
|
|
Status: 200,
|
|
Data: sclst,
|
|
}
|
|
uCorsHadler(w, r)
|
|
json.NewEncoder(w).Encode(scresp)
|
|
}
|
|
|
|
// 列表
|
|
func Serlist(w http.ResponseWriter, r *http.Request) {
|
|
// 数据库初始化
|
|
db.Init()
|
|
// 查询状态为1
|
|
sclst := db.GetlScList(1)
|
|
//
|
|
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
|
|
}
|
|
|
|
// 获取到body的内容
|
|
buf := make([]byte, 1024)
|
|
n, _ := r.Body.Read(buf)
|
|
// 操作完后关闭body
|
|
defer r.Body.Close()
|
|
|
|
// 转换为字符串
|
|
base64Str := string(buf[:n])
|
|
// 解码base64
|
|
decodedData, err := base64.StdEncoding.DecodeString(base64Str)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// base64转为proto对象
|
|
var sc pb.ServiceInfo
|
|
err = proto.Unmarshal(decodedData, &sc)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 转换为json字符串
|
|
scjson, _ := json.Marshal(sc)
|
|
// 转换为结构体
|
|
var scinfo db.StServerInfo
|
|
json.Unmarshal(scjson, &scinfo)
|
|
fmt.Printf("scinfo: %v\n", scinfo)
|
|
|
|
// fmt.Printf("sc: %v", sc)
|
|
|
|
// 数据库初始化
|
|
db.Init()
|
|
// 编辑
|
|
ret := db.UpdateServerInfo(scinfo)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"message": "success",
|
|
"status": "200",
|
|
// "data": string(scjson),
|
|
"data": fmt.Sprintf("执行结果:%d", ret),
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|