-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (87 loc) · 2.84 KB
/
main.go
File metadata and controls
104 lines (87 loc) · 2.84 KB
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
100
101
102
103
104
package main
import (
"log"
"net"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/usebruno/bruno-doggy-daycare/internal/attendance"
"github.com/usebruno/bruno-doggy-daycare/internal/dogs"
"github.com/usebruno/bruno-doggy-daycare/internal/feed"
pb "github.com/usebruno/bruno-doggy-daycare/internal/feed/proto"
"github.com/usebruno/bruno-doggy-daycare/internal/platform/db"
"github.com/usebruno/bruno-doggy-daycare/internal/platform/server"
"github.com/usebruno/bruno-doggy-daycare/internal/social"
"github.com/usebruno/bruno-doggy-daycare/internal/social/graph"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func main() {
// 1. Setup DB
dbPath := os.Getenv("DB_PATH")
database, err := db.New(dbPath)
if err != nil {
log.Fatalf("failed to connect to db: %v", err)
}
defer database.Close()
if err := db.RunMigrations(database); err != nil {
log.Fatalf("failed to run migrations: %v", err)
}
// 2. Setup Repositories(Abstractions)
dogsRepo := dogs.NewRepository(database)
attendanceRepo := attendance.NewRepository(database)
socialRepo := social.NewRepository(database)
feedRepo := feed.NewRepository(database)
// 3. Setup Hubs(Middlewares)
attendanceHub := attendance.NewHub()
go attendanceHub.Run()
// 4. Setup Handlers(Controllers)
dogsHandler := dogs.NewHandler(dogsRepo)
attendanceHandler := attendance.NewHandler(attendanceRepo, attendanceHub)
feedGRPC := feed.NewGRPCServer(feedRepo)
// 5. Setup Router
r := server.NewRouter()
// Dogs
r.Post("/dogs", dogsHandler.CreateDog)
r.Get("/dogs", dogsHandler.ListDogs)
r.Get("/dogs/{id}", dogsHandler.GetDog)
r.Patch("/dogs/{id}", dogsHandler.UpdateDog)
r.Delete("/dogs/{id}", dogsHandler.DeleteDog)
// Attendance
r.Mount("/", attendanceHandler.Routes())
// Social
// REST endpoints removed in favor of GraphQL
// GraphQL
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{Repo: socialRepo}}))
r.Handle("/graphql", srv)
r.Handle("/playground", playground.Handler("GraphQL playground", "/graphql"))
// Feed
// REST and WebSocket endpoints removed in favor of gRPC
// 6. Start gRPC Server
go func() {
grpcPort := os.Getenv("GRPC_PORT")
if grpcPort == "" {
grpcPort = "50051"
}
lis, err := net.Listen("tcp", ":"+grpcPort)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterFeedServiceServer(s, feedGRPC)
reflection.Register(s)
log.Printf("Starting gRPC server on :%s", grpcPort)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Starting server on :%s", port)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}