-
Notifications
You must be signed in to change notification settings - Fork 31
/
server.go
42 lines (35 loc) · 1.16 KB
/
server.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
package main
import (
"net/http"
"github.com/eldimious/golang-api-showcase/config"
db "github.com/eldimious/golang-api-showcase/data/database"
"github.com/eldimious/golang-api-showcase/domain/authors"
"github.com/eldimious/golang-api-showcase/domain/books"
router "github.com/eldimious/golang-api-showcase/router/http"
authorsStore "github.com/eldimious/golang-api-showcase/data/authors"
booksStore "github.com/eldimious/golang-api-showcase/data/books"
)
func main() {
// get configuration stucts via .env file
configuration, err := config.NewConfig()
if err != nil {
panic(err)
}
// establish DB connection
db, err := db.Connect(configuration.Database)
if err != nil {
panic(err)
}
// initialize repos and services using DI
booksRepo := booksStore.New(db)
booksSvc := books.NewService(booksRepo)
authorsRepo := authorsStore.New(db, booksRepo)
booksRepo.AddAuthorFKConstraint() // Author relation must exist before we add the constraint
authorsSvc := authors.NewService(authorsRepo)
httpRouter := router.NewHTTPHandler(authorsSvc, booksSvc)
err = http.ListenAndServe(":"+configuration.Port, httpRouter)
if err != nil {
panic(err)
}
defer db.Close()
}