diff --git a/features/api/channel/channel.go b/features/api/channel/channel.go deleted file mode 100755 index 67e0f30..0000000 --- a/features/api/channel/channel.go +++ /dev/null @@ -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"` -} diff --git a/features/api/channel/handler.go b/features/api/channel/handler.go deleted file mode 100755 index a0ec7cd..0000000 --- a/features/api/channel/handler.go +++ /dev/null @@ -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) - })) - }) -} diff --git a/features/api/channel/routes.go b/features/api/channel/routes.go deleted file mode 100755 index 02532d8..0000000 --- a/features/api/channel/routes.go +++ /dev/null @@ -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 -} diff --git a/features/api/client/client.go b/features/api/client/client.go deleted file mode 100755 index e6ef949..0000000 --- a/features/api/client/client.go +++ /dev/null @@ -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"` -} diff --git a/features/api/client/handler.go b/features/api/client/handler.go deleted file mode 100755 index 7dbfbe1..0000000 --- a/features/api/client/handler.go +++ /dev/null @@ -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) - })) - }) -} diff --git a/features/api/client/routes.go b/features/api/client/routes.go deleted file mode 100755 index 9e20d55..0000000 --- a/features/api/client/routes.go +++ /dev/null @@ -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 -} diff --git a/features/api/server/handler.go b/features/api/server/handler.go deleted file mode 100755 index 916e3a6..0000000 --- a/features/api/server/handler.go +++ /dev/null @@ -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) - })) - }) -} diff --git a/features/api/server/routes.go b/features/api/server/routes.go deleted file mode 100755 index 22342f3..0000000 --- a/features/api/server/routes.go +++ /dev/null @@ -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 -} diff --git a/features/api/server/server.go b/features/api/server/server.go deleted file mode 100755 index 0cbdb34..0000000 --- a/features/api/server/server.go +++ /dev/null @@ -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"` -} diff --git a/features/web/index/handler.go b/features/web/index/handler.go deleted file mode 100755 index ca77456..0000000 --- a/features/web/index/handler.go +++ /dev/null @@ -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) - }) -} diff --git a/features/web/index/page.go b/features/web/index/page.go deleted file mode 100755 index c706649..0000000 --- a/features/web/index/page.go +++ /dev/null @@ -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) -} diff --git a/features/web/index/routes.go b/features/web/index/routes.go deleted file mode 100755 index d14aec2..0000000 --- a/features/web/index/routes.go +++ /dev/null @@ -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 -} diff --git a/features/web/weberror/page.go b/features/web/weberror/page.go deleted file mode 100755 index 5bfa55a..0000000 --- a/features/web/weberror/page.go +++ /dev/null @@ -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) -} diff --git a/gui/template.go b/gui/template.go deleted file mode 100755 index 17f6cd5..0000000 --- a/gui/template.go +++ /dev/null @@ -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 -} diff --git a/main.go b/main.go index 1480e3f..f64b820 100644 --- a/main.go +++ b/main.go @@ -2,29 +2,25 @@ package main import ( "fmt" - "html/template" "log" "net/http" "time" "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" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "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() + cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"Get"}, }) + router.Use( middleware.Logger, middleware.Timeout(5*time.Second), @@ -35,14 +31,8 @@ func Routes(s service.Service, t template.Template) *chi.Mux { 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) { - r.Mount("/", index.GUIRoutes(s, t)) + //r.Mount("/channels", channel.APIRoutes(s)) }) return router @@ -53,6 +43,7 @@ func main() { config, err := config.New() if err != nil { log.Fatal(err) + return } log.Println("Configurations loaded!") @@ -60,21 +51,12 @@ func main() { service, err := service.New(*config) if err != nil { log.Fatal(err) + return } defer service.TSClient.Close() log.Println("Query service connected to", fmt.Sprintf("%s:%d!", config.ServerTS.IP, config.ServerTS.PortQuery)) - log.Println("Loading templates...") - 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) - }) + router := Routes(service) log.Println("Starting the web server locally on port", config.ServerWeb.Port) log.Fatal("Handler: ", http.ListenAndServe(fmt.Sprintf(":%d", config.ServerWeb.Port), router)) diff --git a/static/styles.css b/static/styles.css deleted file mode 100755 index 70a16db..0000000 --- a/static/styles.css +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/templates/error.html b/templates/error.html deleted file mode 100755 index 5ff2593..0000000 --- a/templates/error.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - Error - - -

Oops, an error occurred!

- Error {{.StatusCode}} - {{.Error}} - - \ No newline at end of file diff --git a/templates/index.html b/templates/index.html deleted file mode 100755 index 73c31b4..0000000 --- a/templates/index.html +++ /dev/null @@ -1,33 +0,0 @@ - - - - - TS3 Viewer - - - - - -
-

Online Members

- -

{{.Server.Name}}

- - {{$clients := .Clients}} - {{range .Channels}} -

{{.Name}}

- {{$channelId := .ID}} - {{range $clients}} - {{if eq $channelId .ChannelID}} -

{{.Nickname}}

- {{end}} - {{end}} - {{end}} -
- - - \ No newline at end of file