diff --git a/main.go b/main.go index eb4e0c5..842ef8c 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,10 @@ import ( type GiteaHandler struct { Client *gitea.Client - Server string - Domain string - Token string + Server string + Domain string + Token string + DomainsMap map[string]string } // Init ... @@ -33,6 +34,10 @@ func (h *GiteaHandler) Init() error { // ServeHTTP ... func (h *GiteaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // use domain from the map + if mapped, ok := h.DomainsMap[r.Host]; ok { + r.Host = mapped + } // remove the domain if it's set (works fine if it's empty) user := strings.TrimRight(strings.TrimSuffix(r.Host, h.Domain), ".") log.Printf("user: %s", user) @@ -53,10 +58,19 @@ func (h *GiteaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func main() { + domainsMap := map[string]string{} + items := strings.Split(os.Getenv("DOMAINS_MAP"), ";") + for _, item := range items { + itemParts := strings.Split(item, ":") + if len(itemParts) == 2 { + domainsMap[itemParts[0]] = itemParts[1] + } + } handler := &GiteaHandler{ - Domain: os.Getenv("DOMAIN"), - Server: os.Getenv("GITEA_URL"), - Token: os.Getenv("GITEA_TOKEN"), + Domain: os.Getenv("DOMAIN"), + Server: os.Getenv("GITEA_URL"), + Token: os.Getenv("GITEA_TOKEN"), + DomainsMap: domainsMap, } if err := handler.Init(); err != nil { log.Fatalf("error initializing handler: %s", err)