-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (55 loc) · 1.41 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
package main
import (
"fmt"
"it1shka/distrosim/backend"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func setupEnvironment(defaultEnv map[string]string) {
if err := godotenv.Load(); err != nil {
fmt.Println("Failed to load .env file. Setting up default env:")
for key, value := range defaultEnv {
fmt.Printf("%s=%s\n", key, value)
os.Setenv(key, value)
}
}
}
func setupServer(server *gin.Engine) {
server.Static("/assets", "./assets")
server.Static("/images", "./images")
server.LoadHTMLGlob("html/*.html")
server.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.html", nil)
})
server.GET("/menu", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "menu.html", nil)
})
backend.SetupDatabaseRequests(server)
server.NoRoute(func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, "error.html", gin.H{
"name": "Error 404: Page Not Found",
"error": "$ error --code=404 --description=\"Page Not Found\"",
})
})
}
func main() {
setupEnvironment(map[string]string{
"GIN_MODE": "debug",
"PORT": ":3000",
"DB_FILE": "database.db",
})
dbFile := os.Getenv("DB_FILE")
if err := backend.DatabaseConnect(dbFile); err != nil {
log.Fatal(err)
return
}
gin.SetMode(os.Getenv("GIN_MODE"))
server := gin.New()
setupServer(server)
port := os.Getenv("PORT")
fmt.Printf("Listening on port %s...\n", port)
log.Fatal(server.Run(port))
}