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 {
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)