Skip to content

Commit

Permalink
fix a lot of issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Feb 2, 2024
1 parent ff51917 commit 5cda4e0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 33 deletions.
15 changes: 8 additions & 7 deletions cmds/liter-server/api_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ func registerPlayerAPI(s *Server, g *gin.RouterGroup) {
// server status APIs
func registerStatus(s *Server, g *gin.RouterGroup) {
g.GET("/conns", func(ctx *gin.Context) {
ctx.Header("Cache-Control", "no-cache")
lm := s.conns.LastModified().Format(time.RFC1123)
ctx.Header("Last-Modified", lm)
if lm2 := ctx.GetHeader("If-Modified-Since"); len(lm2) != 0 && lm2 == lm {
ctx.Status(http.StatusNotModified)
return
}

type resT struct {
Id int `json:"id"`
Addr string `json:"addr"`
Expand All @@ -522,8 +530,6 @@ func registerStatus(s *Server, g *gin.RouterGroup) {
Server string `json:"server"`
Player *liter.PlayerInfo `json:"player,omitempty"`
}
ctx.Header("Cache-Control", "no-cache")
lm := s.conns.LastModified().Format(time.RFC1123)
data := make([]resT, 0, s.conns.Count())
s.conns.ForEach(func(i int, conn *Conn) {
local, svr, ok := conn.LocalServer()
Expand All @@ -539,11 +545,6 @@ func registerStatus(s *Server, g *gin.RouterGroup) {
Server: svr,
})
})
ctx.Header("Last-Modified", lm)
if lm2 := ctx.GetHeader("Last-Modified"); len(lm2) != 0 && lm2 == lm {
ctx.Status(http.StatusNotModified)
return
}
ctx.JSON(http.StatusOK, gin.H{
"status": "ok",
"data": data,
Expand Down
51 changes: 30 additions & 21 deletions cmds/liter-server/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -199,16 +200,18 @@ func (p *PlayerInfo) update() (err error) {
return
}

func (p *PlayerInfo) UnmarshalJSON(buf []byte) (err error) {
var errUnknownPlayerInfo = errors.New("Unknown player info")

func (p *PlayerInfo) UnmarshalJSON(buf []byte) error {
var v any
if err = json.Unmarshal(buf, &v); err != nil {
return
if err := json.Unmarshal(buf, &v); err != nil {
return err
}
switch v := v.(type) {
case string:
var err error
if p.Id, err = uuid.Parse(v); err == nil {
profile, err := AuthClient.GetPlayerProfile(p.Id)
if uid, err := uuid.Parse(v); err == nil {
p.Id = uid
profile, err := AuthClient.GetPlayerProfile(uid)
if err != nil {
return nil
}
Expand All @@ -226,12 +229,12 @@ func (p *PlayerInfo) UnmarshalJSON(buf []byte) (err error) {
var ok bool
var id string
p.Name, _ = v["name"].(string)
if p.onlineMode {
if id, ok = v["id"].(string); ok {
var e error
if p.Id, e = uuid.Parse(id); e != nil {
ok = false
}
if id, ok = v["id"].(string); ok {
var e error
if p.Id, e = uuid.Parse(id); e != nil {
ok = false
}
if p.onlineMode {
profile, err := AuthClient.GetPlayerProfile(p.Id)
if err != nil {
return nil
Expand All @@ -240,20 +243,26 @@ func (p *PlayerInfo) UnmarshalJSON(buf []byte) (err error) {
return nil
}
}
if p.Name == "" {
return fmt.Errorf("Unknown player info")
} else if p.onlineMode {
info, err := AuthClient.GetPlayerInfo(p.Name)
if err != nil {
return nil
if !ok {
if p.Name != "" && p.onlineMode {
info, err := AuthClient.GetPlayerInfo(p.Name)
if err != nil {
return err
}
p.Name = info.Name
p.Id = info.Id
} else {
return errUnknownPlayerInfo
}
p.Name = info.Name
p.Id = info.Id
}
default:
return fmt.Errorf("Unexpected player info (%T)%v", v, v)
}
return
return nil
}

func (p *PlayerInfo) MarshalJSON() (buf []byte, err error) {
return json.Marshal(&p.PlayerInfo)
}

type Whitelist struct {
Expand Down
2 changes: 1 addition & 1 deletion cmds/liter-server/dashboard
9 changes: 7 additions & 2 deletions cmds/liter-server/dashboard_assets_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/gin-gonic/gin"
)

var DashboardAssets http.FileSystem = gin.Dir("dashboard/dist", false)
var DashboardAssets http.FileSystem

func runDashResources() (cleaner func(), err error) {
basedir := "."
Expand All @@ -21,7 +21,12 @@ func runDashResources() (cleaner func(), err error) {
}

loger.Infof("Starting frontend debug server")
cmd := exec.Command("npm", "-C", filepath.Join(basedir, "dashboard"), "run", "build-dev")
npmDir := filepath.Join(basedir, "dashboard")
distDir := filepath.Join(npmDir, "dist")

DashboardAssets = gin.Dir(distDir, false)

cmd := exec.Command("npm", "-C", npmDir, "run", "build-dev")

pout, err := cmd.StdoutPipe()
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmds/liter-server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ func proxyLoginPackets(s *Server, svr, cli *liter.Conn) (res *liter.LoginSuccess
loginDisconnectByErr(cli, err)
return
}
return
}

func parseAndForward(dst, src *script.WrappedConn) <-chan error {
Expand Down
2 changes: 1 addition & 1 deletion cmds/liter-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ func (r *Runner) Run() {
var err error

loger.Infof("Liter Server %s", version)
r.manager.SetLogger(loger)

if r.configDir, err = os.Getwd(); err != nil {
loger.Panicf("Cannot get working dir", err)
}
r.LoadConfigs()

r.manager = script.NewManager()
r.manager.SetLogger(loger)
r.server = NewServer(r.configDir, r.manager)
r.server.configLock = &r.configLock
r.server.cfgHash = &r.cfgHash
Expand Down
2 changes: 2 additions & 0 deletions cmds/liter-server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (m *FlatMemory[T]) Count() int {
}

func (m *FlatMemory[T]) LastModified() time.Time {
m.mux.RLock()
defer m.mux.RUnlock()
return m.lastEdit
}

Expand Down

0 comments on commit 5cda4e0

Please sign in to comment.