-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (46 loc) · 1.09 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
package main
import (
"embed"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/mrinjamul/the-science-of-deduction/api/routes"
)
//go:embed templates/*
var files embed.FS
var (
// startTime is the time wshen the server starts
startTime time.Time = time.Now()
)
func main() {
// Get port from env
port := ":3000"
_, present := os.LookupEnv("PORT")
if present {
port = ":" + os.Getenv("PORT")
}
// Set the router as the default one shipped with Gin
server := gin.Default()
templ := template.Must(template.New("").ParseFS(files, "templates/layouts/*.html", "templates/layouts/*.html"))
server.SetHTMLTemplate(templ)
static, err := fs.Sub(files, "templates/static")
if err != nil {
panic(err)
}
media, err := fs.Sub(files, "templates/media")
if err != nil {
panic(err)
}
server.StaticFS("/static", http.FS(static))
server.StaticFS("/media", http.FS(media))
// Initialize the routes
routes.StartTime = startTime
routes.InitRoutes(server)
routes.BootTime = time.Since(startTime)
// Start and run the server
log.Fatal(server.Run(port))
}