-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 1.93 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
package main
import (
"context"
_ "context"
_ "database/sql"
"fmt"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
_ "github.com/lib/pq"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"log"
"net"
"pigeon/db"
"pigeon/pkg/handlers"
pb "pigeon/proto"
)
func main() {
db.Connect()
handlers.RestartExistsJobs(db.GetDetailedJobs())
// Start GRpc Server
//todo this maybe inefficient
go startGRpcServer()
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.Use(middleware.Secure())
//e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
// Format: "method=${method}, uri=${uri}, status=${status}\n",
//}))
e.Use(middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{
KeyLookup: "header:api-key",
Validator: func(key string, c echo.Context) (bool, error) {
// todo this is a temporary solution. NEED TO FIX!!!
return key == db.GetApiKey(), nil
},
}))
// Routes
e.POST("/scheduler", handlers.AttachNewTask)
e.GET("/scheduler/:id", handlers.GetTaskDetail)
e.GET("/scheduler", handlers.GetTasks)
e.DELETE("/scheduler/:id", handlers.CancelTask)
// Start server
e.Logger.Fatal(e.Start(":4040"))
}
func startGRpcServer() {
fmt.Println("⇨ gRPC server started on [::]:6566")
listen, err := net.Listen("tcp", db.GetMyGrpcServer())
if err != nil {
log.Printf("Failed to listen: %v", err)
}
var opts []grpc.ServerOption
server := grpc.NewServer(opts...)
reflection.Register(server)
pb.RegisterNotificationServiceServer(server, &svc{})
err = server.Serve(listen)
log.Fatal(err)
}
func (s *svc) ScheduleNotification(ctx context.Context, req *pb.ScheduleNotificationRequest) (*pb.ScheduleNotificationResponse, error) {
handlers.QneTimeScheduledNotification(req.NotificationId, req.SendAt)
return &pb.ScheduleNotificationResponse{Done: true}, nil
}
type svc struct {
pb.UnimplementedNotificationServiceServer
}