-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (44 loc) · 1.03 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/amrikmalhans/go-htmx-portfolio.git/api"
"github.com/amrikmalhans/go-htmx-portfolio.git/utils"
"github.com/go-chi/chi/v5"
)
func main() {
r := chi.NewRouter()
// init static files
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
// init templates
utils.InitTemplates()
api.PortfolioRoutes(r)
api.JournalsRoutes(r)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
srv := &http.Server{
Addr: "0.0.0.0:" + port, // for railway.app
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
stopChan := make(chan os.Signal, 1)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM)
<-stopChan
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown: ", err)
}
log.Println("Server gracefully stopped")
}