-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (86 loc) · 2.5 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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"webapp/handlers"
"webapp/templates"
"webapp/config"
)
func main() {
// Parse the -dev flag
dev := flag.Bool("dev", false, "run in development mode")
flag.Parse()
templates.InitTemplates()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := templates.Main.Execute(w, nil); err != nil {
log.Printf("Error executing main template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
})
http.HandleFunc("/deposit", handlers.DepositHandler)
http.HandleFunc("/withdraw", handlers.WithdrawHandler)
http.HandleFunc("/confirm-deposit", handlers.ConfirmDepositHandler)
http.HandleFunc("/confirm-withdraw", handlers.ConfirmWithdrawHandler)
// Serve static files from the "static" directory
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
var server *http.Server
// Determine the mode and configure the server accordingly
if *dev {
// Development mode
cert, err := tls.LoadX509KeyPair("dev-ssl-certificates/localhost+4.pem",
"dev-ssl-certificates/localhost+4-key.pem")
if err != nil {
log.Fatalf("Error loading certificates: %v", err)
}
// Create a custom HTTPS server
server = &http.Server{
Addr: ":" + config.DevelopmentPort,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
fmt.Printf("Server running in development mode on port %s\n",
config.DevelopmentPort)
go func() {
err = server.ListenAndServeTLS("", "")
if err != nil && err != http.ErrServerClosed {
log.Fatalf("Error starting HTTPS server: %v", err)
}
}()
} else {
// Production mode
server = &http.Server{
Addr: ":" + config.ProductionPort,
}
fmt.Printf("Server running in production mode on port %s\n",
config.ProductionPort)
go func() {
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatalf("Error starting HTTP server: %v", err)
}
}()
}
// Handle graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-quit
fmt.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v\n", err)
}
}()
// Block the main goroutine until the server is shut down
select {}
}