cleaned up work tree

This commit is contained in:
Hendrik Schlehlein 2019-05-26 21:45:33 +02:00
parent fe74c466fb
commit ef52fa1c58
18 changed files with 7 additions and 422 deletions

View file

@ -1,14 +0,0 @@
package channel
type Service interface {
Channel(id int) (*Channel, error)
Channels() ([]*Channel, error)
}
type Channel struct {
ID int `json:"id"`
Name string `json:"name"`
Subchannels []Channel `json:"subchannels,omitempty"`
TotalClients int `json:"totalClients"`
NeededSubscribePower int `json:"neededSubscribePower"`
}

View file

@ -1,40 +0,0 @@
package channel
import (
"net/http"
"strconv"
"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, r, response.HandlerFunc(func() (int, error) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64)
if err != nil {
return http.StatusBadRequest, err
}
c, err := s.Channel(int(id))
if err != nil {
return http.StatusNotFound, err
}
return response.New(c, r).Send(w, http.StatusOK)
}))
})
}
func ChannelsAPIHandler(s Service) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.Handler(w, r, response.HandlerFunc(func() (int, error) {
cc, err := s.Channels()
if err != nil {
return http.StatusBadRequest, err
}
return response.New(cc, r).Send(w, http.StatusOK)
}))
})
}

View file

@ -1,14 +0,0 @@
package channel
import (
"github.com/go-chi/chi"
)
func APIRoutes(s Service) *chi.Mux {
router := chi.NewRouter()
router.Get("/{id}", ChannelAPIHandler(s))
router.Get("/", ChannelsAPIHandler(s))
return router
}

View file

@ -1,15 +0,0 @@
package client
type Service interface {
Client(id int) (*Client, error)
Clients() ([]*Client, error)
}
type Client struct {
DatabaseID int `json:"databaseId"`
ChannelID int `json:"channelId"`
Nickname string `json:"nickname"`
Type int `json:"type"`
Away bool `json:"away"`
AwayMessage string `json:"awayMessage"`
}

View file

@ -1,40 +0,0 @@
package client
import (
"net/http"
"strconv"
"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, r, response.HandlerFunc(func() (int, error) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64)
if err != nil {
return http.StatusBadRequest, err
}
c, err := s.Client(int(id))
if err != nil {
return http.StatusNotFound, err
}
return response.New(c, r).Send(w, http.StatusOK)
}))
})
}
func ClientsAPIHandler(s Service) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.Handler(w, r, response.HandlerFunc(func() (int, error) {
cc, err := s.Clients()
if err != nil {
return http.StatusBadRequest, err
}
return response.New(cc, r).Send(w, http.StatusOK)
}))
})
}

View file

@ -1,14 +0,0 @@
package client
import (
"github.com/go-chi/chi"
)
func APIRoutes(s Service) *chi.Mux {
router := chi.NewRouter()
router.Get("/{id}", ClientAPIHandler(s))
router.Get("/", ClientsAPIHandler(s))
return router
}

View file

@ -1,20 +0,0 @@
package server
import (
"net/http"
"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, r, response.HandlerFunc(func() (int, error) {
s, err := s.ServerInfo()
if err != nil {
return http.StatusNotFound, err
}
return response.New(s, r).Send(w, http.StatusOK)
}))
})
}

View file

@ -1,13 +0,0 @@
package server
import (
"github.com/go-chi/chi"
)
func APIRoutes(s Service) *chi.Mux {
router := chi.NewRouter()
router.Get("/info", InfoAPIHandler(s))
return router
}

View file

@ -1,22 +0,0 @@
package server
import "time"
type Service interface {
ServerInfo() (*Server, error)
}
type Server struct {
Name string `json:"name"`
Status string `json:"status"`
Version string `json:"version"`
WelcomeMessage string `json:"welcomeMessage"`
MaxClients int `json:"maxClients"`
ClientsOnline int `json:"clientsOnline"`
ReservedSlots int `json:"reservedSlots"`
Uptime time.Duration `json:"uptime"`
TotalPing float32 `json:"totalPing"`
MinAndroidVersion int `json:"minAndroidVersion"`
MinClientVersion int `json:"minClientVersion"`
MiniOSVersion int `json:"miniOSVersion"`
}

View file

@ -1,32 +0,0 @@
package index
import (
"html/template"
"net/http"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/web/weberror"
)
func IndexGUIHandler(s Service, t template.Template) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
server, err := s.ServerInfo()
if err != nil {
weberror.NewPage(err, http.StatusNotFound).Send(w, t)
return
}
channels, err := s.ChannelsRaw()
if err != nil {
weberror.NewPage(err, http.StatusNotFound).Send(w, t)
return
}
clients, err := s.Clients()
if err != nil {
weberror.NewPage(err, http.StatusNotFound).Send(w, t)
return
}
NewPage(*server, channels, clients).Send(w, t)
})
}

View file

@ -1,35 +0,0 @@
package index
import (
"html/template"
"io"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/channel"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/client"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/server"
)
type Service interface {
ServerInfo() (*server.Server, error)
Clients() ([]*client.Client, error)
Channels() ([]*channel.Channel, error)
ChannelsRaw() ([]*channel.Channel, error)
}
type IndexPage struct {
Server server.Server
Channels []*channel.Channel
Clients []*client.Client
}
func NewPage(server server.Server, channels []*channel.Channel, clients []*client.Client) *IndexPage {
return &IndexPage{
Server: server,
Channels: channels,
Clients: clients,
}
}
func (page IndexPage) Send(w io.Writer, t template.Template) {
t.Lookup("index.html").Execute(w, page)
}

View file

@ -1,15 +0,0 @@
package index
import (
"html/template"
"github.com/go-chi/chi"
)
func GUIRoutes(s Service, t template.Template) *chi.Mux {
router := chi.NewRouter()
router.Get("/", IndexGUIHandler(s, t))
return router
}

View file

@ -1,22 +0,0 @@
package weberror
import (
"html/template"
"io"
)
type ErrorPage struct {
Error error
StatusCode int
}
func NewPage(err error, statusCode int) *ErrorPage {
return &ErrorPage{
Error: err,
StatusCode: statusCode,
}
}
func (page ErrorPage) Send(w io.Writer, t template.Template) {
t.Lookup("error.html").Execute(w, page)
}

View file

@ -1,35 +0,0 @@
package gui
import (
"fmt"
"html/template"
"io/ioutil"
"strings"
)
func LoadTemplates() (*template.Template, error) {
return loadTemplates("templates/")
}
func loadTemplates(path string) (*template.Template, error) {
dir, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
var ff []string
for _, file := range dir {
filename := file.Name()
if strings.HasSuffix(filename, ".html") {
ff = append(ff, fmt.Sprintf("%s%s", path, filename))
}
}
templates, err := template.ParseFiles(ff...)
if err != nil {
return nil, err
}
return templates, nil
}

32
main.go
View file

@ -2,29 +2,25 @@ package main
import ( import (
"fmt" "fmt"
"html/template"
"log" "log"
"net/http" "net/http"
"time" "time"
"git.cliffbreak.de/Cliffbreak/tsviewer/config" "git.cliffbreak.de/Cliffbreak/tsviewer/config"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/channel"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/client"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/server"
"git.cliffbreak.de/Cliffbreak/tsviewer/features/web/index"
"git.cliffbreak.de/Cliffbreak/tsviewer/gui"
"git.cliffbreak.de/Cliffbreak/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"
"github.com/go-chi/cors" "github.com/go-chi/cors"
) )
func Routes(s service.Service, t template.Template) *chi.Mux { func Routes(s *service.Service) *chi.Mux {
router := chi.NewRouter() router := chi.NewRouter()
cors := cors.New(cors.Options{ cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, AllowedOrigins: []string{"*"},
AllowedMethods: []string{"Get"}, AllowedMethods: []string{"Get"},
}) })
router.Use( router.Use(
middleware.Logger, middleware.Logger,
middleware.Timeout(5*time.Second), middleware.Timeout(5*time.Second),
@ -35,14 +31,8 @@ func Routes(s service.Service, t template.Template) *chi.Mux {
cors.Handler, cors.Handler,
) )
router.Route("/v1", func(r chi.Router) {
r.Mount("/channels", channel.APIRoutes(s))
r.Mount("/clients", client.APIRoutes(s))
r.Mount("/server", server.APIRoutes(s))
})
router.Route("/", func(r chi.Router) { router.Route("/", func(r chi.Router) {
r.Mount("/", index.GUIRoutes(s, t)) //r.Mount("/channels", channel.APIRoutes(s))
}) })
return router return router
@ -53,6 +43,7 @@ func main() {
config, err := config.New() config, err := config.New()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return
} }
log.Println("Configurations loaded!") log.Println("Configurations loaded!")
@ -60,21 +51,12 @@ func main() {
service, err := service.New(*config) service, err := service.New(*config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return
} }
defer service.TSClient.Close() defer service.TSClient.Close()
log.Println("Query service connected to", fmt.Sprintf("%s:%d!", config.ServerTS.IP, config.ServerTS.PortQuery)) log.Println("Query service connected to", fmt.Sprintf("%s:%d!", config.ServerTS.IP, config.ServerTS.PortQuery))
log.Println("Loading templates...") router := Routes(service)
templates, err := gui.LoadTemplates()
if err != nil {
log.Fatal(err)
}
log.Println("All templates loaded!")
router := Routes(*service, *templates)
router.Get("/static/*", func(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/static", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r)
})
log.Println("Starting the web server locally on port", config.ServerWeb.Port) log.Println("Starting the web server locally on port", config.ServerWeb.Port)
log.Fatal("Handler: ", http.ListenAndServe(fmt.Sprintf(":%d", config.ServerWeb.Port), router)) log.Fatal("Handler: ", http.ListenAndServe(fmt.Sprintf(":%d", config.ServerWeb.Port), router))

View file

@ -1,22 +0,0 @@
*{
padding: 0;
margin: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.content {
max-width: 500px;
margin: auto;
}
h2{
padding-top: 20px;
}
small{
margin-left: 10px;
}
p.client{
margin-left: 10px;
}

View file

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>Oops, an error occurred!</h1>
<p1>Error {{.StatusCode}}</p1>
<p1>{{.Error}}</p1>
</body>
</html>

View file

@ -1,33 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>TS3 Viewer</title>
<link rel="stylesheet" href="static/styles.css">
</head>
<body>
<div class="content">
<h2>Online Members</h2>
<ul>
{{range .Clients}}
<li>{{.Nickname}}</li>
{{end}}
</ul>
<h2>{{.Server.Name}}</h2>
<!-- <small>{{.Server.Version}}</small> -->
{{$clients := .Clients}}
{{range .Channels}}
<p class="channel">{{.Name}}</p>
{{$channelId := .ID}}
{{range $clients}}
{{if eq $channelId .ChannelID}}
<p class="client">{{.Nickname}}</p>
{{end}}
{{end}}
{{end}}
</div>
</body>
</html>