-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (65 loc) · 1.74 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
package main
// Hot reload
// go run github.com/cosmtrek/air
import (
"log"
"os"
"github.com/dknight/go-todoapp-sandbox/controllers"
"github.com/dknight/go-todoapp-sandbox/lib"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
_ "github.com/lib/pq"
)
const (
viewsDir = "./views"
publicDir = "./public"
logDir = "./logs"
)
var (
config *lib.Config
env *lib.Env
)
func init() {
envVar := os.Getenv("ENV")
config = lib.NewConfig(envVar)
env = &lib.Env{
Env: envVar,
DB: lib.NewDB(config),
Logger: lib.NewLogger(logDir),
}
env.Logger.Println("App started")
err := env.DB.Ping()
if err != nil {
log.Fatal(err)
} else {
log.Println("Connection to database succeeded.")
}
}
func main() {
defer env.DB.Close()
engine := html.New(viewsDir, ".gohtml")
app := fiber.New(fiber.Config{
Views: engine,
ViewsLayout: "base",
})
app.Static("/", publicDir)
todoItemController := controllers.NewTodoController(env)
systemController := controllers.NewSystemController(env)
listController := controllers.NewListController(env)
// items
app.Post("/items", todoItemController.Post)
app.Get("/items/:listid", todoItemController.GetItemsByList)
app.Put("/items/:id", todoItemController.Put)
app.Delete("/items/:id", todoItemController.Delete)
// Lists
app.Get("/lists", listController.GetLists)
app.Get("/lists/new", listController.NewList)
app.Post("/lists", listController.Post)
app.Put("/lists/:id", listController.Put)
app.Delete("/lists/:id", listController.Delete)
// System
app.Get("/", todoItemController.Index) // TODO move index to system?
app.Get("/ping", systemController.Ping)
app.Get("/instance", systemController.Instance)
log.Fatalln(app.Listen(config.ServerConnectionString()))
}