From 9e2d7e9acced1858f9ea53a3b739b502bf461e56 Mon Sep 17 00:00:00 2001 From: Hendrik Schlehlein Date: Mon, 21 Jan 2019 18:53:33 +0100 Subject: [PATCH] I tried so hard --- features/{ => api}/channel/channel.go | 0 features/{ => api}/channel/handler.go | 0 features/{ => api}/channel/routes.go | 0 features/{ => api}/client/client.go | 0 features/{ => api}/client/handler.go | 0 features/{ => api}/client/routes.go | 0 features/{ => api}/server/handler.go | 2 +- features/{ => api}/server/routes.go | 0 features/{ => api}/server/server.go | 2 +- features/index/handler.go | 12 ---------- features/web/index/handler.go | 32 +++++++++++++++++++++++++ features/web/index/page.go | 34 +++++++++++++++++++++++++++ features/{ => web}/index/routes.go | 4 ++-- features/web/weberror/page.go | 22 +++++++++++++++++ main.go | 13 ++++++---- service/channel.go | 2 +- service/client.go | 2 +- service/server.go | 4 ++-- static/styles.css | 15 ++++++++++++ templates/error.html | 11 +++++++++ templates/index.html | 15 +++++++++++- 21 files changed, 144 insertions(+), 26 deletions(-) rename features/{ => api}/channel/channel.go (100%) rename features/{ => api}/channel/handler.go (100%) rename features/{ => api}/channel/routes.go (100%) rename features/{ => api}/client/client.go (100%) rename features/{ => api}/client/handler.go (100%) rename features/{ => api}/client/routes.go (100%) rename features/{ => api}/server/handler.go (93%) rename features/{ => api}/server/routes.go (100%) rename features/{ => api}/server/server.go (95%) delete mode 100644 features/index/handler.go create mode 100644 features/web/index/handler.go create mode 100644 features/web/index/page.go rename features/{ => web}/index/routes.go (54%) create mode 100644 features/web/weberror/page.go create mode 100644 static/styles.css create mode 100644 templates/error.html diff --git a/features/channel/channel.go b/features/api/channel/channel.go similarity index 100% rename from features/channel/channel.go rename to features/api/channel/channel.go diff --git a/features/channel/handler.go b/features/api/channel/handler.go similarity index 100% rename from features/channel/handler.go rename to features/api/channel/handler.go diff --git a/features/channel/routes.go b/features/api/channel/routes.go similarity index 100% rename from features/channel/routes.go rename to features/api/channel/routes.go diff --git a/features/client/client.go b/features/api/client/client.go similarity index 100% rename from features/client/client.go rename to features/api/client/client.go diff --git a/features/client/handler.go b/features/api/client/handler.go similarity index 100% rename from features/client/handler.go rename to features/api/client/handler.go diff --git a/features/client/routes.go b/features/api/client/routes.go similarity index 100% rename from features/client/routes.go rename to features/api/client/routes.go diff --git a/features/server/handler.go b/features/api/server/handler.go similarity index 93% rename from features/server/handler.go rename to features/api/server/handler.go index abb1331..916e3a6 100644 --- a/features/server/handler.go +++ b/features/api/server/handler.go @@ -9,7 +9,7 @@ import ( 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.Info() + s, err := s.ServerInfo() if err != nil { return http.StatusNotFound, err } diff --git a/features/server/routes.go b/features/api/server/routes.go similarity index 100% rename from features/server/routes.go rename to features/api/server/routes.go diff --git a/features/server/server.go b/features/api/server/server.go similarity index 95% rename from features/server/server.go rename to features/api/server/server.go index 401614f..0cbdb34 100644 --- a/features/server/server.go +++ b/features/api/server/server.go @@ -3,7 +3,7 @@ package server import "time" type Service interface { - Info() (*Server, error) + ServerInfo() (*Server, error) } type Server struct { diff --git a/features/index/handler.go b/features/index/handler.go deleted file mode 100644 index f93cd8c..0000000 --- a/features/index/handler.go +++ /dev/null @@ -1,12 +0,0 @@ -package index - -import ( - "html/template" - "net/http" -) - -func IndexGUIHandler(t template.Template) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - t.Lookup("index.html").Execute(w, nil) - }) -} diff --git a/features/web/index/handler.go b/features/web/index/handler.go new file mode 100644 index 0000000..23913b4 --- /dev/null +++ b/features/web/index/handler.go @@ -0,0 +1,32 @@ +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.Channels() + 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 new file mode 100644 index 0000000..421decc --- /dev/null +++ b/features/web/index/page.go @@ -0,0 +1,34 @@ +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) +} + +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/index/routes.go b/features/web/index/routes.go similarity index 54% rename from features/index/routes.go rename to features/web/index/routes.go index 837c53e..d14aec2 100644 --- a/features/index/routes.go +++ b/features/web/index/routes.go @@ -6,10 +6,10 @@ import ( "github.com/go-chi/chi" ) -func GUIRoutes(t template.Template) *chi.Mux { +func GUIRoutes(s Service, t template.Template) *chi.Mux { router := chi.NewRouter() - router.Get("/", IndexGUIHandler(t)) + router.Get("/", IndexGUIHandler(s, t)) return router } diff --git a/features/web/weberror/page.go b/features/web/weberror/page.go new file mode 100644 index 0000000..5bfa55a --- /dev/null +++ b/features/web/weberror/page.go @@ -0,0 +1,22 @@ +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/main.go b/main.go index 65df4ee..983a777 100644 --- a/main.go +++ b/main.go @@ -7,10 +7,10 @@ import ( "time" "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/index" - "git.cliffbreak.de/Cliffbreak/tsviewer/features/server" + "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" @@ -34,7 +34,7 @@ func Routes(s service.Service, t template.Template) *chi.Mux { }) router.Route("/", func(r chi.Router) { - r.Mount("/", index.GUIRoutes(t)) + r.Mount("/", index.GUIRoutes(s, t)) }) return router @@ -58,6 +58,9 @@ func main() { } 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.Fatal("Handler: ", http.ListenAndServe(":8080", router)) } diff --git a/service/channel.go b/service/channel.go index b55cffd..1631148 100644 --- a/service/channel.go +++ b/service/channel.go @@ -3,7 +3,7 @@ package service import ( "errors" - "git.cliffbreak.de/Cliffbreak/tsviewer/features/channel" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/api/channel" "github.com/multiplay/go-ts3" ) diff --git a/service/client.go b/service/client.go index e6548d8..be4f992 100644 --- a/service/client.go +++ b/service/client.go @@ -3,7 +3,7 @@ package service import ( "errors" - "git.cliffbreak.de/Cliffbreak/tsviewer/features/client" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/api/client" "github.com/multiplay/go-ts3" ) diff --git a/service/server.go b/service/server.go index ce88c49..3ee9148 100644 --- a/service/server.go +++ b/service/server.go @@ -3,10 +3,10 @@ package service import ( "time" - "git.cliffbreak.de/Cliffbreak/tsviewer/features/server" + "git.cliffbreak.de/Cliffbreak/tsviewer/features/api/server" ) -func (s Service) Info() (*server.Server, error) { +func (s Service) ServerInfo() (*server.Server, error) { serverInfo, err := s.TSClient.Server.Info() if err != nil { return nil, err diff --git a/static/styles.css b/static/styles.css new file mode 100644 index 0000000..b572f0e --- /dev/null +++ b/static/styles.css @@ -0,0 +1,15 @@ +*{ + padding: 0; + margin: 0; +} + +h2{ +} + +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 new file mode 100644 index 0000000..5ff2593 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,11 @@ + + + + Error + + +

Oops, an error occurred!

+ Error {{.StatusCode}} + {{.Error}} + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 9fadccd..2c1a7de 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,10 +3,23 @@ TS3 Viewer + + -

Index page!

+

{{.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