bug fixes

This commit is contained in:
Hendrik Schlehlein 2019-01-17 01:35:51 +01:00
parent 615f1969d7
commit c534e4bf49
13 changed files with 92 additions and 53 deletions

View file

@ -2,5 +2,6 @@ FROM golang:latest
RUN mkdir /app RUN mkdir /app
ADD . /app/ ADD . /app/
WORKDIR /app WORKDIR /app
RUN go get -u
RUN go build -o main . RUN go build -o main .
CMD ["/app/main"] CMD ["/app/main"]

View file

@ -1,4 +1,4 @@
# go-tsviewer **[WIP]** # tsviewer **[WIP]**
## **WARNING** This API is not usable ATM ## **WARNING** This API is not usable ATM
A REST API made for TS3 Viewer. A REST API made for TS3 Viewer.

View file

@ -8,10 +8,9 @@ import (
) )
type Config struct { type Config struct {
IP string `json:"ip"`
Port uint16 `json:"port"`
User User `json:"user"` User User `json:"user"`
Server Server `json:"server"` ServerTS ServerTS `json:"serverTS"`
ServerWeb ServerWeb `json:"serverWeb"`
} }
type User struct { type User struct {
@ -19,7 +18,13 @@ type User struct {
Password string `json:"password"` 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"` Port uint16 `json:"port"`
} }
@ -56,14 +61,17 @@ func (config Config) createFile() error {
func defaults() Config { func defaults() Config {
return Config{ return Config{
IP: "127.0.0.1",
Port: 10011,
User: User{ User: User{
Name: "serveradmin", Name: "serveradmin",
Password: "", Password: "",
}, },
Server: Server{ ServerTS: ServerTS{
Port: 9987, IP: "127.0.0.1",
PortServer: 9987,
PortQuery: 10011,
},
ServerWeb: ServerWeb{
Port: 80,
}, },
} }
} }

View file

@ -4,13 +4,13 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"git.cliffbreak.de/haveachin/go-tsviewer/response" "git.cliffbreak.de/Cliffbreak/tsviewer/response"
"github.com/go-chi/chi" "github.com/go-chi/chi"
) )
func ChannelAPIHandler(s Service) http.HandlerFunc { func ChannelAPIHandler(s Service) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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) id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64)
if err != nil { if err != nil {
return http.StatusBadRequest, err return http.StatusBadRequest, err
@ -28,7 +28,7 @@ func ChannelAPIHandler(s Service) http.HandlerFunc {
func ChannelsAPIHandler(s Service) http.HandlerFunc { func ChannelsAPIHandler(s Service) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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() cc, err := s.Channels()
if err != nil { if err != nil {
return http.StatusBadRequest, err return http.StatusBadRequest, err

View file

@ -4,13 +4,13 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"git.cliffbreak.de/haveachin/go-tsviewer/response" "git.cliffbreak.de/Cliffbreak/tsviewer/response"
"github.com/go-chi/chi" "github.com/go-chi/chi"
) )
func ClientAPIHandler(s Service) http.HandlerFunc { func ClientAPIHandler(s Service) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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) id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64)
if err != nil { if err != nil {
return http.StatusBadRequest, err return http.StatusBadRequest, err
@ -28,7 +28,7 @@ func ClientAPIHandler(s Service) http.HandlerFunc {
func ClientsAPIHandler(s Service) http.HandlerFunc { func ClientsAPIHandler(s Service) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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() cc, err := s.Clients()
if err != nil { if err != nil {
return http.StatusBadRequest, err return http.StatusBadRequest, err

View file

@ -3,12 +3,12 @@ package server
import ( import (
"net/http" "net/http"
"git.cliffbreak.de/haveachin/go-tsviewer/response" "git.cliffbreak.de/Cliffbreak/tsviewer/response"
) )
func InfoAPIHandler(s Service) http.HandlerFunc { func InfoAPIHandler(s Service) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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() s, err := s.Info()
if err != nil { if err != nil {
return http.StatusNotFound, err return http.StatusNotFound, err

10
main.go
View file

@ -5,11 +5,11 @@ import (
"net/http" "net/http"
"time" "time"
"git.cliffbreak.de/haveachin/go-tsviewer/config" "git.cliffbreak.de/Cliffbreak/tsviewer/config"
"git.cliffbreak.de/haveachin/go-tsviewer/features/channel" "git.cliffbreak.de/Cliffbreak/tsviewer/features/channel"
"git.cliffbreak.de/haveachin/go-tsviewer/features/client" "git.cliffbreak.de/Cliffbreak/tsviewer/features/client"
"git.cliffbreak.de/haveachin/go-tsviewer/features/server" "git.cliffbreak.de/Cliffbreak/tsviewer/features/server"
"git.cliffbreak.de/haveachin/go-tsviewer/service" "git.cliffbreak.de/Cliffbreak/tsviewer/service"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
) )

View file

@ -10,12 +10,10 @@ type Error struct {
} }
// NewError creates a new Response fill with an error // NewError creates a new Response fill with an error
func NewError(status int, err error) *Response { func NewError(status int, err error) *Error {
return &Response{ return &Error{
Content: Error{
Status: status, Status: status,
Error: err.Error(), Error: err.Error(),
Timestamp: time.Now().Format("2006-01-02T15:04:05Z"), //.Format("02 Jan 2006, 15:04:05 MST"), Timestamp: time.Now().Format("2006-01-02T15:04:05Z"), //.Format("02 Jan 2006, 15:04:05 MST"),
},
} }
} }

View file

@ -5,7 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"git.cliffbreak.de/haveachin/go-tsviewer/request/meta" "git.cliffbreak.de/Cliffbreak/tsviewer/request/meta"
) )
type HandlerFunc func() (int, error) type HandlerFunc func() (int, error)
@ -74,14 +74,14 @@ func (resp Response) Send(w http.ResponseWriter, status int) (int, error) {
return status, nil return status, nil
} }
func Handler(w http.ResponseWriter, hf HandlerFunc) { func Handler(w http.ResponseWriter, r *http.Request, hf HandlerFunc) {
status, err := hf() status, err := hf()
if err == nil { if err == nil {
return return
} }
log.Printf("HTTP %d: %q", status, err) 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) http.Error(w, http.StatusText(status), status)
} }
} }

View file

@ -3,7 +3,7 @@ package service
import ( import (
"errors" "errors"
"git.cliffbreak.de/haveachin/go-tsviewer/features/channel" "git.cliffbreak.de/Cliffbreak/tsviewer/features/channel"
"github.com/multiplay/go-ts3" "github.com/multiplay/go-ts3"
) )
@ -37,17 +37,19 @@ func (s Service) Channels() ([]*channel.Channel, error) {
var cc []*channel.Channel var cc []*channel.Channel
for _, channel := range channels { for i, channel := range channels {
if channel.ParentID == 0 { if channel == nil {
cc = append(cc, convertChannel(channel))
continue continue
} }
if channel.ParentID == 0 {
cc = append(cc, convertChannel(channel))
removeItem(&channels, i)
}
}
for _, c := range cc { for _, c := range cc {
if c.ID == channel.ParentID { addSubChannels(c, &channels)
c.Subchannels = append(c.Subchannels, *convertChannel(channel))
}
}
} }
return cc, nil return cc, nil
@ -62,3 +64,29 @@ func convertChannel(c *ts3.Channel) *channel.Channel {
NeededSubscribePower: c.NeededSubscribePower, 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)
}
}
}

View file

@ -3,7 +3,7 @@ package service
import ( import (
"errors" "errors"
"git.cliffbreak.de/haveachin/go-tsviewer/features/client" "git.cliffbreak.de/Cliffbreak/tsviewer/features/client"
"github.com/multiplay/go-ts3" "github.com/multiplay/go-ts3"
) )

View file

@ -3,7 +3,7 @@ package service
import ( import (
"time" "time"
"git.cliffbreak.de/haveachin/go-tsviewer/features/server" "git.cliffbreak.de/Cliffbreak/tsviewer/features/server"
) )
func (s Service) Info() (*server.Server, error) { func (s Service) Info() (*server.Server, error) {

View file

@ -1,10 +1,11 @@
package service package service
import ( import (
"strconv" "fmt"
"log"
"time"
"git.cliffbreak.de/haveachin/go-tsviewer/config" "git.cliffbreak.de/Cliffbreak/tsviewer/config"
"git.cliffbreak.de/haveachin/go-tsviewer/stringer"
ts3 "github.com/multiplay/go-ts3" ts3 "github.com/multiplay/go-ts3"
) )
@ -13,12 +14,7 @@ type Service struct {
} }
func New(config config.Config) (*Service, error) { func New(config config.Config) (*Service, error) {
addr, err := stringer.Build(config.IP, ":", strconv.Itoa(int(config.Port))) client, err := ts3.NewClient(fmt.Sprintf("%s:%d", config.ServerTS.IP, config.ServerTS.PortQuery))
if err != nil {
return nil, err
}
client, err := ts3.NewClient(addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -27,10 +23,18 @@ func New(config config.Config) (*Service, error) {
return nil, err 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 return nil, err
} }
go func() {
for true {
time.Sleep(time.Second * 150)
client.Server.Version()
log.Println("keep alive")
}
}()
return &Service{ return &Service{
TSClient: client, TSClient: client,
}, nil }, nil