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
ADD . /app/
WORKDIR /app
RUN go get -u
RUN go build -o main .
CMD ["/app/main"]

View file

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

View file

@ -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,
},
}
}

View file

@ -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

View file

@ -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

View file

@ -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

10
main.go
View file

@ -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"
)

View file

@ -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"),
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}
}

View file

@ -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"
)

View file

@ -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) {

View file

@ -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