81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
func AddCaddyRoute(cfg *Config, fqdn string, port string) error {
|
|
if err := addCaddyAPIRoute(cfg, fqdn, port); err != nil {
|
|
return fmt.Errorf("caddy API: %w", err)
|
|
}
|
|
|
|
if err := appendCaddyfile(cfg, fqdn, port); err != nil {
|
|
return fmt.Errorf("caddyfile: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func addCaddyAPIRoute(cfg *Config, fqdn string, port string) error {
|
|
route := map[string]interface{}{
|
|
"match": []map[string]interface{}{
|
|
{"host": []string{fqdn}},
|
|
},
|
|
"handle": []map[string]interface{}{
|
|
{
|
|
"handler": "reverse_proxy",
|
|
"upstreams": []map[string]string{
|
|
{"dial": "localhost:" + port},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
payload, err := json.Marshal(route)
|
|
if err != nil {
|
|
return fmt.Errorf("marshaling route: %w", err)
|
|
}
|
|
|
|
url := cfg.CaddyAdminURL + "/config/apps/http/servers/srv0/routes"
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
|
|
if err != nil {
|
|
return fmt.Errorf("creating request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func appendCaddyfile(cfg *Config, fqdn string, port string) error {
|
|
block := fmt.Sprintf("\n%s {\n\treverse_proxy localhost:%s\n}\n", fqdn, port)
|
|
|
|
f, err := os.OpenFile(cfg.CaddyfilePath, os.O_APPEND|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("opening Caddyfile: %w", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
if _, err := f.WriteString(block); err != nil {
|
|
return fmt.Errorf("writing to Caddyfile: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|