-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (60 loc) · 1.63 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
package main
import (
"bytes"
"io"
"log"
"main/app/acast"
"main/app/admin"
"main/app/config"
"main/app/google"
"main/app/notifications"
"main/app/processing"
"net/http"
"os"
"strings"
)
func main() {
logFile, err := os.OpenFile("files/selfpod-logs.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal("Error opening log file:", err)
}
defer logFile.Close()
log.SetOutput(logFile)
notifications.SendDiscordNotification("Server starting...")
processing.RestoreFailedProcess()
startServer()
}
func startServer() {
mux := http.NewServeMux()
mux.HandleFunc("/acast", acast.WebHook)
mux.HandleFunc("/login", google.LoginToGoogle)
mux.HandleFunc(config.GoogleRedirectPath, google.Oauth2Callback)
mux.HandleFunc("/try_upload", admin.TryUploadAgain)
loggedMux := requestLoggingMiddleware(mux)
log.Println("Server starting...")
err := http.ListenAndServe(":"+config.Port, loggedMux)
if err != nil {
log.Fatal("Error starting server: ", err)
return
}
}
func requestLoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading request body: %v", err)
}
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
headers := make([]string, 0, len(r.Header))
for k, v := range r.Header {
headers = append(headers, k+": "+strings.Join(v, ", "))
}
log.Printf(
"Received request: %s %s from %s\nHeaders: %s\nBody: %s",
r.Method, r.URL.Path, r.RemoteAddr, strings.Join(headers, "; "), string(bodyBytes),
)
next.ServeHTTP(w, r)
},
)
}