-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (137 loc) · 4.78 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"flag"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"webcup/api"
"webcup/gen/auth"
bo "webcup/gen/bo"
bo_contact "webcup/gen/bo_contact"
contacts "webcup/gen/contacts"
data "webcup/gen/data"
fileapi "webcup/gen/fileapi"
files "webcup/gen/files"
jwttoken "webcup/gen/jwt_token"
oauth "webcup/gen/o_auth"
public_users "webcup/gen/public_users"
users "webcup/gen/users"
)
type ApiEndpoints struct {
dataEndpoints *data.Endpoints
contactsEndpoints *contacts.Endpoints
public_usersEndpoints *public_users.Endpoints
usersEndpoints *users.Endpoints
fileapiEndpoints *fileapi.Endpoints
filesEndpoints *files.Endpoints
authEndpoints *auth.Endpoints
jwtTokenEndpoints *jwttoken.Endpoints
oAuthEndpoints *oauth.Endpoints
boEndpoints *bo.Endpoints
boContactEndpoints *bo_contact.Endpoints
}
func main() {
// Setup logger. Replace logger with your own log package of choice.
var (
logger *log.Logger
server *api.Server
)
{
logger = log.New(os.Stderr, fmt.Sprintf("[%v]", "GM API"), log.Ltime)
server = api.NewServer()
}
// Initialize the services.
var (
authSvc auth.Service = api.NewAuth(logger, server)
jwtTokenSvc jwttoken.Service = api.NewJWTToken(logger, server)
dataSvc data.Service = api.NewData(logger, server)
boSvc bo.Service = api.NewBo(logger, server)
contactsSvc contacts.Service = api.NewContacts(logger, server)
public_usersSvc public_users.Service = api.NewPublicUsers(logger, server)
usersSvc users.Service = api.NewUsers(logger, server)
fileapiSvc fileapi.Service = api.NewFileapi(logger)
filesSvc files.Service = api.NewFiles(logger, server)
oAuthSvc oauth.Service = api.NewOAuth(logger, server)
boContactSvc bo_contact.Service = api.NewBoContact(logger, server)
)
// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
var (
apiEndpoints ApiEndpoints = ApiEndpoints{
dataEndpoints: data.NewEndpoints(dataSvc),
contactsEndpoints: contacts.NewEndpoints(contactsSvc),
public_usersEndpoints: public_users.NewEndpoints(public_usersSvc),
usersEndpoints: users.NewEndpoints(usersSvc),
authEndpoints: auth.NewEndpoints(authSvc),
fileapiEndpoints: fileapi.NewEndpoints(fileapiSvc),
filesEndpoints: files.NewEndpoints(filesSvc),
jwtTokenEndpoints: jwttoken.NewEndpoints(jwtTokenSvc),
oAuthEndpoints: oauth.NewEndpoints(oAuthSvc),
boEndpoints: bo.NewEndpoints(boSvc),
boContactEndpoints: bo_contact.NewEndpoints(boContactSvc),
}
)
// Define command line flags, add any other flag required to configure the
// service.
var (
hostF = flag.String("host", server.Config.Domain, "Server host (valid values: localhost)")
domainF = flag.String("domain", "", "Host domain name (overrides host domain specified in service design)")
httpPortF = flag.String("http-port", "", "HTTP port (overrides host HTTP port specified in service design)")
secureF = flag.Bool("secure", server.Config.SSL, "Use secure scheme (https or grpcs)")
dbgF = flag.Bool("debug", false, "Log request and response bodies")
)
flag.Parse()
// Create channel used by both the signal handler and server goroutines
// to notify the main goroutine when to stop the server.
errc := make(chan error)
// Setup interrupt handler. This optional step configures the process so
// that SIGINT and SIGTERM signals cause the services to stop gracefully.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
var addr string = fmt.Sprintf("http://%s", server.Config.Host)
fmt.Printf("API on %s\n", addr)
// Start the servers and send errors (if any) to the error channel.
switch *hostF {
case server.Config.Domain:
{
addr := addr
u, err := url.Parse(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid URL %#v: %s\n", addr, err)
os.Exit(1)
}
if *secureF {
u.Scheme = "https"
}
if *domainF != "" {
u.Host = *domainF
}
if *httpPortF != "" {
h := strings.Split(u.Host, ":")[0]
u.Host = h + ":" + *httpPortF
} else if u.Port() == "" {
u.Host += ":3000"
}
handleHTTPServer(ctx, u, &apiEndpoints, &wg, errc, logger, *dbgF)
}
default:
fmt.Fprintf(os.Stderr, "invalid host argument: %q (valid hosts: %v)\n", *hostF, server.Config.Host)
}
// Wait for signal.
logger.Printf("exiting (%v)", <-errc)
// Send cancellation signal to the goroutines.
cancel()
wg.Wait()
logger.Println("exited")
}