feat: ci/cd

This commit is contained in:
Giovanni Rezcjikov
2026-03-09 12:50:29 +03:00
parent f4e4fb21e3
commit daaa702dad
21 changed files with 585 additions and 267 deletions
+43 -37
View File
@@ -8,7 +8,7 @@ import (
"github.com/dusiburg/kotosoft-id/internal/config"
"github.com/dusiburg/kotosoft-id/internal/db"
"github.com/dusiburg/kotosoft-id/internal/handlers"
apphttp "github.com/dusiburg/kotosoft-id/internal/http"
http "github.com/dusiburg/kotosoft-id/internal/http"
"github.com/dusiburg/kotosoft-id/internal/models"
"github.com/dusiburg/kotosoft-id/internal/repositories"
"github.com/dusiburg/kotosoft-id/internal/services"
@@ -17,64 +17,70 @@ import (
func main() {
cfg, err := config.New()
if err != nil {
log.Fatal(err)
log.Fatalf("config: %v", err)
}
pg, err := db.ConnectPostgres(cfg.Postgres.URL)
postgres, err := db.ConnectPostgres(cfg.Postgres.URL)
if err != nil {
log.Fatal(err)
log.Fatalf("postgres: %v", err)
}
if err := db.RunMigrations(cfg.Postgres.MigrationFilePath, postgres.Pool); err != nil {
log.Fatalf("migrations: %v", err)
}
if err := db.RunMigrations(cfg.Postgres.MigrationFilePath, pg.Pool); err != nil {
log.Fatal(err)
}
rdb, err := db.ConnectRedis(cfg.Redis.URL)
redis, err := db.ConnectRedis(cfg.Redis.URL)
if err != nil {
log.Fatal(err)
log.Fatalf("redis: %v", err)
}
userRepo := repositories.NewUserRepository(pg.Pool)
oauthRepo := repositories.NewOAuthRepository(pg.Pool)
userRepo := repositories.NewUserRepository(postgres.Pool)
oauthRepo := repositories.NewOAuthRepository(postgres.Pool)
ctx := context.Background()
basicClient, err := ensureBasicClient(ctx, cfg, oauthRepo)
if err != nil {
log.Fatal(err)
if err := seedClient(context.Background(), oauthRepo, cfg.Client); err != nil {
log.Fatalf("seed client: %v", err)
}
svc := services.New(userRepo, oauthRepo, rdb.Client, services.TokenConfig{
AccessTTL: cfg.JWT.TTL,
RefreshTTL: cfg.JWT.TTL * 7,
}, basicClient)
svc := services.New(
userRepo,
oauthRepo,
redis.Client,
services.TokenConfig{
AccessTTL: cfg.JWT.TTL,
RefreshTTL: cfg.JWT.TTL * 7,
},
)
limiter := services.NewRateLimiter(redis.Client)
authHandler := handlers.NewAuthHandler(svc)
oauthHandler := handlers.NewOAuthHandler(svc, cfg.Server.FrontendURL)
consentHandler := handlers.NewConsentHandler(svc)
r := apphttp.NewRouter(authHandler, oauthHandler, consentHandler, svc, oauthRepo)
log.Fatal(r.Run(cfg.Server.Addr))
router := http.NewRouter(authHandler, oauthHandler, consentHandler, svc, oauthRepo, limiter)
log.Printf("starting server on %s", cfg.Server.Addr)
if err := router.Run(cfg.Server.Addr); err != nil {
log.Fatalf("server: %v", err)
}
}
func ensureBasicClient(ctx context.Context, cfg *config.Conf, repo *repositories.OAuthRepository) (*models.OAuthClient, error) {
client, err := repo.GetOAuthClientByClientID(ctx, cfg.Client.ID)
if err == nil {
return client, nil
}
hashedSecret, err := services.HashClientSecret(cfg.Client.Secret)
func seedClient(ctx context.Context, repo *repositories.OAuthRepository, cfg config.Client) error {
hashedSecret, err := services.HashClientSecret(cfg.Secret)
if err != nil {
return nil, err
return err
}
return repo.CreateOAuthClient(ctx, models.OAuthClient{
ClientID: cfg.Client.ID,
redirectURIsJSON, _ := json.Marshal(cfg.RedirectURIs)
grantTypesJSON, _ := json.Marshal(cfg.GrantTypes)
scopesJSON, _ := json.Marshal(cfg.Scopes)
return repo.UpsertClient(ctx, models.OAuthClient{
ClientID: cfg.ID,
ClientSecret: hashedSecret,
Name: cfg.Client.Name,
RedirectURIs: json.RawMessage(cfg.Client.RedirectURIsJSON()),
GrantTypes: json.RawMessage(cfg.Client.GrantTypesJSON()),
Scopes: json.RawMessage(cfg.Client.ScopesJSON()),
Name: cfg.Name,
RedirectURIs: redirectURIsJSON,
GrantTypes: grantTypesJSON,
Scopes: scopesJSON,
Trusted: true,
})
}