forked from spacecloud-io/space-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
98 lines (82 loc) · 2.51 KB
/
server.go
File metadata and controls
98 lines (82 loc) · 2.51 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
package main
import (
"log"
"net"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/rs/cors"
"google.golang.org/grpc"
"github.com/spaceuptech/space-cloud/config"
"github.com/spaceuptech/space-cloud/modules/auth"
"github.com/spaceuptech/space-cloud/modules/crud"
"github.com/spaceuptech/space-cloud/modules/faas"
"github.com/spaceuptech/space-cloud/modules/filestore"
"github.com/spaceuptech/space-cloud/modules/realtime"
"github.com/spaceuptech/space-cloud/modules/userman"
pb "github.com/spaceuptech/space-cloud/proto"
)
type server struct {
router *mux.Router
auth *auth.Module
crud *crud.Module
user *userman.Module
file *filestore.Module
faas *faas.Module
realtime *realtime.Module
isProd bool
}
func initServer(isProd bool) *server {
r := mux.NewRouter()
c := crud.Init()
a := auth.Init(c)
u := userman.Init(c, a)
f := filestore.Init()
realtime := realtime.Init()
faas := faas.Init()
return &server{r, a, c, u, f, faas, realtime, isProd}
}
func (s *server) start(port string) error {
portInt, _ := strconv.Atoi(port)
go s.initGRPCServer(strconv.Itoa(portInt + 1))
// Allow cors
corsObj := cors.New(cors.Options{
AllowCredentials: true,
AllowOriginFunc: func(s string) bool {
return true
},
AllowedMethods: []string{"GET", "PUT", "POST", "DELETE"},
AllowedHeaders: []string{"Authorization", "Content-Type"},
ExposedHeaders: []string{"Authorization", "Content-Type"},
})
handler := corsObj.Handler(s.router)
return http.ListenAndServe(":"+port, handler)
}
func (s *server) loadConfig(config *config.Project) error {
// Set the configuration for the auth module
s.auth.SetConfig(config.Secret, config.Modules.Crud, config.Modules.FileStore)
// Set the configuration for the user management module
s.user.SetConfig(config.Modules.Auth)
// Set the configuration for the file storage module
s.file.SetConfig(config.Modules.FileStore)
// Set the configuration for the FaaS module
err := s.faas.SetConfig(config.Modules.FaaS)
if err != nil {
return err
}
// Set the configuration for the Realtime module
s.realtime.SetConfig(config.Modules.Realtime)
// Set the configuration for the curd module
return s.crud.SetConfig(config.Modules.Crud)
}
func (s *server) initGRPCServer(port string) {
lis, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatal("Failed to listen:", err)
}
grpcServer := grpc.NewServer()
pb.RegisterSpaceCloudServer(grpcServer, s)
if err := grpcServer.Serve(lis); err != nil {
log.Fatal("failed to serve:", err)
}
}