Compare commits

..

5 commits

Author SHA1 Message Date
59bd3d7139 changed config to yaml; removed flag support 2019-04-22 02:51:02 +02:00
a454dbc947 Change ReadME 2019-02-07 21:46:45 +01:00
8416fcbdb4 chnages 2019-02-07 15:13:13 +01:00
alpha
cf378679fb Changes in ReadME 2019-02-07 15:08:42 +01:00
d57986721d Merge branch 'master' of 123niel/tsviewer into master
Geprüft
Mit freundlichen Grüßen
Eichhorn
2019-02-07 14:02:28 +00:00
4 changed files with 58 additions and 140 deletions

2
.gitignore vendored
View file

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

View file

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

View file

@ -1,161 +1,84 @@
package config
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/spf13/viper"
)
type Config struct {
User User `json:"user"`
ServerTS ServerTS `json:"serverTS"`
ServerWeb ServerWeb `json:"serverWeb"`
User struct {
Nickname string
Name string
Password string
}
type User struct {
Nickname string `json:"nickname"`
Name string `json:"name"`
Password string `json:"password"`
ServerTS struct {
IP string
PortServer uint16
PortQuery uint16
}
type ServerTS struct {
IP string `json:"ip"`
PortServer uint16 `json:"portServer"`
PortQuery uint16 `json:"portQuery"`
ServerWeb struct {
Port uint16
}
type ServerWeb struct {
Port uint16 `json:"port"`
}
const FileName = "config.json"
func New() (*Config, error) {
config := defaults()
configFile, err := os.Open(FileName)
if err != nil {
if err := config.createFile(); err != nil {
viper.SetConfigName("config")
viper.SetConfigFile("yaml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
return nil, err
}
log.Println(config)
return &config, nil
}
defer configFile.Close()
setDefaults()
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
var config Config
err := viper.Unmarshal(&config)
config.overrideWithEnv()
config.overrideWithFlags()
config.overrideWithEnvironmentVars()
return &config, nil
return &config, err
}
func (config Config) createFile() error {
configJSON, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
func setDefaults() {
viper.SetDefault("User.Nickname", "serveradmin")
viper.SetDefault("User.Name", "serveradmin")
viper.SetDefault("User.Password", "<changeMe>")
viper.SetDefault("ServerTS.IP", "127.0.0.1")
viper.SetDefault("ServerTS.PortServer", "9987")
viper.SetDefault("ServerTS.PortQuery", "10011")
viper.SetDefault("ServerWeb.Port", "80")
}
return ioutil.WriteFile(FileName, configJSON, 0644)
}
func defaults() Config {
return Config{
User: User{
Nickname: "serveradmin",
Name: "serveradmin",
Password: "",
},
ServerTS: ServerTS{
IP: "127.0.0.1",
PortServer: 9987,
PortQuery: 10011,
},
ServerWeb: ServerWeb{
Port: 80,
},
}
}
func (config *Config) overrideWithEnv() {
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 != "" {
func (config *Config) overrideWithEnvironmentVars() {
if nickname := os.Getenv("TS3_NICKNAME"); nickname != "" {
config.User.Nickname = nickname
}
if username != "" {
if username := os.Getenv("TS3_NAME"); username != "" {
config.User.Name = username
}
if password != "" {
if password := os.Getenv("TS3_PW"); password != "" {
config.User.Password = password
}
if tsIP != "" {
if tsIP := os.Getenv("TS3_IP"); tsIP != "" {
config.ServerTS.IP = tsIP
}
if tsPort <= 65535 && tsPort != 0 {
if tsPort, err := strconv.Atoi(os.Getenv("TS3_PORT")); tsPort <= 65535 && tsPort >= 0 && err == nil {
config.ServerTS.PortServer = uint16(tsPort)
}
if tsQuery <= 65535 && tsQuery != 0 {
if tsQuery, err := strconv.Atoi(os.Getenv("TS3_QUERY")); tsQuery <= 65535 && tsQuery >= 0 && err == nil {
config.ServerTS.PortQuery = uint16(tsQuery)
}
if webPort <= 65535 && webPort != 0 {
if webPort, err := strconv.Atoi(os.Getenv("WEB_PORT")); webPort <= 65535 && webPort >= 0 && err == nil {
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,7 +19,6 @@ import (
"github.com/go-chi/cors"
)
// Routes includes middleware and distributes request to different Routers
func Routes(s service.Service, t template.Template) *chi.Mux {
router := chi.NewRouter()
cors := cors.New(cors.Options{