add healthcheck route
This commit is contained in:
24
cmd/api/errors.go
Normal file
24
cmd/api/errors.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func (app *application) logError(r *http.Request, err error) {
|
||||||
|
app.slogger.Error(err.Error(), "request_method", r.Method, "request_url", r.URL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message any) {
|
||||||
|
env := envelope{"error": message}
|
||||||
|
|
||||||
|
err := app.writeJSON(w, status, env, nil)
|
||||||
|
if err != nil {
|
||||||
|
app.logError(r, err)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *application) serverErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
app.logError(r, err)
|
||||||
|
|
||||||
|
message := "the server encountered a problem and could not process your request"
|
||||||
|
app.errorResponse(w, r, http.StatusInternalServerError, message)
|
||||||
|
}
|
||||||
21
cmd/api/healthcheck.go
Normal file
21
cmd/api/healthcheck.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
env := envelope{
|
||||||
|
"status": "available",
|
||||||
|
"system_info": map[string]string{
|
||||||
|
"environment": app.cfg.env,
|
||||||
|
"version": version,
|
||||||
|
},
|
||||||
|
"foundry": map[string]any{
|
||||||
|
"status": app.foundryApp.Status,
|
||||||
|
"is_available": app.foundryApp.IsAvailable,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := app.writeJSON(w, http.StatusOK, env, nil)
|
||||||
|
if err != nil {
|
||||||
|
app.serverErrorResponse(w, r, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
25
cmd/api/helpers.go
Normal file
25
cmd/api/helpers.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type envelope map[string]any
|
||||||
|
|
||||||
|
func (app *application) writeJSON(w http.ResponseWriter, status int, data envelope, headers http.Header) error {
|
||||||
|
js, err := json.MarshalIndent(data, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
js = append(js, '\n')
|
||||||
|
for key, value := range headers {
|
||||||
|
w.Header()[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
w.Write(js)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -44,6 +44,8 @@ type application struct {
|
|||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var version = "0.1.0"
|
||||||
|
|
||||||
func openDB(cfg config) (*sql.DB, error) {
|
func openDB(cfg config) (*sql.DB, error) {
|
||||||
db, err := sql.Open("sqlite3", cfg.db.dsn)
|
db, err := sql.Open("sqlite3", cfg.db.dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ func (app *application) routes() http.Handler {
|
|||||||
|
|
||||||
router.HandlerFunc(http.MethodGet, "/", app.ShowText)
|
router.HandlerFunc(http.MethodGet, "/", app.ShowText)
|
||||||
router.HandlerFunc(http.MethodGet, "/v1/when", app.WhenNextSession)
|
router.HandlerFunc(http.MethodGet, "/v1/when", app.WhenNextSession)
|
||||||
|
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/actions"
|
||||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/transport"
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/transport"
|
||||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
|
||||||
)
|
)
|
||||||
@@ -73,21 +74,21 @@ func (foundry *FoundryApi) ServeWebSocket() error {
|
|||||||
}
|
}
|
||||||
case types.RespServerChangeCode:
|
case types.RespServerChangeCode:
|
||||||
foundry.IsAvailable = true
|
foundry.IsAvailable = true
|
||||||
// data, err := actions.SplitToTypeAndData([]byte(message.MsgJson))
|
data, err := actions.SplitToTypeAndData([]byte(message.MsgJson))
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if data == nil {
|
if data == nil {
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
|
|
||||||
// err = data.Action(foundry.transport, &foundry.Status)
|
err = data.Action(foundry.transport, &foundry.Status)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
case types.RespDataCode:
|
case types.RespDataCode:
|
||||||
foundry.transport.ExchangeChan.Msgs[message.Id] = make(chan []byte, 1)
|
foundry.transport.ExchangeChan.Msgs[message.Id] = make(chan []byte, 1)
|
||||||
foundry.transport.ExchangeChan.Msgs[message.Id] <- []byte(message.MsgJson)
|
foundry.transport.ExchangeChan.Msgs[message.Id] <- []byte(message.MsgJson)
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ package types
|
|||||||
import "gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/json"
|
import "gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/json"
|
||||||
|
|
||||||
type FoundryStatus struct {
|
type FoundryStatus struct {
|
||||||
IsActive bool
|
IsActive bool `json:"on_world"`
|
||||||
Version string
|
Version string `json:"version"`
|
||||||
World string
|
World string `json:"world,omitempty"`
|
||||||
System string
|
System string `json:"system,omitempty"`
|
||||||
SystemVersion string
|
SystemVersion string `json:"-"`
|
||||||
Users int
|
Users int `json:"-"`
|
||||||
Uptime int64
|
Uptime int64 `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFoundryStatus(status *json.Status) *FoundryStatus {
|
func NewFoundryStatus(status *json.Status) *FoundryStatus {
|
||||||
|
|||||||
Reference in New Issue
Block a user