Browse Source

使用proto解析

master
xyiege 7 months ago
parent
commit
22bc00339d
  1. 38
      aufs/core/setting.go

38
aufs/core/setting.go

@ -2,8 +2,8 @@ package core
import ( import (
"aufs/db" "aufs/db"
"aufs/proto/pb"
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"strconv" "strconv"
) )
@ -71,35 +71,17 @@ func Scedit(w http.ResponseWriter, r *http.Request) {
return return
} }
// 解析表单数据 // 接受body并将其转换为二进制数据
r.ParseForm() var sc pb.ServiceInfo
// 接受json 数据 err := json.NewDecoder(r.Body).Decode(&sc)
rcon, err := io.ReadAll(r.Body)
if err != nil {
// 解析失败返回错误信息
errorResp := map[string]string{"error": "body read error", "details": err.Error()}
json.NewEncoder(w).Encode(errorResp)
return
}
// 将传递过来的r.body 内容转为db.StServerInfo
var scinfo db.StServerInfo
err = json.Unmarshal(rcon, &scinfo)
if err != nil { if err != nil {
// 解析失败返回错误信息 // 解析失败返回错误信息
errorResp := map[string]string{"error": "JSON格式错误", "details": err.Error()} errorResp := map[string]string{"error": "JSON格式错误", "details": err.Error()}
json.NewEncoder(w).Encode(errorResp) json.NewEncoder(w).Encode(errorResp)
return return
} }
// 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() defer r.Body.Close()
// 处理接收的json // 处理接收的json
// 数据库初始化 // 数据库初始化
// db.Init() // db.Init()
@ -108,16 +90,8 @@ func Scedit(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{ json.NewEncoder(w).Encode(map[string]string{
"message": "success", "message": "success",
"status": "200", "status": "200",
"data": string(rcon), "data": sc.Scname,
}) })
// 输出
// scresp := ScJson{
// Message: "success",
// Status: 200,
// Data: []db.StServerInfo{scinfo},
// }
// uCorsHadler(w, r)
// json.NewEncoder(w).Encode(scresp)
} }

Loading…
Cancel
Save