2024-09-22 18:46:25 +05:30
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2024-10-04 07:07:39 +05:30
|
|
|
"time"
|
2024-09-22 18:46:25 +05:30
|
|
|
|
|
|
|
"github.com/etherlabsio/healthcheck/v2"
|
2024-10-04 07:07:39 +05:30
|
|
|
"github.com/getsentry/sentry-go"
|
2024-09-22 18:46:25 +05:30
|
|
|
sentrygin "github.com/getsentry/sentry-go/gin"
|
2024-10-14 17:12:12 +05:30
|
|
|
"github.com/gin-contrib/pprof"
|
2024-09-22 18:46:25 +05:30
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/multierr"
|
|
|
|
|
2024-10-01 05:19:23 +05:30
|
|
|
db "ely.by/accounts-profiles-endpoint/internal/db/mysql"
|
|
|
|
"ely.by/accounts-profiles-endpoint/internal/http"
|
2024-10-04 07:07:39 +05:30
|
|
|
sentryLogging "ely.by/accounts-profiles-endpoint/internal/logging/sentry"
|
2024-10-01 05:19:23 +05:30
|
|
|
"ely.by/accounts-profiles-endpoint/internal/services/chrly"
|
|
|
|
"ely.by/accounts-profiles-endpoint/internal/services/signer"
|
2024-09-22 18:46:25 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func Serve() error {
|
|
|
|
config := initConfig()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, _ = signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM, os.Kill)
|
|
|
|
|
|
|
|
var errors, err error
|
2024-10-04 07:07:39 +05:30
|
|
|
err = sentryLogging.InitWithConfig(config)
|
2024-09-22 18:46:25 +05:30
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to initialize Sentry: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql, err := db.NewWithConfig(config)
|
|
|
|
errors = multierr.Append(errors, err)
|
|
|
|
|
|
|
|
texturesProvider, err := chrly.NewWithConfig(config)
|
|
|
|
errors = multierr.Append(errors, err)
|
|
|
|
|
|
|
|
signerService, err := signer.NewWithConfig(config)
|
|
|
|
errors = multierr.Append(errors, err)
|
|
|
|
|
|
|
|
if errors != nil {
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.GetBool("debug") {
|
|
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
} else {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := gin.Default()
|
2024-10-14 17:12:12 +05:30
|
|
|
pprof.Register(r)
|
2024-09-22 18:46:25 +05:30
|
|
|
r.Use(sentrygin.New(sentrygin.Options{Repanic: true}))
|
2024-10-04 07:07:39 +05:30
|
|
|
r.Use(sentryLogging.ErrorMiddleware())
|
2024-09-22 18:46:25 +05:30
|
|
|
r.Use(http.ErrorMiddleware())
|
|
|
|
|
|
|
|
r.GET("/healthcheck", gin.WrapH(healthcheck.Handler(
|
|
|
|
healthcheck.WithChecker("mysql", healthcheck.CheckerFunc(mysql.Ping)),
|
|
|
|
)))
|
|
|
|
|
|
|
|
sessionserver := http.NewMojangApi(mysql, texturesProvider, signerService)
|
|
|
|
sessionserver.DefineRoutes(r)
|
|
|
|
|
|
|
|
server, err := http.NewServerWithConfig(config, r)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create a server: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = http.StartServer(ctx, server)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start a server: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-10-04 07:07:39 +05:30
|
|
|
sentry.Flush(2 * time.Second)
|
|
|
|
|
2024-09-22 18:46:25 +05:30
|
|
|
return nil
|
|
|
|
}
|