-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (111 loc) · 3.3 KB
/
Copy pathmain.go
File metadata and controls
133 lines (111 loc) · 3.3 KB
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
package main
import (
"context"
"embed"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/caddyserver/certmagic"
)
//go:embed all:static
var staticFiles embed.FS
var gunicornURL = "127.0.0.1:8000"
func main() {
// You can handle more graceful failures here
auto_migrate := os.Getenv("AUTO_MIGRATE") == "true"
err := runMigrations(auto_migrate)
if err != nil {
fmt.Println("Error running migrations: ", err)
return
}
gunicornCmd, err := startGunicorn()
if err != nil {
fmt.Println("Error starting gunicorn: ", err)
return
}
router := http.NewServeMux()
router.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/icons/favicon.ico", http.StatusMovedPermanently)
})
router.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age=3600")
http.FileServer(http.FS(staticFiles)).ServeHTTP(w, r)
})
// Django
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
reverseProxy(w, r, gunicornURL)
})
server := &http.Server{Handler: router}
go func() {
if os.Getenv("CERT_AUTO_TLS") == "true" {
certmagic.DefaultACME.Agreed = true
certmagic.DefaultACME.Email = os.Getenv("CERT_EMAIL")
if strings.EqualFold(os.Getenv("CERT_STAGING"), "true") {
certmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCA
}
println("Starting server at: " + os.Getenv("HOST_NAME"))
if err := certmagic.HTTPS([]string{os.Getenv("HOST_NAME")}, router); err != nil {
println("Error starting server: ", err)
}
} else {
println("Starting server")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("HTTP server error: %v\n", err)
}
}
}()
// Capture interrupt signal
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
// Handle shutdown
fmt.Println("Shutting down...")
// Terminate Python subprocess
if err := gunicornCmd.Process.Signal(os.Interrupt); err != nil {
fmt.Printf("Error terminating Python subprocess: %v\n", err)
}
gunicornCmd.Process.Wait()
// Shutdown HTTP server gracefully
if err := server.Shutdown(context.Background()); err != nil {
fmt.Printf("Error shutting down HTTP server: %v\n", err)
}
}
func reverseProxy(w http.ResponseWriter, r *http.Request, gunicornURL string) {
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "http",
Host: gunicornURL,
})
// Serve the request using the reverse proxy
proxy.ServeHTTP(w, r)
}
func startGunicorn() (*exec.Cmd, error) {
cmdArgs := []string{"app.config.wsgi", "-b " + gunicornURL, "--workers", "3", "--max-requests", "1200", "--max-requests-jitter", "50"}
cmd := exec.Command("gunicorn", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
fmt.Println("Error starting gunicorn process:", err)
return nil, nil
}
return cmd, nil
}
func runMigrations(auto_migrate bool) error {
cmdArgs := []string{"manage.py", "migrate"}
if auto_migrate {
fmt.Println("Running migrations")
} else {
fmt.Println("Checking migrations")
cmdArgs = append(cmdArgs, "--check")
}
cmd := exec.Command("python3", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}