@@ -0,0 +1,87 @@
|
||||
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"
|
||||
identityrepo "veyra/services/api/internal/identity/repository"
|
||||
identityservice "veyra/services/api/internal/identity/service"
|
||||
"veyra/services/api/internal/operations/service"
|
||||
"veyra/services/api/internal/platform/config"
|
||||
"veyra/services/api/internal/platform/ratelimit"
|
||||
portfoliorepo "veyra/services/api/internal/portfolio/repository"
|
||||
portfolioservice "veyra/services/api/internal/portfolio/service"
|
||||
transport "veyra/services/api/internal/transport/http"
|
||||
)
|
||||
|
||||
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 identityservice.Repository
|
||||
var accountRepo portfolioservice.Repository
|
||||
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 = identityrepo.NewPostgres(pool)
|
||||
accountRepo = portfoliorepo.NewPostgres(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 := identityservice.NewAuthService(authRepo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
identityOptions := transport.IdentityOptions{Auth: auth, Accounts: portfolioservice.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: transport.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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"veyra/services/api/internal/platform/config"
|
||||
"veyra/services/api/internal/platform/database"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
slog.Error("database command failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
envFile := flag.String("env-file", "", "optional environment file")
|
||||
action := flag.String("action", "migrate", "create or migrate")
|
||||
flag.Parse()
|
||||
if *envFile != "" {
|
||||
if err := config.LoadEnvFile(*envFile); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
c, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
switch *action {
|
||||
case "create":
|
||||
created, err := database.Create(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slog.Info("database ready", "database", c.PGDatabase, "created", created)
|
||||
case "migrate":
|
||||
pool, err := database.Open(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := database.Migrate(ctx, pool); err != nil {
|
||||
return err
|
||||
}
|
||||
slog.Info("migrations applied", "database", c.PGDatabase)
|
||||
default:
|
||||
return fmt.Errorf("action must be create or migrate")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Command user provisions development identities without exposing a registration API.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"veyra/services/api/internal/identity/repository"
|
||||
"veyra/services/api/internal/identity/service"
|
||||
"veyra/services/api/internal/platform/config"
|
||||
"veyra/services/api/internal/platform/database"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
slog.Error("provision identity failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
func run() error {
|
||||
envFile := flag.String("env-file", "", "environment file")
|
||||
role := flag.String("role", "ADMIN", "ADMIN or USER")
|
||||
flag.Parse()
|
||||
if *envFile != "" {
|
||||
if err := config.LoadEnvFile(*envFile); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
c, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c.Environment != "local" && c.Environment != "test" {
|
||||
return fmt.Errorf("development provisioning is disabled outside local/test environments")
|
||||
}
|
||||
var login, password, name string
|
||||
switch *role {
|
||||
case "ADMIN":
|
||||
login = os.Getenv("ADMIN_LOGIN")
|
||||
password = os.Getenv("ADMIN_PASSWORD")
|
||||
name = "管理员"
|
||||
case "USER":
|
||||
login = os.Getenv("DEV_USER_LOGIN")
|
||||
password = os.Getenv("DEV_USER_PASSWORD")
|
||||
name = "开发测试用户"
|
||||
default:
|
||||
return fmt.Errorf("role must be ADMIN or USER")
|
||||
}
|
||||
if login == "" || password == "" {
|
||||
return fmt.Errorf("configure the selected identity login and password in the local environment file")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
pool, err := database.Open(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
auth, err := service.NewAuthService(repository.NewPostgres(pool))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user, err := auth.ProvisionUser(ctx, login, password, name, *role)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slog.Info("identity provisioned", "user_id", user.ID, "role", *role)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user