-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage-nginx.go
71 lines (58 loc) · 2.05 KB
/
manage-nginx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"github.com/anhoffa/starchasers-nginx-config-creator/utils"
"os"
"os/exec"
"path/filepath"
)
// startNginx starts the Nginx service in the foreground as the child process.
func startNginx() error {
if err := exec.Command("nginx", "-g", "daemon off;").Start(); err != nil {
return err
}
log.Info("Nginx started successfully")
return nil
}
// reloadNginxConfig reloads the Nginx configuration without stopping the service.
func reloadNginxConfig() error {
if err := utils.RedirectCmdOutput("nginx", "-s", "reload"); err != nil {
return fmt.Errorf("failed to reload Nginx configuration: %w", err)
}
log.Info("Nginx configuration reloaded successfully")
return nil
}
// modifyNginxConfig modifies the Nginx configuration file by overwriting it with a new configuration.
// It should be used only after the configuration has been validated with validateNginxConfig.
func modifyNginxConfig(newConfig string) error {
dir := filepath.Dir(nginxConfigFilePath)
tmpPath := filepath.Join(dir, "nginx.tmp")
err := os.WriteFile(tmpPath, []byte(newConfig), 0644)
if err != nil {
return fmt.Errorf("error writing the modified config file: %w", err)
}
if err := os.Rename(tmpPath, filepath.Join(dir, "nginx.conf")); err != nil {
return err
}
return nil
}
// setupNginxConfig generates, validates, modifies, and (re)loads the Nginx configuration.
func setupNginxConfig(loadFunc func() error, jsonConfig []byte) error {
config, err := generateNginxConfig(jsonConfig)
if err != nil {
log.Errorw("Error generating NGINX config", "error", err)
return fmt.Errorf("error generating NGINX config: %w", err)
}
if err := validateNginxConfig(config); err != nil {
log.Errorw("Error validating NGINX config", "error", err)
return fmt.Errorf("error validating NGINX config: %w", err)
}
log.Infow("Config: ", "config", config)
if err := modifyNginxConfig(config); err != nil {
log.Errorw("Error modifying NGINX config", "error", err)
}
if err := loadFunc(); err != nil {
log.Fatalw("Failed to start Nginx: %v", "error", err)
}
return nil
}