Skip to content

Instantly share code, notes, and snippets.

@zeusro
Created March 4, 2020 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeusro/0f6b2a8c2896c355c0ea2772ab2284cb to your computer and use it in GitHub Desktop.
Save zeusro/0f6b2a8c2896c355c0ea2772ab2284cb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
)
func main() {
files, err := ioutil.ReadDir("./nginx")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
filePath := "nginx/" + f.Name()
dat, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Print(err)
}
content := string(dat)
reServerName := regexp.MustCompile(`server_name[\.\s\w-/:]+`)
reProxy := regexp.MustCompile(`proxy_pass[\.\s\w-/:]+`)
host1 := strings.Replace(reServerName.FindString(content), "server_name", "", -1)
host1 = strings.TrimSpace(host1)
host2 := strings.ReplaceAll(host1, "old.io", "new.fun")
proxy := strings.Replace(reProxy.FindString(content), "proxy_pass", "", -1)
proxy = strings.TrimSpace(proxy)
caddyTemplate := `
https://$host1 https://$host2 {
gzip
tls {
dns dnspod
wildcard
}
proxy / $proxy {
transparent
}
}
`
caddyTemplate = strings.ReplaceAll(caddyTemplate, "$host1", host1)
caddyTemplate = strings.ReplaceAll(caddyTemplate, "$host2", host2)
caddyTemplate = strings.ReplaceAll(caddyTemplate, "$proxy", proxy)
// fmt.Println(caddyTemplate)
ioutil.WriteFile("output/"+host2, []byte(caddyTemplate), 0644)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment