I tried so hard

This commit is contained in:
Hendrik Schlehlein 2019-01-21 18:53:33 +01:00
parent 872dd200e1
commit 9e2d7e9acc
21 changed files with 144 additions and 26 deletions

View file

@ -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
}

View file

@ -3,7 +3,7 @@ package server
import "time"
type Service interface {
Info() (*Server, error)
ServerInfo() (*Server, error)
}
type Server struct {

View file

@ -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)
})
}

View file

@ -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)
})
}

View file

@ -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)
}

View file

@ -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
}

View file

@ -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)
}

13
main.go
View file

@ -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))
}

View file

@ -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"
)

View file

@ -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"
)

View file

@ -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

15
static/styles.css Normal file
View file

@ -0,0 +1,15 @@
*{
padding: 0;
margin: 0;
}
h2{
}
small{
margin-left: 10px;
}
p.client{
margin-left: 10px;
}

11
templates/error.html Normal file
View file

@ -0,0 +1,11 @@
<!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

@ -3,10 +3,23 @@
<head>
<title>TS3 Viewer</title>
<link rel="stylesheet" href="static/styles.css">
</head>
<body>
<h1>Index page!</h1>
<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}}
</body>
</html>