-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.48 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
package main
import (
"fmt"
"log"
"net"
"os"
"github.com/tk42/grpc-surrealdb-template/api/routes"
"github.com/tk42/grpc-surrealdb-template/pkg/book"
bufbuild "github.com/tk42/grpc-surrealdb-template/gen/golang/github.com/tk42/grpc-surrealdb-template"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
surrealdb "github.com/surrealdb/surrealdb.go"
)
var (
SERVER_PORT = os.Getenv("PORT")
SURREALDB_URL = os.Getenv("SURREALDB_URL")
SURREALDB_NS = os.Getenv("SURREALDB_NS")
SURREALDB_DB = os.Getenv("SURREALDB_DB")
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)
svc := routes.BookRouter(bookRepo)
server := grpc.NewServer()
reflection.Register(server)
bufbuild.RegisterBookServiceServer(server, svc)
lis, err := net.Listen("tcp", ":"+SERVER_PORT)
if err != nil {
log.Fatalf("failed to listen: %s", err)
}
if err := server.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
}
}
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
}
_, err = db.Use(SURREALDB_NS, SURREALDB_DB)
return db, err
}