-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (115 loc) · 3.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"fmt"
"github.com/nightsilvertech/clockwerk/constant"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
ep "github.com/nightsilvertech/clockwerk/endpoints"
"github.com/nightsilvertech/clockwerk/gvar"
pb "github.com/nightsilvertech/clockwerk/protocs/api/v1"
"github.com/nightsilvertech/clockwerk/repository"
"github.com/nightsilvertech/clockwerk/service"
"github.com/nightsilvertech/clockwerk/transports"
"github.com/robfig/cron/v3"
"github.com/soheilhy/cmux"
"golang.org/x/crypto/bcrypt"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
)
var redisHost, redisPort, redisPass string
var username, password, port string
func CORS(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers := []string{"Content-Type", "Accept", "Authorization","Access-Control-Allow-Headers","X-Requested-With"}
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
}
h.ServeHTTP(w, r)
})
}
func ServeGRPC(listener net.Listener, service pb.ClockwerkServer, serverOptions []grpc.ServerOption) error {
log.Println(fmt.Sprintf("grpc served at port %s", port))
var grpcServer *grpc.Server
if len(serverOptions) > 0 {
grpcServer = grpc.NewServer(serverOptions...)
} else {
grpcServer = grpc.NewServer()
}
pb.RegisterClockwerkServer(grpcServer, service)
return grpcServer.Serve(listener)
}
func ServeHTTP(listener net.Listener, service pb.ClockwerkServer) error {
log.Println(fmt.Sprintf("http served at port %s", port))
mux := runtime.NewServeMux()
err := pb.RegisterClockwerkHandlerServer(context.Background(), mux, service)
if err != nil {
return err
}
return http.Serve(listener, CORS(mux))
}
func MergeServer(service pb.ClockwerkServer, serverOptions []grpc.ServerOption) {
if len(port) > 0 {
port = fmt.Sprintf(":%s", constant.DefaultPort)
}
listener, err := net.Listen("tcp", port)
if err != nil {
panic(err)
}
m := cmux.New(listener)
httpListener := m.Match(cmux.HTTP1Fast())
grpcListener := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings(
"content-type", "application/grpc",
))
g := new(errgroup.Group)
g.Go(func() error { return ServeGRPC(grpcListener, service, serverOptions) })
g.Go(func() error { return ServeHTTP(httpListener, service) })
g.Go(func() error { return m.Serve() })
log.Fatal(g.Wait())
}
func CreatedCredential(username, password string) {
hashByte, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
panic(err)
}
key := fmt.Sprintf("%s_%s", gvar.HashKeyMap, username)
gvar.SyncMapHashStorage.Store(key, string(hashByte))
}
func PrepareCredential() {
//err := godotenv.Load()
//if err != nil {
// log.Fatal("Error loading .env file")
//}
port = "1929"
username = os.Getenv("SCHEDULER_USERNAME")
password = os.Getenv("SCHEDULER_PASSWORD")
if len(username) == 0 || len(password) == 0 {
panic("please set your credential")
}
redisHost = os.Getenv("REDIS_HOST")
redisPort = os.Getenv("REDIS_PORT")
redisPass = os.Getenv("REDIS_PASS")
if len(redisHost) == 0 || len(redisPort) == 0 || len(redisPass) == 0 {
panic("please set your redis host, port and password")
}
log.Println(fmt.Sprintf("connect to redis at %s port %s", redisHost, redisPort))
}
func main() {
PrepareCredential()
CreatedCredential(username, password)
cronjb := cron.New()
repo := repository.NewRepo(redisHost, redisPort, redisPass)
services := service.NewClockwerk(cronjb, repo)
_, _ = services.Backup(context.Background(), nil)
server := transports.NewClockwerkServer(ep.NewClockwerkEndpoint(services))
MergeServer(server, nil)
}