2fas-server/internal/api/health/ports/http.go

138 lines
3.5 KiB
Go
Raw Normal View History

2022-12-31 10:22:38 +01:00
package ports
import (
"context"
"encoding/json"
"os"
"time"
2022-12-31 10:22:38 +01:00
"github.com/gin-gonic/gin"
2024-01-02 09:48:34 +01:00
"github.com/redis/go-redis/v9"
2023-01-30 19:59:42 +01:00
"github.com/twofas/2fas-server/config"
mobile "github.com/twofas/2fas-server/internal/api/mobile/domain"
support "github.com/twofas/2fas-server/internal/api/support/domain"
"github.com/twofas/2fas-server/internal/common/aws"
"github.com/twofas/2fas-server/internal/common/clock"
"github.com/twofas/2fas-server/internal/common/logging"
2022-12-31 10:22:38 +01:00
)
type RoutesHandler struct {
applicationName string
redis *redis.Client
2022-12-31 10:22:38 +01:00
}
func NewRoutesHandler(applicationName string, redis *redis.Client) *RoutesHandler {
2022-12-31 10:22:38 +01:00
return &RoutesHandler{
applicationName: applicationName,
redis: redis,
2022-12-31 10:22:38 +01:00
}
}
func (r *RoutesHandler) CheckApplicationHealth(c *gin.Context) {
c.JSON(200, gin.H{})
}
func (r *RoutesHandler) FakeError(c *gin.Context) {
logging.WithFields(logging.Fields{
"context_field_1": "some value",
"context_field_2": "another value",
}).Error("Standard fake error")
messageStruct := &struct {
Ctx1 string `json:"ctx_1"`
Ctx2 string `json:"ctx_2"`
Message string `json:"message"`
}{
Ctx1: "msg json key 1",
Ctx2: "msg json key 2",
Message: "Fake error with message as JSON",
}
message, _ := json.Marshal(messageStruct)
logging.Error(string(message))
c.JSON(500, gin.H{})
}
func (r *RoutesHandler) FakeWarning(c *gin.Context) {
logging.WithFields(logging.Fields{
"context_field_1": "some value",
"context_field_2": "another value",
}).Warning("Fake warning")
c.JSON(200, gin.H{})
}
func (r *RoutesHandler) FakeSecurityWarning(c *gin.Context) {
logging.WithFields(logging.Fields{
"type": "security",
"ip": c.ClientIP(),
"uri": c.Request.URL.String(),
}).Warning("Fake warning")
c.JSON(200, gin.H{})
}
type configuration struct {
Common config.Configuration `json:"common"`
DebugLogsConfiguration support.DebugLogsConfig `json:"mobile_debug"`
PushConfiguration *mobile.FcmPushConfig `json:"push"`
}
type redisStatus struct {
Addr string `json:"addr"`
Db int `json:"db"`
Connection string `json:"connection"`
}
type systemInfo struct {
ApplicationName string `json:"application_name"`
LocalTime string `json:"local_time"`
Config configuration `json:"configuration"`
Environment []string `json:"environment"`
Redis redisStatus `json:"redis"`
2022-12-31 10:22:38 +01:00
}
func (r *RoutesHandler) RedisInfo(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
res := r.redis.Info(ctx)
c.String(200, res.String())
}
func (r *RoutesHandler) GetApplicationConfiguration(c *gin.Context) {
s3 := aws.NewAwsS3(config.Config.Aws.Region, config.Config.Aws.S3AccessKeyId, config.Config.Aws.S3AccessSecretKey)
pushConfig := mobile.NewFcmPushConfig(s3)
debugLogsConfig := support.LoadDebugLogsConfig()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
res := r.redis.Info(ctx)
var info redisStatus
if res.Err() != nil {
info = redisStatus{Connection: res.Err().Error()}
} else {
info = redisStatus{Connection: "OK"}
}
info.Addr = r.redis.Options().Addr
info.Db = r.redis.Options().DB
c.JSON(200, &systemInfo{
LocalTime: clock.New().Now().String(),
Config: configuration{
Common: config.Config,
DebugLogsConfiguration: debugLogsConfig,
PushConfiguration: pushConfig,
},
Environment: os.Environ(),
Redis: info,
})
}