mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-05-23 23:51:56 +00:00
[breaking-change] Accept multiple index repository patterns.
This commit is contained in:
32
src/auth.go
32
src/auth.go
@@ -147,18 +147,30 @@ func authorizeWildcardMatch(r *http.Request) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if slices.Equal(hostParts[1:], strings.Split(config.Wildcard.Domain, ".")) {
|
||||
if slices.Equal(hostParts[1:], wildcardPattern.Domain) {
|
||||
userName := hostParts[0]
|
||||
repoName := projectName
|
||||
if repoName == ".index" {
|
||||
repoName = fmt.Sprintf(config.Wildcard.IndexRepo, userName)
|
||||
var repoURLs []string
|
||||
repoURLTemplate := wildcardPattern.CloneURL
|
||||
if projectName == ".index" {
|
||||
for _, indexRepoTemplate := range wildcardPattern.IndexRepos {
|
||||
indexRepo := indexRepoTemplate.ExecuteString(map[string]any{"user": userName})
|
||||
repoURLs = append(repoURLs, repoURLTemplate.ExecuteString(map[string]interface{}{
|
||||
"user": userName,
|
||||
"project": indexRepo,
|
||||
}))
|
||||
}
|
||||
} else {
|
||||
repoURLs = append(repoURLs, repoURLTemplate.ExecuteString(map[string]interface{}{
|
||||
"user": userName,
|
||||
"project": projectName,
|
||||
}))
|
||||
}
|
||||
return repoURLs, nil
|
||||
} else {
|
||||
return nil, AuthError{
|
||||
http.StatusUnauthorized,
|
||||
fmt.Sprintf("domain %s does not match wildcard *.%s", host, config.Wildcard.Domain),
|
||||
}
|
||||
return []string{fmt.Sprintf(config.Wildcard.CloneURL, userName, repoName)}, nil
|
||||
}
|
||||
|
||||
return nil, AuthError{
|
||||
http.StatusUnauthorized,
|
||||
fmt.Sprintf("domain %s does not match wildcard *.%s", host, config.Wildcard.Domain),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/valyala/fasttemplate"
|
||||
)
|
||||
|
||||
type CacheConfig struct {
|
||||
@@ -19,9 +22,9 @@ type Config struct {
|
||||
Health string `toml:"health"`
|
||||
} `toml:"listen"`
|
||||
Wildcard struct {
|
||||
Domain string `toml:"domain"`
|
||||
CloneURL string `toml:"clone-url"`
|
||||
IndexRepo string `toml:"index-repo"`
|
||||
Domain string `toml:"domain"`
|
||||
CloneURL string `toml:"clone-url"`
|
||||
IndexRepos []string `toml:"index-repos"`
|
||||
} `toml:"wildcard"`
|
||||
Backend struct {
|
||||
Type string `toml:"type"`
|
||||
@@ -41,7 +44,14 @@ type Config struct {
|
||||
} `toml:"backend"`
|
||||
}
|
||||
|
||||
type WildcardPattern struct {
|
||||
Domain []string
|
||||
CloneURL *fasttemplate.Template
|
||||
IndexRepos []*fasttemplate.Template
|
||||
}
|
||||
|
||||
var config Config
|
||||
var wildcardPattern WildcardPattern
|
||||
|
||||
func ReadConfig(path string) error {
|
||||
file, err := os.Open(path)
|
||||
@@ -70,3 +80,25 @@ func UpdateConfigEnv() {
|
||||
updateFromEnv(&config.Backend.S3.Region, "S3_REGION")
|
||||
updateFromEnv(&config.Backend.S3.Bucket, "S3_BUCKET")
|
||||
}
|
||||
|
||||
func CompileWildcardPattern() {
|
||||
wildcardPattern = WildcardPattern{
|
||||
Domain: strings.Split(config.Wildcard.Domain, "."),
|
||||
}
|
||||
|
||||
template, err := fasttemplate.NewTemplate(config.Wildcard.CloneURL, "<", ">")
|
||||
if err != nil {
|
||||
log.Fatalf("wildcard pattern: clone URL: %s", err)
|
||||
} else {
|
||||
wildcardPattern.CloneURL = template
|
||||
}
|
||||
|
||||
for _, indexRepo := range config.Wildcard.IndexRepos {
|
||||
template, err := fasttemplate.NewTemplate(indexRepo, "<", ">")
|
||||
if err != nil {
|
||||
log.Fatalf("wildcard pattern: clone URL: %s", err)
|
||||
} else {
|
||||
wildcardPattern.IndexRepos = append(wildcardPattern.IndexRepos, template)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ func main() {
|
||||
log.Fatalln("config:", err)
|
||||
}
|
||||
UpdateConfigEnv() // environment takes priority
|
||||
CompileWildcardPattern()
|
||||
|
||||
switch config.LogFormat {
|
||||
case "short":
|
||||
|
||||
Reference in New Issue
Block a user