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.
 
 
 
 
 
 

264 lines
5.9 KiB

package server
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strconv"
"time"
"github.com/bitly/go-simplejson"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
)
type Server struct {
Id int64 `json:"id"`
Name string `json:"name"`
Code int32 `json:"code"`
IP string `json:"ip"`
Branch string `json:"branch"`
GamePort int64 `json:"game_port" mapstructure:"game_port"`
VersionPort int64 `json:"version_port" mapstructure:"version_port"`
GGatePort int64 `json:"g_gate_port" mapstructure:"g_gate_port"`
CGatePort int64 `json:"c_gate_port" mapstructure:"c_gate_port"`
GmPort int64 `json:"gm_port" mapstructure:"gm_port"`
Order int64 `json:"order" mapstructure:"order"`
}
const FilePath = "/data/docker/data.json"
//const FilePath = "C:\\cygwin64\\data\\docker\\data.json"
var serverList map[int64]*Server
func init() {
serverList = make(map[int64]*Server)
Init()
}
func Init() {
data, err := os.ReadFile(FilePath)
if err != nil {
return
}
json, err := simplejson.NewJson(data)
if err != nil {
return
}
poolMap, err := json.Map()
for id, pool := range poolMap {
conf := Server{}
err := mapstructure.Decode(pool, &conf)
if err == nil {
i, _ := strconv.ParseInt(id, 10, 64)
serverList[i] = &conf
}
}
}
func Save() error {
f, err := os.OpenFile(FilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 1)
if err != nil {
return err
}
data, _ := json.MarshalIndent(serverList, "", "\t\t")
n, err := f.Write(data)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}
type services []Server
func (a services) Len() int {
return len(a)
}
func (a services) Less(i, j int) bool {
return a[i].Order > a[j].Order
}
func (a services) Swap(i, j int) {
a[j], a[i] = a[i], a[j]
}
func GetServerList(ctx *gin.Context) {
var sc []Server
for _, server := range serverList {
sc = append(sc, *server)
}
sort.Sort(services(sc))
ctx.JSON(200, sc)
}
//func SubmitServerList(ctx *gin.Context) {
// body := ctx.Request.Body
// da, _ := io.ReadAll(body)
// cc := Server{}
// err := json.Unmarshal(da, &cc)
// if err != nil {
// fmt.Println("submit server info error")
// ctx.JSON(-1, nil)
// return
// }
// if cc.Id == 0 {
// cc.Id = time.Now().Unix()
// }
// serverList[cc.Id] = &cc
// err = Save()
// if err != nil {
// ctx.JSON(-1, nil)
// return
// }
//}
func UpdateServerList(ctx *gin.Context) {
body := ctx.Request.Body
da, _ := io.ReadAll(body)
jsData := simplejson.New()
err := jsData.UnmarshalJSON(da)
if err != nil {
fmt.Println("submit info error")
return
}
name, err := jsData.Get("name").String()
branch, err := jsData.Get("branch").String()
ip, err := jsData.Get("ip").String()
code, err := jsData.Get("code").Int64()
//d, _ := strconv.Atoi(code)
gamePort, err := jsData.Get("game_port").Int64()
//ga, _ := strconv.Atoi(gamePort)
versionPort, err := jsData.Get("version_port").Int64()
//ve, _ := strconv.Atoi(versionPort)
gGatePort, err := jsData.Get("g_gate_port").Int64()
//gg, _ := strconv.Atoi(gGatePort)
cGatePort, err := jsData.Get("c_gate_port").Int64()
//cg, _ := strconv.Atoi(cGatePort)
gmPort, err := jsData.Get("gm_port").Int64()
//gm, _ := strconv.Atoi(gmPort)
gmorder, err := jsData.Get("order").String()
order, _ := strconv.Atoi(gmorder)
cc := Server{
Id: int64(code),
Name: name,
Code: int32(code),
IP: ip,
Branch: branch,
GamePort: int64(gamePort),
VersionPort: int64(versionPort),
GGatePort: int64(gGatePort),
CGatePort: int64(cGatePort),
GmPort: int64(gmPort),
Order: int64(order),
}
if err != nil {
fmt.Println("submit server info error")
ctx.JSON(-1, nil)
return
}
if cc.Id == 0 {
cc.Id = time.Now().Unix()
}
serverList[cc.Id] = &cc
err = Save()
if err != nil {
ctx.JSON(-1, nil)
return
}
}
func SubmitServerList(ctx *gin.Context) {
body := ctx.Request.Body
da, _ := io.ReadAll(body)
jsData := simplejson.New()
err := jsData.UnmarshalJSON(da)
if err != nil {
fmt.Println("submit info error")
return
}
name, err := jsData.Get("name").String()
branch, err := jsData.Get("branch").String()
ip, err := jsData.Get("ip").String()
code, err := jsData.Get("code").String()
d, _ := strconv.Atoi(code)
gamePort, err := jsData.Get("game_port").String()
ga, _ := strconv.Atoi(gamePort)
versionPort, err := jsData.Get("version_port").String()
ve, _ := strconv.Atoi(versionPort)
gGatePort, err := jsData.Get("g_gate_port").String()
gg, _ := strconv.Atoi(gGatePort)
cGatePort, err := jsData.Get("c_gate_port").String()
cg, _ := strconv.Atoi(cGatePort)
gmPort, err := jsData.Get("gm_port").String()
gm, _ := strconv.Atoi(gmPort)
gmorder, err := jsData.Get("order").String()
if gmorder == "" {
gmorder = "0"
}
order, _ := strconv.Atoi(gmorder)
cc := Server{
Id: int64(d),
Name: name,
Code: int32(d),
IP: ip,
Branch: branch,
GamePort: int64(ga),
VersionPort: int64(ve),
GGatePort: int64(gg),
CGatePort: int64(cg),
GmPort: int64(gm),
Order: int64(order),
}
if err != nil {
fmt.Println("submit server info error")
ctx.JSON(-1, nil)
return
}
if cc.Id == 0 {
cc.Id = time.Now().Unix()
}
serverList[cc.Id] = &cc
err = Save()
if err != nil {
ctx.JSON(-1, nil)
return
}
}
func ServerApi(ctx *gin.Context) {
var sc []string
for _, server := range serverList {
if server.Code == 0 {
continue
}
sc = append(sc, server.Name+":"+strconv.Itoa(int(server.Code)))
}
ret := make(map[string][]string)
ret["list"] = sc
ctx.JSON(200, ret)
}
func DeleteServerList(ctx *gin.Context) {
id := ctx.Request.URL.Query().Get("id")
if id == "" {
ctx.JSON(-1, nil)
return
}
d, err := strconv.ParseInt(id, 10, 64)
delete(serverList, d)
err = Save()
if err != nil {
fmt.Println("delete server list conf error")
ctx.JSON(-1, nil)
return
}
ctx.JSON(200, nil)
}