-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (49 loc) · 1.25 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
package main
import (
"fmt"
"log"
"os"
"github.com/tk42/gofiber-surrealdb-template/api/routes"
"github.com/tk42/gofiber-surrealdb-template/pkg/book"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
surrealdb "github.com/surrealdb/surrealdb.go"
)
var (
SURREALDB_URL = os.Getenv("SURREALDB_URL")
SURREALDB_USER = os.Getenv("SURREALDB_USER")
SURREALDB_PASS = os.Getenv("SURREALDB_PASS")
)
func main() {
db, err := databaseConnection()
if err != nil {
log.Fatal("Database Connection Error $s", err)
}
fmt.Println("Database connection success!")
bookRepo := book.NewRepo(db)
bookService := book.NewService(bookRepo)
app := fiber.New()
app.Use(cors.New())
app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.Send([]byte("Welcome to the clean-architecture surrealdb shop!"))
})
api := app.Group("/api")
routes.BookRouter(api, bookService)
log.Fatal(app.Listen(":8080"))
}
func databaseConnection() (*surrealdb.DB, error) {
db, err := surrealdb.New(SURREALDB_URL)
if err != nil {
return nil, err
}
_, err = db.Signin(map[string]interface{}{
"user": SURREALDB_USER,
"pass": SURREALDB_PASS,
})
if err != nil {
return nil, err
}
// specify Namespace and Database
_, err = db.Use("ns", "books")
return db, err
}