86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"git.sundynix.cn/Blizzard/Veyra/internal/config"
|
|
"git.sundynix.cn/Blizzard/Veyra/internal/handler"
|
|
"git.sundynix.cn/Blizzard/Veyra/internal/ratelimit"
|
|
"git.sundynix.cn/Blizzard/Veyra/internal/repository"
|
|
"git.sundynix.cn/Blizzard/Veyra/internal/service"
|
|
)
|
|
|
|
func main() {
|
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
|
if err := run(logger); err != nil {
|
|
logger.Error("API stopped", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run(logger *slog.Logger) error {
|
|
envFile := flag.String("env-file", "", "optional environment file")
|
|
flag.Parse()
|
|
if *envFile != "" {
|
|
if err := config.LoadEnvFile(*envFile); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
c, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var postgres service.Probe
|
|
var authRepo service.AuthRepository
|
|
var accountRepo service.AccountRepository
|
|
if c.PGUser != "" {
|
|
if err := c.ValidateDatabase(); err != nil {
|
|
return err
|
|
}
|
|
pool, err := pgxpool.New(context.Background(), c.DatabaseURL(c.PGDatabase))
|
|
if err != nil {
|
|
return errors.New("invalid PostgreSQL configuration")
|
|
}
|
|
defer pool.Close()
|
|
postgres = pool.Ping
|
|
authRepo = repository.NewIdentityRepository(pool)
|
|
accountRepo = repository.NewAccountRepository(pool)
|
|
}
|
|
redisClient := redis.NewClient(&redis.Options{Addr: c.RedisAddr, Username: c.RedisUsername, Password: c.RedisPassword, DB: c.RedisDB, DialTimeout: time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, MaxRetries: -1, ContextTimeoutEnabled: true})
|
|
defer redisClient.Close()
|
|
health := service.NewHealthService(postgres, func(ctx context.Context) error { return redisClient.Ping(ctx).Err() })
|
|
auth, err := service.NewAuthService(authRepo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
identityOptions := handler.IdentityOptions{Auth: auth, Accounts: service.NewAccountService(accountRepo), Limiter: ratelimit.NewRedis(redisClient), DevelopmentLogin: c.Environment == "local" || c.Environment == "test", SecureCookie: c.Environment != "local" && c.Environment != "test"}
|
|
server := &http.Server{Addr: c.HTTPAddr, Handler: handler.NewRouter(health, logger, identityOptions), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 1 << 20}
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
result := make(chan error, 1)
|
|
go func() { result <- server.ListenAndServe() }()
|
|
logger.Info("API starting", "address", c.HTTPAddr, "environment", c.Environment)
|
|
select {
|
|
case err := <-result:
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
return nil
|
|
}
|
|
return err
|
|
case <-ctx.Done():
|
|
shutdown, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
return server.Shutdown(shutdown)
|
|
}
|
|
}
|