Foxnouns.NET/rate/main.go

55 lines
1 KiB
Go
Raw Normal View History

2024-09-10 16:53:43 +02:00
package main
import (
"encoding/json"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
)
func main() {
// read config file and parse it
confB, err := os.ReadFile("proxy-config.json")
if err != nil {
log.Fatalf("reading config.json: %v", err)
}
hn := &Handler{}
err = json.Unmarshal(confB, hn)
if err != nil {
log.Fatalf("unmarshaling config.json: %v", err)
}
2024-09-14 18:07:49 +02:00
// Override port from environment if it's set
if portEnv := os.Getenv("PORT"); portEnv != "" {
port, err := strconv.Atoi(portEnv)
if err == nil {
hn.Port = port
}
}
2024-09-10 16:53:43 +02:00
proxyURL, err := url.Parse(hn.ProxyTarget)
if err != nil {
log.Fatalf("parsing proxy_target as URL: %v", err)
}
hn.proxy = &httputil.ReverseProxy{
Rewrite: func(pr *httputil.ProxyRequest) {
pr.SetURL(proxyURL)
pr.Out.Host = pr.In.Host
},
}
hn.limiter = NewLimiter()
hn.client = &http.Client{}
log.Printf("serving on port %v", hn.Port)
err = http.ListenAndServe(":"+strconv.Itoa(hn.Port), hn)
if err != nil {
log.Fatalf("listening on port %v: %v", hn.Port, err)
}
}