Compare commits

..

1 commit

Author SHA1 Message Date
8efe5c4c7a fixed style issues in README.md and main.go 2019-02-07 17:02:55 +01:00
4 changed files with 141 additions and 59 deletions

2
.gitignore vendored
View file

@ -13,5 +13,3 @@
*.out *.out
# Custom Blacklist # Custom Blacklist
#CM

View file

@ -1,16 +1,20 @@
# tsviewer **[ALPHA 1.0.1]** # tsviewer **[WIP]**
A TS3 Viewer with REST API. A TS3 Viewer with REST API.
## DOCKER-Config
## Config
### Docker-Config
```bash ```bash
docker build -t name/cont:ver . docker build -t name/cont:ver .
docker run -e TS3_IP="<SERVER-IP>" -e TS3_PW="<SERVER-ADMIN_PW>" -e WEB_PORT="8080" -d -p 8080:8080 name/cont:ver docker run -e TS3_IP="<SERVER-IP>" -e TS3_PW="<SERVER-ADMIN_PW>" -e WEB_PORT="8080" -d -p 8080:8080 name/cont:ver
``` ```
ENV:
```Docker
ENV:
```Docker
ENV TS3_IP=127.0.0.1 ENV TS3_IP=127.0.0.1
ENV TS3_NAME=serveradmin ENV TS3_NAME=serveradmin
ENV TS3_PW=<changeMe> ENV TS3_PW=<changeMe>
@ -18,15 +22,12 @@ ENV TS3_PORT=9987
ENV TS3_QUERY=10011 ENV TS3_QUERY=10011
ENV WEB_PORT=8080 ENV WEB_PORT=8080
ENV BLACKLIST_USER=USER ENV BLACKLIST_USER=USER
``` ```
# Features ### API-Config
## Config
The config file is generated automatically on first startup. The config file is generated automatically on first startup.
```json ```json
{ {
"user": { "user": {
@ -45,17 +46,20 @@ The config file is generated automatically on first startup.
} }
``` ```
## URL-Parameter ## Features
### URL-Parameter
| Name | Type | Description | | Name | Type | Description |
| ---------- | ------ | ------------------------ | | ---------- | ------ | ------------------------ |
| `pretty` | `bool` | pretty-prints JSON | | `pretty` | `bool` | pretty-prints JSON |
| `envelope` | `bool` | wraps JSON in data array | | `envelope` | `bool` | wraps JSON in data array |
## Channels ### Channels
- **`GET`** `/v1/channels/:id` - **`GET`** `/v1/channels/:id`
- **`GET`** `/v1/channels` - **`GET`** `/v1/channels`
```json ```json
{ {
"databaseId": 1, "databaseId": 1,
@ -67,10 +71,11 @@ The config file is generated automatically on first startup.
} }
``` ```
## Clients ### Clients
- **`GET`** `/v1/clients/:id` - **`GET`** `/v1/clients/:id`
- **`GET`** `/v1/clients/` - **`GET`** `/v1/clients/`
```json ```json
{ {
"id": 1, "id": 1,
@ -83,9 +88,10 @@ The config file is generated automatically on first startup.
} }
``` ```
## Server ### Server
- **`GET`** `/v1/server/info` - **`GET`** `/v1/server/info`
```json ```json
{ {
"name": "TeamSpeak ]I[ Server", "name": "TeamSpeak ]I[ Server",

View file

@ -1,84 +1,161 @@
package config package config
import ( import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os" "os"
"strconv" "strconv"
"github.com/spf13/viper"
) )
type Config struct { type Config struct {
User struct { User User `json:"user"`
Nickname string ServerTS ServerTS `json:"serverTS"`
Name string ServerWeb ServerWeb `json:"serverWeb"`
Password string }
}
ServerTS struct { type User struct {
IP string Nickname string `json:"nickname"`
PortServer uint16 Name string `json:"name"`
PortQuery uint16 Password string `json:"password"`
} }
ServerWeb struct {
Port uint16 type ServerTS struct {
} IP string `json:"ip"`
PortServer uint16 `json:"portServer"`
PortQuery uint16 `json:"portQuery"`
}
type ServerWeb struct {
Port uint16 `json:"port"`
} }
const FileName = "config.json" const FileName = "config.json"
func New() (*Config, error) { func New() (*Config, error) {
viper.SetConfigName("config") config := defaults()
viper.SetConfigFile("yaml")
viper.AddConfigPath(".") configFile, err := os.Open(FileName)
if err := viper.ReadInConfig(); err != nil { if err != nil {
return nil, err if err := config.createFile(); err != nil {
return nil, err
}
log.Println(config)
return &config, nil
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
config.overrideWithEnv()
config.overrideWithFlags()
return &config, nil
}
func (config Config) createFile() error {
configJSON, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
} }
setDefaults() return ioutil.WriteFile(FileName, configJSON, 0644)
var config Config
err := viper.Unmarshal(&config)
config.overrideWithEnvironmentVars()
return &config, err
} }
func setDefaults() { func defaults() Config {
viper.SetDefault("User.Nickname", "serveradmin") return Config{
viper.SetDefault("User.Name", "serveradmin") User: User{
viper.SetDefault("User.Password", "<changeMe>") Nickname: "serveradmin",
viper.SetDefault("ServerTS.IP", "127.0.0.1") Name: "serveradmin",
viper.SetDefault("ServerTS.PortServer", "9987") Password: "",
viper.SetDefault("ServerTS.PortQuery", "10011") },
viper.SetDefault("ServerWeb.Port", "80") ServerTS: ServerTS{
IP: "127.0.0.1",
PortServer: 9987,
PortQuery: 10011,
},
ServerWeb: ServerWeb{
Port: 80,
},
}
} }
func (config *Config) overrideWithEnvironmentVars() { func (config *Config) overrideWithEnv() {
if nickname := os.Getenv("TS3_NICKNAME"); nickname != "" { nickname := os.Getenv("TS3_NICKNAME")
username := os.Getenv("TS3_NAME")
password := os.Getenv("TS3_PW")
tsIP := os.Getenv("TS3_IP")
tsPort, _ := strconv.Atoi(os.Getenv("TS3_PORT"))
tsQuery, _ := strconv.Atoi(os.Getenv("TS3_QUERY"))
webPort, _ := strconv.Atoi(os.Getenv("WEB_PORT"))
if nickname != "" {
config.User.Nickname = nickname config.User.Nickname = nickname
} }
if username := os.Getenv("TS3_NAME"); username != "" { if username != "" {
config.User.Name = username config.User.Name = username
} }
if password := os.Getenv("TS3_PW"); password != "" { if password != "" {
config.User.Password = password config.User.Password = password
} }
if tsIP := os.Getenv("TS3_IP"); tsIP != "" { if tsIP != "" {
config.ServerTS.IP = tsIP config.ServerTS.IP = tsIP
} }
if tsPort, err := strconv.Atoi(os.Getenv("TS3_PORT")); tsPort <= 65535 && tsPort >= 0 && err == nil { if tsPort <= 65535 && tsPort != 0 {
config.ServerTS.PortServer = uint16(tsPort) config.ServerTS.PortServer = uint16(tsPort)
} }
if tsQuery, err := strconv.Atoi(os.Getenv("TS3_QUERY")); tsQuery <= 65535 && tsQuery >= 0 && err == nil { if tsQuery <= 65535 && tsQuery != 0 {
config.ServerTS.PortQuery = uint16(tsQuery) config.ServerTS.PortQuery = uint16(tsQuery)
} }
if webPort, err := strconv.Atoi(os.Getenv("WEB_PORT")); webPort <= 65535 && webPort >= 0 && err == nil { if webPort <= 65535 && webPort != 0 {
config.ServerWeb.Port = uint16(webPort) config.ServerWeb.Port = uint16(webPort)
} }
} }
func (config *Config) overrideWithFlags() {
nickname := flag.String("ts3_nickname", "", "TS3Query nickname")
username := flag.String("ts3_name", "", "TS3Query username")
password := flag.String("ts3_pw", "", "TS3Query user password")
tsIP := flag.String("ts3_ip", "", "TS3 IP address")
tsPort := flag.Uint64("ts3_port", 65536, "TS3 port")
tsQuery := flag.Uint64("ts3_query", 65536, "TS3 query port")
webPort := flag.Uint64("web_port", 65536, "Webserver port")
flag.Parse()
if *nickname != "" {
config.User.Nickname = *nickname
}
if *username != "" {
config.User.Name = *username
}
if *password != "" {
config.User.Password = *password
}
if *tsIP != "" {
config.ServerTS.IP = *tsIP
}
if *tsPort <= 65535 && *tsPort != 0 {
config.ServerTS.PortServer = uint16(*tsPort)
}
if *tsQuery <= 65535 && *tsQuery != 0 {
config.ServerTS.PortQuery = uint16(*tsQuery)
}
if *webPort <= 65535 && *webPort != 0 {
config.ServerWeb.Port = uint16(*webPort)
}
}

View file

@ -19,6 +19,7 @@ import (
"github.com/go-chi/cors" "github.com/go-chi/cors"
) )
// Routes includes middleware and distributes request to different Routers
func Routes(s service.Service, t template.Template) *chi.Mux { func Routes(s service.Service, t template.Template) *chi.Mux {
router := chi.NewRouter() router := chi.NewRouter()
cors := cors.New(cors.Options{ cors := cors.New(cors.Options{