-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmain.go
81 lines (66 loc) · 1.79 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
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"github.com/joho/godotenv"
"github.com/lmittmann/tint"
"github.com/go-fuego/fuego"
"github.com/go-fuego/fuego/examples/full-app-gourmet/controller"
"github.com/go-fuego/fuego/examples/full-app-gourmet/server"
"github.com/go-fuego/fuego/examples/full-app-gourmet/store"
"github.com/go-fuego/fuego/examples/full-app-gourmet/views"
)
//go:generate sqlc generate
func main() {
// Load .env.local then .env files
err := godotenv.Load(".env.local", ".env")
if err != nil {
wd, _ := os.Getwd()
slog.Error(fmt.Sprintf("Error loading .env files: %s in dir %s", err, wd))
return
}
// Flags
port := flag.Int("port", 8083, "port to listen to")
dbPath := flag.String("db", "./recipe.db", "path to database file")
debug := flag.Bool("debug", false, "debug mode")
flag.Parse()
logLevel := slog.LevelInfo
if *debug {
logLevel = slog.LevelDebug
}
// Set my custom colored logger
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
AddSource: true,
Level: logLevel,
TimeFormat: "15:04:05",
}),
))
// Connect to database
db := store.InitDB(*dbPath)
store := store.New(db)
// Create resources that will be available in API controllers
apiResources := controller.Resource{
RecipesQueries: store,
IngredientsQueries: store,
DosingQueries: store,
}
// Create resources that will be available in HTML controllers
viewsResources := views.Resource{
RecipesQueries: store,
IngredientsQueries: store,
DosingQueries: store,
}
rs := server.Resources{
API: apiResources,
Views: viewsResources,
}
app := rs.Setup(fuego.WithAddr(fmt.Sprintf("localhost:%d", *port)))
// Run the server!
err = app.Run()
if err != nil {
slog.Error("Error running server: %s", "err", err)
}
}