added server info

This commit is contained in:
Hendrik Schlehlein 2019-01-16 22:01:48 +01:00
parent e99d11dfbc
commit 171e8128a4
10 changed files with 99 additions and 13 deletions

View file

@ -49,5 +49,5 @@ The config file is generated automatically on first startup.
"name": "main1", "name": "main1",
"totalClients": 0, "totalClients": 0,
"neededSubscribePower": 0 "neededSubscribePower": 0
}, }
``` ```

View file

@ -8,7 +8,7 @@ import (
"github.com/go-chi/chi" "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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.Handler(w, response.HandlerFunc(func() (int, error) { response.Handler(w, response.HandlerFunc(func() (int, error) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.Handler(w, response.HandlerFunc(func() (int, error) { response.Handler(w, response.HandlerFunc(func() (int, error) {
cc, err := s.Channels() cc, err := s.Channels()

View file

@ -4,11 +4,11 @@ import (
"github.com/go-chi/chi" "github.com/go-chi/chi"
) )
func Routes(s Service) *chi.Mux { func APIRoutes(s Service) *chi.Mux {
router := chi.NewRouter() router := chi.NewRouter()
router.Get("/{id}", ChannelHandler(s)) router.Get("/{id}", ChannelAPIHandler(s))
router.Get("/", ChannelsHandler(s)) router.Get("/", ChannelsAPIHandler(s))
return router return router
} }

View file

@ -8,7 +8,7 @@ import (
"github.com/go-chi/chi" "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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.Handler(w, response.HandlerFunc(func() (int, error) { response.Handler(w, response.HandlerFunc(func() (int, error) {
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.Handler(w, response.HandlerFunc(func() (int, error) { response.Handler(w, response.HandlerFunc(func() (int, error) {
cc, err := s.Clients() cc, err := s.Clients()

View file

@ -4,11 +4,11 @@ import (
"github.com/go-chi/chi" "github.com/go-chi/chi"
) )
func Routes(s Service) *chi.Mux { func APIRoutes(s Service) *chi.Mux {
router := chi.NewRouter() router := chi.NewRouter()
router.Get("/{id}", ClientHandler(s)) router.Get("/{id}", ClientAPIHandler(s))
router.Get("/", ClientsHandler(s)) router.Get("/", ClientsAPIHandler(s))
return router return router
} }

View file

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

13
features/server/routes.go Normal file
View file

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

22
features/server/server.go Normal file
View file

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

View file

@ -8,6 +8,7 @@ import (
"git.cliffbreak.de/haveachin/go-tsviewer/config" "git.cliffbreak.de/haveachin/go-tsviewer/config"
"git.cliffbreak.de/haveachin/go-tsviewer/features/channel" "git.cliffbreak.de/haveachin/go-tsviewer/features/channel"
"git.cliffbreak.de/haveachin/go-tsviewer/features/client" "git.cliffbreak.de/haveachin/go-tsviewer/features/client"
"git.cliffbreak.de/haveachin/go-tsviewer/features/server"
"git.cliffbreak.de/haveachin/go-tsviewer/service" "git.cliffbreak.de/haveachin/go-tsviewer/service"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
@ -24,8 +25,9 @@ func Routes(s service.Service) *chi.Mux {
) )
router.Route("/v1", func(r chi.Router) { router.Route("/v1", func(r chi.Router) {
r.Mount("/channels", channel.Routes(s)) r.Mount("/channels", channel.APIRoutes(s))
r.Mount("/clients", client.Routes(s)) r.Mount("/clients", client.APIRoutes(s))
r.Mount("/server", server.APIRoutes(s))
}) })
return router return router

29
service/server.go Normal file
View file

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