-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (108 loc) · 3.62 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
manager "github.com/jf17/daemon-manager-systemd/service"
"log"
"net/http"
"os"
)
var systemDRecord manager.SystemDRecord
func startDaemon() (string, error) {
return systemDRecord.Start()
}
func stopDaemon() (string, error) {
return systemDRecord.Stop()
}
func restartDaemon() (string, error) {
return systemDRecord.Restart()
}
func checkDaemonStatus() (string, error) {
return systemDRecord.Status()
}
func handleStartRequest(w http.ResponseWriter, _ *http.Request) {
log.Printf("Stopping daemon: %s", systemDRecord.Name) // Log the action
start, err := startDaemon()
if err != nil {
log.Printf("Error started daemon %s: %v", systemDRecord.Name, err) // Log error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Daemon %s started successfully", systemDRecord.Name) // Log success
fmt.Fprintln(w, start)
}
func handleStopRequest(w http.ResponseWriter, _ *http.Request) {
log.Printf("Stopping daemon: %s", systemDRecord.Name) // Log the action
stop, err := stopDaemon()
if err != nil {
log.Printf("Error stopping daemon %s: %v", systemDRecord.Name, err) // Log error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Daemon %s stopped successfully", systemDRecord.Name) // Log success
fmt.Fprintln(w, stop)
}
func handleRestartRequest(w http.ResponseWriter, _ *http.Request) {
log.Printf("Restarting daemon: %s", systemDRecord.Name) // Log the action
restart, err := restartDaemon()
if err != nil {
log.Printf("Error restarting daemon %s: %v", systemDRecord.Name, err) // Log error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Daemon %s restart successfully", systemDRecord.Name) // Log success
fmt.Fprintln(w, restart)
}
func handleStatusRequest(w http.ResponseWriter, _ *http.Request) {
log.Printf("Check status daemon: %s", systemDRecord.Name) // Log the action
status, err := checkDaemonStatus()
if err != nil {
log.Printf("Error check status daemon %s: %v", systemDRecord.Name, err) // Log error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, status)
}
func main() {
logFile, err := os.OpenFile("daemon-manager.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Error opening log logFile: %v", err)
}
defer logFile.Close()
log.SetOutput(logFile) // Set output to the log logFile
name := ""
path := ""
port := ""
for i := 1; i < len(os.Args); i += 2 {
switch os.Args[i] {
case "-name":
name = os.Args[i+1]
case "-path":
path = os.Args[i+1]
case "-port":
port = ":" + os.Args[i+1]
default:
fmt.Println("Invalid argument:", os.Args[i])
return
}
}
if name == "" || path == "" || port == "" {
fmt.Println("Problems with arguments when launching the application.\n Example: sudo ./daemon-manager-systemd -port 8080 -name apache2 -path /lib/systemd/system/")
log.Println("Problems with arguments when launching the application.")
return
}
fmt.Println("name=" + name)
fmt.Println("path=" + path)
fmt.Println("port=" + port)
systemDRecord = manager.SystemDRecord{Name: name, Path: path}
http.HandleFunc("/"+name+"/start", handleStartRequest)
http.HandleFunc("/"+name+"/stop", handleStopRequest)
http.HandleFunc("/"+name+"/restart", handleRestartRequest)
http.HandleFunc("/"+name+"/status", handleStatusRequest)
fmt.Printf("Server listening on port %s...\n", port)
fmt.Println("http://localhost" + port + "/" + name + "/status")
errListenAndServe := http.ListenAndServe(port, nil)
if errListenAndServe != nil {
log.Fatalf("Error ListenAndServe %s", errListenAndServe)
return
}
}