forked from alexandrebrg/go-clearkey-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (118 loc) · 4.02 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
package main
import (
"fmt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/spf13/viper"
plogger "gitlab.com/protocole/clearkey/internal/core/ports/logger"
"gitlab.com/protocole/clearkey/internal/core/ports/repositories"
"gitlab.com/protocole/clearkey/internal/core/services"
"gitlab.com/protocole/clearkey/internal/handlers"
zlogger "gitlab.com/protocole/clearkey/internal/loggers"
"gitlab.com/protocole/clearkey/internal/repositories/memory"
"gitlab.com/protocole/clearkey/internal/repositories/postgresql"
"gitlab.com/protocole/clearkey/pkg/apperrors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
err := loadEnvironment()
if err != nil {
log.Fatal(err)
return
}
// only choice available for now is zlogger
plogger.SetLogger(zlogger.NewZLogger(viper.GetString("EnvType")))
service := services.NewService(chooseRepository())
handler := handlers.NewHandler(service)
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
r.Use(cors.Handler(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: viper.GetStringSlice("Domains"),
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
r.Post("/license", handler.GetKey)
r.Post("/license/register", handler.PostKey)
errs := make(chan error, 2)
go func() {
plogger.Log.Infof("Application is now listening on %s:%s", viper.GetString("Ip"), viper.GetString("Port"))
ip := fmt.Sprintf("%s:%s", viper.GetString("Ip"), viper.GetString("Port"))
errs <- http.ListenAndServe(ip, r)
}()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()
plogger.Log.Errorf(fmt.Sprintf("\nTerminated %s\n", <-errs))
}
/*
This function use the native logger as we load the logger after this method
*/
func loadEnvironment() error {
// This corresponds to key ID & env var name
envKeys := map[string]string{
"EnvType": "ENV",
"Port": "PORT",
"Ip": "IP",
"Domains": "ALLOWED_DOMAINS",
"Repository": "REPOSITORY_TYPE",
"Psql_pass": "PSQL_PASSWORD",
"Psql_user": "PSQL_USER",
"Psql_addr": "PSQL_ADDR",
"Psql_db": "PSQL_DB",
"Psql_insecure": "PSQL_INSECURE",
}
for keyId, keyValue := range envKeys {
if err := viper.BindEnv(keyId, keyValue); err != nil {
log.Fatalf("Failed to boot up environment variables, reason: %s", err.Error())
return apperrors.EnvVarLoadFailed
}
}
viper.SetDefault("EnvType", "development")
viper.SetDefault("Port", 8080)
viper.SetDefault("Ip", "0.0.0.0")
viper.SetDefault("ALLOWED_DOMAINS", []string{"http://localhost:*", "http://127.0.0.1:*"})
viper.SetDefault("Repository", "RAM")
viper.SetDefault("Psql_pass", "postgres")
viper.SetDefault("Psql_user", "postgres")
viper.SetDefault("Psql_addr", "127.0.0.1:5433")
viper.SetDefault("Psql_db", "postgres")
viper.SetDefault("Psql_insecure", true)
return nil
}
func chooseRepository() repositories.KeyStorageRepository {
repositoryType := viper.GetString("Repository")
plogger.Log.Infof("Selected repository is %s", repositoryType)
switch repositoryType {
case "RAM":
return memory.NewMemoryRepository()
case "PSQL":
repository, err := postgresql.NewPostgreSQLRepository(
viper.GetString("Psql_user"),
viper.GetString("Psql_pass"),
viper.GetString("Psql_db"),
viper.GetString("Psql_addr"),
viper.GetBool("Psql_insecure"),
)
if err != nil {
plogger.Log.Fatalf(err.Error())
}
return repository
}
plogger.Log.Fatalf("Invalid repository %s, please choose between the available ones", repositoryType)
return nil
}