From 171e8128a409771c4a555da87b19885d6ef424e7 Mon Sep 17 00:00:00 2001 From: Hendrik Schlehlein Date: Wed, 16 Jan 2019 22:01:48 +0100 Subject: [PATCH] added server info --- README.md | 2 +- features/channel/handler.go | 4 ++-- features/channel/routes.go | 6 +++--- features/client/handler.go | 4 ++-- features/client/routes.go | 6 +++--- features/server/handler.go | 20 ++++++++++++++++++++ features/server/routes.go | 13 +++++++++++++ features/server/server.go | 22 ++++++++++++++++++++++ main.go | 6 ++++-- service/server.go | 29 +++++++++++++++++++++++++++++ 10 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 features/server/handler.go create mode 100644 features/server/routes.go create mode 100644 features/server/server.go create mode 100644 service/server.go diff --git a/README.md b/README.md index 2057cae..746faff 100644 --- a/README.md +++ b/README.md @@ -49,5 +49,5 @@ The config file is generated automatically on first startup. "name": "main1", "totalClients": 0, "neededSubscribePower": 0 -}, +} ``` diff --git a/features/channel/handler.go b/features/channel/handler.go index a05ff77..d0e3be8 100644 --- a/features/channel/handler.go +++ b/features/channel/handler.go @@ -8,7 +8,7 @@ import ( "github.com/go-chi/chi" ) -func ChannelHandler(s Service) http.HandlerFunc { +func ChannelAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { response.Handler(w, response.HandlerFunc(func() (int, error) { id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) @@ -26,7 +26,7 @@ func ChannelHandler(s Service) http.HandlerFunc { }) } -func ChannelsHandler(s Service) http.HandlerFunc { +func ChannelsAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { response.Handler(w, response.HandlerFunc(func() (int, error) { cc, err := s.Channels() diff --git a/features/channel/routes.go b/features/channel/routes.go index ddf6ba0..02532d8 100644 --- a/features/channel/routes.go +++ b/features/channel/routes.go @@ -4,11 +4,11 @@ import ( "github.com/go-chi/chi" ) -func Routes(s Service) *chi.Mux { +func APIRoutes(s Service) *chi.Mux { router := chi.NewRouter() - router.Get("/{id}", ChannelHandler(s)) - router.Get("/", ChannelsHandler(s)) + router.Get("/{id}", ChannelAPIHandler(s)) + router.Get("/", ChannelsAPIHandler(s)) return router } diff --git a/features/client/handler.go b/features/client/handler.go index 3578061..56b036a 100644 --- a/features/client/handler.go +++ b/features/client/handler.go @@ -8,7 +8,7 @@ import ( "github.com/go-chi/chi" ) -func ClientHandler(s Service) http.HandlerFunc { +func ClientAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { response.Handler(w, response.HandlerFunc(func() (int, error) { id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) @@ -26,7 +26,7 @@ func ClientHandler(s Service) http.HandlerFunc { }) } -func ClientsHandler(s Service) http.HandlerFunc { +func ClientsAPIHandler(s Service) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { response.Handler(w, response.HandlerFunc(func() (int, error) { cc, err := s.Clients() diff --git a/features/client/routes.go b/features/client/routes.go index a34c596..9e20d55 100644 --- a/features/client/routes.go +++ b/features/client/routes.go @@ -4,11 +4,11 @@ import ( "github.com/go-chi/chi" ) -func Routes(s Service) *chi.Mux { +func APIRoutes(s Service) *chi.Mux { router := chi.NewRouter() - router.Get("/{id}", ClientHandler(s)) - router.Get("/", ClientsHandler(s)) + router.Get("/{id}", ClientAPIHandler(s)) + router.Get("/", ClientsAPIHandler(s)) return router } diff --git a/features/server/handler.go b/features/server/handler.go new file mode 100644 index 0000000..6ed9303 --- /dev/null +++ b/features/server/handler.go @@ -0,0 +1,20 @@ +package server + +import ( + "net/http" + + "git.cliffbreak.de/haveachin/go-tsviewer/response" +) + +func InfoAPIHandler(s Service) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + response.Handler(w, response.HandlerFunc(func() (int, error) { + s, err := s.Info() + if err != nil { + return http.StatusNotFound, err + } + + return response.New(s, r).Send(w, http.StatusOK) + })) + }) +} diff --git a/features/server/routes.go b/features/server/routes.go new file mode 100644 index 0000000..22342f3 --- /dev/null +++ b/features/server/routes.go @@ -0,0 +1,13 @@ +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/server/server.go b/features/server/server.go new file mode 100644 index 0000000..401614f --- /dev/null +++ b/features/server/server.go @@ -0,0 +1,22 @@ +package server + +import "time" + +type Service interface { + Info() (*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/main.go b/main.go index 836267f..c8e02de 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "git.cliffbreak.de/haveachin/go-tsviewer/config" "git.cliffbreak.de/haveachin/go-tsviewer/features/channel" "git.cliffbreak.de/haveachin/go-tsviewer/features/client" + "git.cliffbreak.de/haveachin/go-tsviewer/features/server" "git.cliffbreak.de/haveachin/go-tsviewer/service" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" @@ -24,8 +25,9 @@ func Routes(s service.Service) *chi.Mux { ) router.Route("/v1", func(r chi.Router) { - r.Mount("/channels", channel.Routes(s)) - r.Mount("/clients", client.Routes(s)) + r.Mount("/channels", channel.APIRoutes(s)) + r.Mount("/clients", client.APIRoutes(s)) + r.Mount("/server", server.APIRoutes(s)) }) return router diff --git a/service/server.go b/service/server.go new file mode 100644 index 0000000..d5f2d4a --- /dev/null +++ b/service/server.go @@ -0,0 +1,29 @@ +package service + +import ( + "time" + + "git.cliffbreak.de/haveachin/go-tsviewer/features/server" +) + +func (s Service) Info() (*server.Server, error) { + serverInfo, err := s.TSClient.Server.Info() + if err != nil { + return nil, err + } + + return &server.Server{ + Name: serverInfo.Name, + Status: serverInfo.Status, + Version: serverInfo.Version, + WelcomeMessage: serverInfo.WelcomeMessage, + MaxClients: serverInfo.MaxClients, + ClientsOnline: serverInfo.ClientsOnline, + ReservedSlots: serverInfo.ReservedSlots, + Uptime: time.Duration(serverInfo.Uptime) * time.Nanosecond, + TotalPing: serverInfo.TotalPing, + MinAndroidVersion: serverInfo.MinAndroidVersion, + MinClientVersion: serverInfo.MinClientVersion, + MiniOSVersion: serverInfo.MiniOSVersion, + }, nil +}