feat: map concrete domains on pages domains

This commit is contained in:
Pavel 2023-11-13 09:25:14 +03:00
parent 3a2bc89fbd
commit e08858764a

26
main.go
View File

@ -18,9 +18,10 @@ import (
type GiteaHandler struct { type GiteaHandler struct {
Client *gitea.Client Client *gitea.Client
Server string Server string
Domain string Domain string
Token string Token string
DomainsMap map[string]string
} }
// Init ... // Init ...
@ -33,6 +34,10 @@ func (h *GiteaHandler) Init() error {
// ServeHTTP ... // ServeHTTP ...
func (h *GiteaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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) // remove the domain if it's set (works fine if it's empty)
user := strings.TrimRight(strings.TrimSuffix(r.Host, h.Domain), ".") user := strings.TrimRight(strings.TrimSuffix(r.Host, h.Domain), ".")
log.Printf("user: %s", user) log.Printf("user: %s", user)
@ -53,10 +58,19 @@ func (h *GiteaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func main() { 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{ handler := &GiteaHandler{
Domain: os.Getenv("DOMAIN"), Domain: os.Getenv("DOMAIN"),
Server: os.Getenv("GITEA_URL"), Server: os.Getenv("GITEA_URL"),
Token: os.Getenv("GITEA_TOKEN"), Token: os.Getenv("GITEA_TOKEN"),
DomainsMap: domainsMap,
} }
if err := handler.Init(); err != nil { if err := handler.Init(); err != nil {
log.Fatalf("error initializing handler: %s", err) log.Fatalf("error initializing handler: %s", err)