mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
generate basic caddy.json with empty http and https servers
This commit is contained in:
@@ -5,27 +5,33 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig"
|
||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"uncloud/internal/api"
|
||||
"uncloud/internal/fs"
|
||||
"uncloud/internal/machine/store"
|
||||
)
|
||||
|
||||
const CaddyGroup = "uncloud"
|
||||
|
||||
// Controller monitors container changes in the cluster store and generates a configuration file for Caddy reverse
|
||||
// proxy. The generated Caddyfile allows Caddy to route external traffic to service containers across the internal
|
||||
// network.
|
||||
type Controller struct {
|
||||
store *store.Store
|
||||
path string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewController(store *store.Store, path string) (*Controller, error) {
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("create parent directory for Caddyfile '%s': %w", dir, err)
|
||||
if err := os.MkdirAll(dir, 0750); err != nil {
|
||||
return nil, fmt.Errorf("create parent directory for Caddy configuration '%s': %w", dir, err)
|
||||
}
|
||||
if err := fs.Chown(dir, "", CaddyGroup); err != nil {
|
||||
return nil, fmt.Errorf("change owner of parent directory for Caddy configuration '%s': %w", dir, err)
|
||||
}
|
||||
|
||||
return &Controller{
|
||||
@@ -90,14 +96,41 @@ func (c *Controller) filterAvailableContainers(containerRecords []*store.Contain
|
||||
}
|
||||
|
||||
func (c *Controller) generateConfig(containers []*api.Container) error {
|
||||
servers := make(map[string]*caddyhttp.Server)
|
||||
servers["http"] = &caddyhttp.Server{
|
||||
Listen: []string{fmt.Sprintf(":%d", caddyhttp.DefaultHTTPPort)},
|
||||
}
|
||||
servers["https"] = &caddyhttp.Server{
|
||||
Listen: []string{fmt.Sprintf(":%d", caddyhttp.DefaultHTTPSPort)},
|
||||
}
|
||||
|
||||
httpApp := caddyhttp.App{
|
||||
Servers: servers,
|
||||
}
|
||||
|
||||
config := &caddy.Config{
|
||||
AppsRaw: make(caddy.ModuleMap),
|
||||
}
|
||||
var warnings []caddyconfig.Warning
|
||||
config.AppsRaw["http"] = caddyconfig.JSON(httpApp, &warnings)
|
||||
|
||||
_, err := json.MarshalIndent(config, "", " ")
|
||||
if len(warnings) > 0 {
|
||||
for _, w := range warnings {
|
||||
slog.Warn("Generate Caddy configuration warning.", "warn", w.String())
|
||||
}
|
||||
}
|
||||
|
||||
configBytes, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal Caddy configuration: %w", err)
|
||||
}
|
||||
|
||||
if err = os.WriteFile(c.path, configBytes, 0640); err != nil {
|
||||
return fmt.Errorf("write Caddy configuration to file '%s': %w", c.path, err)
|
||||
}
|
||||
if err = fs.Chown(c.path, "", CaddyGroup); err != nil {
|
||||
return fmt.Errorf("change owner of Caddy configuration file '%s': %w", c.path, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func (c *Config) Write(path, owner string) error {
|
||||
if err := os.WriteFile(path, data.Bytes(), 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fs.Chown(path, owner); err != nil {
|
||||
if err := fs.Chown(path, owner, owner); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -72,7 +72,7 @@ func MkDataDir(dir, owner string) error {
|
||||
}
|
||||
|
||||
if owner != "" {
|
||||
if err := fs.Chown(dir, owner); err != nil {
|
||||
if err := fs.Chown(dir, owner, owner); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ func (c *Config) SetDefaults() (*Config, error) {
|
||||
}
|
||||
|
||||
if cfg.CaddyfilePath == "" {
|
||||
cfg.CaddyfilePath = filepath.Join(cfg.DataDir, "caddy", "Caddyfile")
|
||||
cfg.CaddyfilePath = filepath.Join(cfg.DataDir, "caddy", "caddy.json")
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
|
||||
Reference in New Issue
Block a user