From e08858764a177ca12739fb7d10a6439afc3b2467 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Mon, 13 Nov 2023 09:25:14 +0300 Subject: [PATCH] feat: map concrete domains on pages domains --- main.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) 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)