From c534e4bf490cd37bf186c192236ba156a2dd5a0e Mon Sep 17 00:00:00 2001 From: Hendrik Schlehlein Date: Thu, 17 Jan 2019 01:35:51 +0100 Subject: [PATCH] bug fixes --- DOCKERFILE | 1 + README.md | 2 +- config/config.go | 26 ++++++++++++++-------- features/channel/handler.go | 6 ++--- features/client/handler.go | 6 ++--- features/server/handler.go | 4 ++-- main.go | 10 ++++----- response/error.go | 12 +++++----- response/response.go | 6 ++--- service/channel.go | 44 ++++++++++++++++++++++++++++++------- service/client.go | 2 +- service/server.go | 2 +- service/service.go | 24 +++++++++++--------- 13 files changed, 92 insertions(+), 53 deletions(-) diff --git a/DOCKERFILE b/DOCKERFILE index 2a9fb99..a58f996 100644 --- a/DOCKERFILE +++ b/DOCKERFILE @@ -2,5 +2,6 @@ FROM golang:latest RUN mkdir /app ADD . /app/ WORKDIR /app +RUN go get -u RUN go build -o main . CMD ["/app/main"] \ No newline at end of file diff --git a/README.md b/README.md index 7f05e39..407397e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# go-tsviewer **[WIP]** +# tsviewer **[WIP]** ## **WARNING** This API is not usable ATM A REST API made for TS3 Viewer. diff --git a/config/config.go b/config/config.go index ea0acb5..ac9fde4 100644 --- a/config/config.go +++ b/config/config.go @@ -8,10 +8,9 @@ import ( ) type Config struct { - IP string `json:"ip"` - Port uint16 `json:"port"` - User User `json:"user"` - Server Server `json:"server"` + User User `json:"user"` + ServerTS ServerTS `json:"serverTS"` + ServerWeb ServerWeb `json:"serverWeb"` } type User struct { @@ -19,7 +18,13 @@ type User struct { Password string `json:"password"` } -type Server struct { +type ServerTS struct { + IP string `json:"ip"` + PortServer uint16 `json:"portServer"` + PortQuery uint16 `json:"portQuery"` +} + +type ServerWeb struct { Port uint16 `json:"port"` } @@ -56,14 +61,17 @@ func (config Config) createFile() error { func defaults() Config { return Config{ - IP: "127.0.0.1", - Port: 10011, User: User{ Name: "serveradmin", Password: "", }, - Server: Server{ - Port: 9987, + ServerTS: ServerTS{ + IP: "127.0.0.1", + PortServer: 9987, + PortQuery: 10011, + }, + ServerWeb: ServerWeb{ + Port: 80, }, } } diff --git a/features/channel/handler.go b/features/channel/handler.go index d0e3be8..a0ec7cd 100644 --- a/features/channel/handler.go +++ b/features/channel/handler.go @@ -4,13 +4,13 @@ import ( "net/http" "strconv" - "git.cliffbreak.de/haveachin/go-tsviewer/response" + "git.cliffbreak.de/Cliffbreak/tsviewer/response" "github.com/go-chi/chi" ) func ChannelAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - response.Handler(w, response.HandlerFunc(func() (int, error) { + response.Handler(w, r, response.HandlerFunc(func() (int, error) { id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) if err != nil { return http.StatusBadRequest, err @@ -28,7 +28,7 @@ func ChannelAPIHandler(s Service) http.HandlerFunc { func ChannelsAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - response.Handler(w, response.HandlerFunc(func() (int, error) { + response.Handler(w, r, response.HandlerFunc(func() (int, error) { cc, err := s.Channels() if err != nil { return http.StatusBadRequest, err diff --git a/features/client/handler.go b/features/client/handler.go index 56b036a..7dbfbe1 100644 --- a/features/client/handler.go +++ b/features/client/handler.go @@ -4,13 +4,13 @@ import ( "net/http" "strconv" - "git.cliffbreak.de/haveachin/go-tsviewer/response" + "git.cliffbreak.de/Cliffbreak/tsviewer/response" "github.com/go-chi/chi" ) func ClientAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - response.Handler(w, response.HandlerFunc(func() (int, error) { + response.Handler(w, r, response.HandlerFunc(func() (int, error) { id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) if err != nil { return http.StatusBadRequest, err @@ -28,7 +28,7 @@ func ClientAPIHandler(s Service) http.HandlerFunc { func ClientsAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - response.Handler(w, response.HandlerFunc(func() (int, error) { + response.Handler(w, r, response.HandlerFunc(func() (int, error) { cc, err := s.Clients() if err != nil { return http.StatusBadRequest, err diff --git a/features/server/handler.go b/features/server/handler.go index 6ed9303..abb1331 100644 --- a/features/server/handler.go +++ b/features/server/handler.go @@ -3,12 +3,12 @@ package server import ( "net/http" - "git.cliffbreak.de/haveachin/go-tsviewer/response" + "git.cliffbreak.de/Cliffbreak/tsviewer/response" ) func InfoAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - response.Handler(w, response.HandlerFunc(func() (int, error) { + response.Handler(w, r, response.HandlerFunc(func() (int, error) { s, err := s.Info() if err != nil { return http.StatusNotFound, err diff --git a/main.go b/main.go index c8e02de..f86ede9 100644 --- a/main.go +++ b/main.go @@ -5,11 +5,11 @@ import ( "net/http" "time" - "git.cliffbreak.de/haveachin/go-tsviewer/config" - "git.cliffbreak.de/haveachin/go-tsviewer/features/channel" - "git.cliffbreak.de/haveachin/go-tsviewer/features/client" - "git.cliffbreak.de/haveachin/go-tsviewer/features/server" - "git.cliffbreak.de/haveachin/go-tsviewer/service" + "git.cliffbreak.de/Cliffbreak/tsviewer/config" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/channel" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/client" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/server" + "git.cliffbreak.de/Cliffbreak/tsviewer/service" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" ) diff --git a/response/error.go b/response/error.go index ce1c42d..1d495bb 100644 --- a/response/error.go +++ b/response/error.go @@ -10,12 +10,10 @@ type Error struct { } // NewError creates a new Response fill with an error -func NewError(status int, err error) *Response { - return &Response{ - Content: Error{ - Status: status, - Error: err.Error(), - Timestamp: time.Now().Format("2006-01-02T15:04:05Z"), //.Format("02 Jan 2006, 15:04:05 MST"), - }, +func NewError(status int, err error) *Error { + return &Error{ + Status: status, + Error: err.Error(), + Timestamp: time.Now().Format("2006-01-02T15:04:05Z"), //.Format("02 Jan 2006, 15:04:05 MST"), } } diff --git a/response/response.go b/response/response.go index 82f65e4..a0a37d8 100644 --- a/response/response.go +++ b/response/response.go @@ -5,7 +5,7 @@ import ( "log" "net/http" - "git.cliffbreak.de/haveachin/go-tsviewer/request/meta" + "git.cliffbreak.de/Cliffbreak/tsviewer/request/meta" ) type HandlerFunc func() (int, error) @@ -74,14 +74,14 @@ func (resp Response) Send(w http.ResponseWriter, status int) (int, error) { return status, nil } -func Handler(w http.ResponseWriter, hf HandlerFunc) { +func Handler(w http.ResponseWriter, r *http.Request, hf HandlerFunc) { status, err := hf() if err == nil { return } log.Printf("HTTP %d: %q", status, err) - if status, err = NewError(status, err).Send(w, status); err != nil { + if status, err = New(NewError(status, err), r).Send(w, status); err != nil { http.Error(w, http.StatusText(status), status) } } diff --git a/service/channel.go b/service/channel.go index 51cf321..b55cffd 100644 --- a/service/channel.go +++ b/service/channel.go @@ -3,7 +3,7 @@ package service import ( "errors" - "git.cliffbreak.de/haveachin/go-tsviewer/features/channel" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/channel" "github.com/multiplay/go-ts3" ) @@ -37,19 +37,21 @@ func (s Service) Channels() ([]*channel.Channel, error) { var cc []*channel.Channel - for _, channel := range channels { - if channel.ParentID == 0 { - cc = append(cc, convertChannel(channel)) + for i, channel := range channels { + if channel == nil { continue } - for _, c := range cc { - if c.ID == channel.ParentID { - c.Subchannels = append(c.Subchannels, *convertChannel(channel)) - } + if channel.ParentID == 0 { + cc = append(cc, convertChannel(channel)) + removeItem(&channels, i) } } + for _, c := range cc { + addSubChannels(c, &channels) + } + return cc, nil } @@ -62,3 +64,29 @@ func convertChannel(c *ts3.Channel) *channel.Channel { NeededSubscribePower: c.NeededSubscribePower, } } + +func removeItem(slice *[]*ts3.Channel, id int) { + ss := *slice + length := len(ss) + + if length < id+1 { + ss = nil + } else if length == id+1 { + ss = ss[id-1:] + } else if length > id+1 { + ss = append(ss[:id], ss[id+1:]...) + } + + *slice = ss +} + +func addSubChannels(c *channel.Channel, channels *[]*ts3.Channel) { + for i, channel := range *channels { + if c.ID == channel.ParentID { + removeItem(channels, i) + subChannel := convertChannel(channel) + addSubChannels(subChannel, channels) + c.Subchannels = append(c.Subchannels, *subChannel) + } + } +} diff --git a/service/client.go b/service/client.go index f806807..85e902f 100644 --- a/service/client.go +++ b/service/client.go @@ -3,7 +3,7 @@ package service import ( "errors" - "git.cliffbreak.de/haveachin/go-tsviewer/features/client" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/client" "github.com/multiplay/go-ts3" ) diff --git a/service/server.go b/service/server.go index d5f2d4a..ce88c49 100644 --- a/service/server.go +++ b/service/server.go @@ -3,7 +3,7 @@ package service import ( "time" - "git.cliffbreak.de/haveachin/go-tsviewer/features/server" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/server" ) func (s Service) Info() (*server.Server, error) { diff --git a/service/service.go b/service/service.go index 86d4187..1a11c76 100644 --- a/service/service.go +++ b/service/service.go @@ -1,10 +1,11 @@ package service import ( - "strconv" + "fmt" + "log" + "time" - "git.cliffbreak.de/haveachin/go-tsviewer/config" - "git.cliffbreak.de/haveachin/go-tsviewer/stringer" + "git.cliffbreak.de/Cliffbreak/tsviewer/config" ts3 "github.com/multiplay/go-ts3" ) @@ -13,12 +14,7 @@ type Service struct { } func New(config config.Config) (*Service, error) { - addr, err := stringer.Build(config.IP, ":", strconv.Itoa(int(config.Port))) - if err != nil { - return nil, err - } - - client, err := ts3.NewClient(addr) + client, err := ts3.NewClient(fmt.Sprintf("%s:%d", config.ServerTS.IP, config.ServerTS.PortQuery)) if err != nil { return nil, err } @@ -27,10 +23,18 @@ func New(config config.Config) (*Service, error) { return nil, err } - if err := client.UsePort(int(config.Server.Port)); err != nil { + if err := client.UsePort(int(config.ServerTS.PortServer)); err != nil { return nil, err } + go func() { + for true { + time.Sleep(time.Second * 150) + client.Server.Version() + log.Println("keep alive") + } + }() + return &Service{ TSClient: client, }, nil