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
|
||||
}
|
||||
|
||||
var version = "0.1.0"
|
||||
|
||||
func openDB(cfg config) (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite3", cfg.db.dsn)
|
||||
if err != nil {
|
||||
|
||||
@@ -11,6 +11,7 @@ func (app *application) routes() http.Handler {
|
||||
|
||||
router.HandlerFunc(http.MethodGet, "/", app.ShowText)
|
||||
router.HandlerFunc(http.MethodGet, "/v1/when", app.WhenNextSession)
|
||||
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sync"
|
||||
"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/types"
|
||||
)
|
||||
@@ -73,21 +74,21 @@ func (foundry *FoundryApi) ServeWebSocket() error {
|
||||
}
|
||||
case types.RespServerChangeCode:
|
||||
foundry.IsAvailable = true
|
||||
// data, err := actions.SplitToTypeAndData([]byte(message.MsgJson))
|
||||
// if err != nil {
|
||||
// wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
||||
// continue
|
||||
// }
|
||||
data, err := actions.SplitToTypeAndData([]byte(message.MsgJson))
|
||||
if err != nil {
|
||||
wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
||||
continue
|
||||
}
|
||||
|
||||
// if data == nil {
|
||||
// continue
|
||||
// }
|
||||
if data == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// err = data.Action(foundry.transport, &foundry.Status)
|
||||
// if err != nil {
|
||||
// wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
||||
// continue
|
||||
// }
|
||||
err = data.Action(foundry.transport, &foundry.Status)
|
||||
if err != nil {
|
||||
wsChannels.Err() <- &types.FoundryError{Direction: types.ReaderCode, Err: err, Type: types.WebSocketCode}
|
||||
continue
|
||||
}
|
||||
case types.RespDataCode:
|
||||
foundry.transport.ExchangeChan.Msgs[message.Id] = make(chan []byte, 1)
|
||||
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"
|
||||
|
||||
type FoundryStatus struct {
|
||||
IsActive bool
|
||||
Version string
|
||||
World string
|
||||
System string
|
||||
SystemVersion string
|
||||
Users int
|
||||
Uptime int64
|
||||
IsActive bool `json:"on_world"`
|
||||
Version string `json:"version"`
|
||||
World string `json:"world,omitempty"`
|
||||
System string `json:"system,omitempty"`
|
||||
SystemVersion string `json:"-"`
|
||||
Users int `json:"-"`
|
||||
Uptime int64 `json:"-"`
|
||||
}
|
||||
|
||||
func NewFoundryStatus(status *json.Status) *FoundryStatus {
|
||||
|
||||
Reference in New Issue
Block a user