diff --git a/routers/web/pages/pages.go b/routers/web/pages/pages.go index ae4c1b98b2..09847b7322 100644 --- a/routers/web/pages/pages.go +++ b/routers/web/pages/pages.go @@ -70,15 +70,19 @@ func getRepoFromRequest(ctx *context.Context) (*repo_model.Repository, *pages_mo return repo, config, nil } - // Parse subdomain: {repo}.{owner}.pages.{domain} - // This is a simplified implementation + // Parse subdomain: {repo}-{owner}.{domain} parts := strings.Split(host, ".") - if len(parts) < 4 { + if len(parts) < 2 { return nil, nil, errors.New("invalid pages subdomain") } - repoName := parts[0] - ownerName := parts[1] + // First part is {repo}-{owner} + repoOwner := strings.SplitN(parts[0], "-", 2) + if len(repoOwner) != 2 { + return nil, nil, errors.New("invalid pages subdomain format") + } + repoName := repoOwner[0] + ownerName := repoOwner[1] repo, err = repo_model.GetRepositoryByOwnerAndName(ctx, ownerName, repoName) if err != nil { diff --git a/routers/web/web.go b/routers/web/web.go index 67368873db..7e5ac5173a 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -302,6 +302,33 @@ var optSignInFromAnyOrigin = verifyAuthWithOptions(&common.VerifyOptions{Disable // registerWebRoutes register routes func registerWebRoutes(m *web.Router) { + // Check for Pages subdomain requests first + m.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + host := req.Host + // Remove port if present + if idx := strings.Index(host, ":"); idx > 0 { + host = host[:idx] + } + // Check if this is a subdomain of our main domain + mainDomain := setting.Domain + if strings.HasSuffix(host, "."+mainDomain) { + subdomain := strings.TrimSuffix(host, "."+mainDomain) + // Skip known subdomains + if subdomain != "" && subdomain != "www" && subdomain != "api" && subdomain != "git" && strings.Contains(subdomain, "-") { + // This looks like a Pages subdomain ({repo}-{owner}) + ctx := context.GetWebContext(req.Context()) + if ctx != nil { + log.Trace("Pages subdomain request: %s", host) + pages.ServeLandingPage(ctx) + return + } + } + } + next.ServeHTTP(w, req) + }) + }) + // required to be signed in or signed out reqSignIn := verifyAuthWithOptions(&common.VerifyOptions{SignInRequired: true}) reqSignOut := verifyAuthWithOptions(&common.VerifyOptions{SignOutRequired: true}) diff --git a/services/pages/pages.go b/services/pages/pages.go index 8e7a4b1744..9c7d6e7a92 100644 --- a/services/pages/pages.go +++ b/services/pages/pages.go @@ -44,6 +44,10 @@ func GetPagesConfig(ctx context.Context, repo *repo_model.Repository) (*pages_mo } return &config, nil } + // If Pages is enabled but no config file, return a default config + if dbConfig != nil && dbConfig.Enabled { + return getDefaultConfig(repo, string(dbConfig.Template)), nil + } return nil, err } @@ -93,6 +97,26 @@ func GetPagesConfig(ctx context.Context, repo *repo_model.Repository) (*pages_mo } // loadConfigFromRepo loads the landing.yaml configuration from the repository + + +// getDefaultConfig returns a default landing page configuration +func getDefaultConfig(repo *repo_model.Repository, template string) *pages_module.LandingConfig { + if template == "" { + template = "simple" + } + return &pages_module.LandingConfig{ + Enabled: true, + Template: template, + Hero: pages_module.HeroConfig{ + Title: repo.Name, + Tagline: repo.Description, + }, + Branding: pages_module.BrandingConfig{ + PrimaryColor: "#4183c4", + }, + } +} + func loadConfigFromRepo(ctx context.Context, repo *repo_model.Repository) (*pages_module.LandingConfig, string, error) { gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) if err != nil {