-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
58 lines (48 loc) · 1.33 KB
/
main.go
File metadata and controls
58 lines (48 loc) · 1.33 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
package main
import "fmt"
import "net/http"
import "os"
import "os/exec"
func addCORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cross-Origin-Resource-Sharing", "*")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
next.ServeHTTP(w, r)
})
}
func githubHandler(w http.ResponseWriter, r *http.Request) {
// Secret
// Validate
fmt.Fprintf(w, "Deploy triggered!")
go func() {
cmd := exec.Command("./deploy.sh")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Printf("Error running deploy script: %v\n", err)
}
}()
}
func main() {
// Load from environment variables
port := os.Getenv("PORT")
if port == "" {
port = ":8082"
}
static_dir := os.Getenv("STATIC_DIR")
if static_dir == "" {
static_dir = "./files"
}
mux := http.NewServeMux()
// Serve static
handler := addCORS(http.StripPrefix("/static/", http.FileServer(http.Dir(static_dir))))
mux.Handle("/static/", handler)
mux.HandleFunc("POST /github", githubHandler)
// Start server
fmt.Printf("Server listening on port %s - http://localhost%s\n", port, port)
err := http.ListenAndServe(port, mux)
if err != nil {
fmt.Printf("Error starting server: %v\n", err)
}
}