-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
99 lines (78 loc) · 2.45 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"embed"
"flag"
"fmt"
"os"
"github.com/go-goyave/goyave-blog-example/database/repository"
seeders "github.com/go-goyave/goyave-blog-example/database/seed"
"github.com/go-goyave/goyave-blog-example/http/route"
"github.com/go-goyave/goyave-blog-example/service/article"
"github.com/go-goyave/goyave-blog-example/service/storage"
"github.com/go-goyave/goyave-blog-example/service/user"
"goyave.dev/goyave/v5"
"goyave.dev/goyave/v5/util/errors"
"goyave.dev/goyave/v5/util/fsutil"
"goyave.dev/goyave/v5/util/fsutil/osfs"
"goyave.dev/goyave/v5/util/session"
_ "goyave.dev/goyave/v5/database/dialect/postgres"
)
//go:embed resources
var resources embed.FS
func main() {
var seed bool
flag.BoolVar(&seed, "seed", false, "If true, the database will be seeded with random data.")
flag.Parse()
resources := fsutil.NewEmbed(resources)
langFS, err := resources.Sub("resources/lang")
if err != nil {
fmt.Fprintln(os.Stderr, err.(*errors.Error).String())
os.Exit(1)
}
imgFS, err := resources.Sub("resources/img")
if err != nil {
fmt.Fprintln(os.Stderr, err.(*errors.Error).String())
os.Exit(1)
}
opts := goyave.Options{
LangFS: langFS,
}
server, err := goyave.New(opts)
if err != nil {
fmt.Fprintln(os.Stderr, err.(*errors.Error).String())
os.Exit(1)
}
server.Logger.Info("Registering hooks")
server.RegisterSignalHook()
server.RegisterStartupHook(func(s *goyave.Server) {
server.Logger.Info("Server is listening", "host", s.Host())
})
server.RegisterShutdownHook(func(s *goyave.Server) {
s.Logger.Info("Server is shutting down")
})
registerServices(server, imgFS)
server.Logger.Info("Registering routes")
server.RegisterRoutes(route.Register)
if seed {
server.Logger.Info("Seeding database...")
seeders.Seed(server.DB())
}
if err := server.Start(); err != nil {
server.Logger.Error(err)
os.Exit(2)
}
}
func registerServices(server *goyave.Server, imgFS fsutil.Embed) {
server.Logger.Info("Registering services")
session := session.GORM(server.DB(), nil)
userRepo := repository.NewUser(server.DB())
articleRepo := repository.NewArticle(server.DB())
storageFS, err := (&osfs.FS{}).Sub(".storage/avatars")
if err != nil {
panic(errors.New(err))
}
storageService := storage.NewService(storageFS, imgFS)
server.RegisterService(storageService)
server.RegisterService(user.NewService(session, server.Logger, userRepo, storageService))
server.RegisterService(article.NewService(session, articleRepo))
}