feat: auth, site creation with PHP version selection
This commit is contained in:
@@ -8,19 +8,35 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/panelhosting/panel/internal/authsvc"
|
||||
"github.com/panelhosting/panel/internal/bootstrap"
|
||||
"github.com/panelhosting/panel/internal/config"
|
||||
"github.com/panelhosting/panel/internal/handler"
|
||||
"github.com/panelhosting/panel/internal/middleware"
|
||||
"github.com/panelhosting/panel/internal/repository"
|
||||
"github.com/panelhosting/panel/internal/setup"
|
||||
"github.com/panelhosting/panel/internal/sitesvc"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
httpServer *http.Server
|
||||
}
|
||||
|
||||
func New(pool *pgxpool.Pool, addr string) (*Server, error) {
|
||||
func New(pool *pgxpool.Pool, cfg *config.Config) (*Server, error) {
|
||||
users := repository.NewUserRepository(pool)
|
||||
setupSvc := setup.NewService(users)
|
||||
servers := repository.NewServerRepository(pool)
|
||||
sessions := repository.NewSessionRepository(pool)
|
||||
sites := repository.NewSiteRepository(pool)
|
||||
phpVers := repository.NewPHPVersionRepository(pool)
|
||||
|
||||
setupSvc := setup.NewService(users, servers, cfg)
|
||||
authSvc := authsvc.NewService(users, sessions, cfg.SessionTTLHours)
|
||||
siteSvc := sitesvc.NewService(sites, servers, phpVers)
|
||||
|
||||
setupHandler := handler.NewSetupHandler(setupSvc)
|
||||
authHandler := handler.NewAuthHandler(authSvc, cfg)
|
||||
siteHandler := handler.NewSiteHandler(siteSvc)
|
||||
authMW := middleware.NewAuth(authSvc, cfg)
|
||||
|
||||
setupRequired, err := setupSvc.Status(context.Background())
|
||||
if err != nil {
|
||||
@@ -30,12 +46,27 @@ func New(pool *pgxpool.Pool, addr string) (*Server, error) {
|
||||
log.Println("setup required: open / to create first admin")
|
||||
}
|
||||
|
||||
if err := bootstrap.EnsureDefaultServer(context.Background(), servers, cfg); err != nil {
|
||||
return nil, fmt.Errorf("bootstrap server: %w", err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /health", handler.Health)
|
||||
|
||||
mux.HandleFunc("GET /api/v1/setup/status", setupHandler.Status)
|
||||
mux.HandleFunc("POST /api/v1/setup/register", setupHandler.Register)
|
||||
mux.HandleFunc("GET /{$}", handler.IndexPage(setupSvc))
|
||||
|
||||
mux.HandleFunc("POST /api/v1/auth/login", authHandler.Login)
|
||||
mux.HandleFunc("POST /api/v1/auth/logout", authMW.Require(authHandler.Logout))
|
||||
mux.HandleFunc("GET /api/v1/auth/me", authMW.Require(authHandler.Me))
|
||||
|
||||
mux.HandleFunc("GET /api/v1/php-versions", siteHandler.PHPVersions)
|
||||
mux.HandleFunc("GET /api/v1/sites", authMW.Require(siteHandler.List))
|
||||
mux.HandleFunc("POST /api/v1/sites", authMW.Require(siteHandler.Create))
|
||||
|
||||
mux.HandleFunc("GET /{$}", authMW.Optional(handler.IndexPage(setupSvc)))
|
||||
|
||||
addr := fmt.Sprintf(":%s", cfg.HTTPPort)
|
||||
return &Server{
|
||||
httpServer: &http.Server{
|
||||
Addr: addr,
|
||||
|
||||
Reference in New Issue
Block a user