summaryrefslogtreecommitdiff
path: root/internal/nginx
diff options
context:
space:
mode:
Diffstat (limited to 'internal/nginx')
-rw-r--r--internal/nginx/nginx.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/internal/nginx/nginx.go b/internal/nginx/nginx.go
new file mode 100644
index 0000000..abdd4c9
--- /dev/null
+++ b/internal/nginx/nginx.go
@@ -0,0 +1,45 @@
+// Package nginx regenerates one vhost file per app and reloads. Skiff never
+// touches nginx.conf itself - that just needs one `include skiff.d/*.conf;`
+// line added once, by hand, when nginx is first set up.
+package nginx
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "os"
+
+ "skiff/internal/layout"
+ "skiff/internal/sh"
+ "skiff/internal/tmpl"
+)
+
+// EnsureRoute writes app's vhost file and reloads nginx, but only if the
+// rendered config actually changed - most deploys don't touch routing.
+func EnsureRoute(ctx context.Context, out io.Writer, cfg layout.Config, app, ip string, port int, serverName string) error {
+ rendered, err := tmpl.NginxApp(tmpl.NginxAppData{
+ Name: app,
+ IP: ip,
+ Port: port,
+ ServerName: serverName,
+ })
+ if err != nil {
+ return err
+ }
+
+ path := cfg.NginxConfPath(app)
+ existing, _ := os.ReadFile(path)
+ if string(existing) == rendered {
+ return nil
+ }
+
+ if err := os.WriteFile(path, []byte(rendered), 0644); err != nil {
+ return fmt.Errorf("write nginx conf for %s: %w", app, err)
+ }
+ fmt.Fprintf(out, "==> nginx route changed for %s, reloading\n", app)
+ return Reload(ctx, out)
+}
+
+func Reload(ctx context.Context, out io.Writer) error {
+ return sh.Run(ctx, out, "", "nginx", "-s", "reload")
+}