-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (67 loc) · 1.82 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
// Make sure you change this line to match your module
"github.com/andreev1024/ndiploma/apiserver"
"github.com/andreev1024/ndiploma/storage"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/gorilla/sessions"
)
const (
apiServerAddrFlagName string = "addr"
apiServerStorageDatabaseURL string = "database-url"
)
func main() {
if err := app().Run(os.Args); err != nil {
logrus.WithError(err).Fatal("could not run application")
}
}
func app() *cli.App {
return &cli.App{
Name: "api-server",
Usage: "The API",
Commands: []*cli.Command{
apiServerCmd(),
},
}
}
func apiServerCmd() *cli.Command {
return &cli.Command{
Name: "start",
Usage: "starts the API server",
Flags: []cli.Flag{
&cli.StringFlag{Name: apiServerAddrFlagName, EnvVars: []string{"API_SERVER_ADDR"}},
&cli.StringFlag{Name: apiServerStorageDatabaseURL, EnvVars: []string{"DATABASE_URL"}},
},
Action: func(c *cli.Context) error {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
stopper := make(chan struct{})
go func() {
<-done
close(stopper)
}()
databaseURL := c.String(apiServerStorageDatabaseURL)
s, err := storage.NewStorage(databaseURL)
if err != nil {
return fmt.Errorf("could not initialize storage: %w", err)
}
var (
//TODO move to env var
// key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)
key = []byte("super-secret-key")
sessionsStore = sessions.NewCookieStore(key)
)
addr := c.String(apiServerAddrFlagName)
server, err := apiserver.NewAPIServer(addr, s, sessionsStore)
if err != nil {
return err
}
return server.Start(stopper)
},
}
}