-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (86 loc) · 2.36 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
package main
import (
"context"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jackc/pgx/v5"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type TemplateRenderer struct {
templates *template.Template
}
func main() {
// create a channel to listen for signals
sigChan := make(chan os.Signal, 1)
// listen to SIGTERM and SIGINT (ctrl-c)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt)
// Load connection string from .env file
if os.Getenv("GO_ENV") != "production" {
fmt.Println("Loading env...")
err := godotenv.Load()
if err != nil {
log.Fatal("failed to load env", err)
}
}
templates, err := template.New("").ParseGlob("views/*.html")
if err != nil {
log.Fatalf("Error initializing templates: %v", err)
os.Exit(1)
}
conn, err := pgx.Connect(context.Background(), os.Getenv("DB_URL"))
if err != nil {
log.Fatalf("Error connecting to database: %v\n", err)
}
defer func() {
// shutdown database connection
if err := conn.Close(context.Background()); err != nil {
log.Printf("Error closing database connection: %v\n", err)
} else {
log.Println("Database connection closed gracefully.")
}
}()
e := echo.New()
e.Renderer = &TemplateRenderer{
templates: templates,
}
// serve server in a goroutine, allow the code to listen to ctrl-c
go func() {
e.Use(middleware.Logger())
e.Static("/static", "static")
e.GET("/", renderPage)
e.GET("/service/health-check", func(c echo.Context) error {
c.Response().Writer.WriteHeader(http.StatusOK)
c.Response().Write([]byte("APP VERSION: 1.0.0"))
return nil
})
port := os.Getenv("PORT")
if port == "" {
port = "5173"
}
e.Start(fmt.Sprintf(":%s", port))
}()
// block until a signal is received
sig := <-sigChan
log.Printf("Caught signal: %s\n", sig)
// start graceful shutdown with a timeout
// should stop everything and clean up within 10 seconds
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
log.Printf("Error during server shutdown: %v\n", err)
} else {
log.Println("Server shut down gracefully.")
}
}
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}