-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (66 loc) · 2.62 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
package main
import (
ApiClient "github.com/aeternas/SwadeshNess/apiClient"
ClientMiddlewares "github.com/aeternas/SwadeshNess/clientMiddlewares"
Config "github.com/aeternas/SwadeshNess/configuration"
. "github.com/aeternas/SwadeshNess/handlers"
HTTPApiClient "github.com/aeternas/SwadeshNess/httpApiClient"
ServerMiddlewares "github.com/aeternas/SwadeshNess/serverMiddlewares"
Wrappers "github.com/aeternas/SwadeshNess/wrappers"
"log"
"net/http"
"time"
)
var (
reader Config.AnyReader
translationHandler AnyHandler
groupListHandler AnyHandler
versionHandler AnyHandler
configuration Config.Configuration
apiClient ApiClient.ApiClient
)
const (
DATABASE_CONFIG = "configuration/db/db.json"
)
func init() {
var wrapper = Wrappers.New(new(Wrappers.OsWrapper))
var lReader *Config.Reader = &Config.Reader{Path: DATABASE_CONFIG, OsWrapper: wrapper}
reader = lReader
lConfiguration, _ := reader.ReadConfiguration()
configuration = lConfiguration
clientMiddlewares := []ClientMiddlewares.ClientMiddleware{
ClientMiddlewares.NewCachingDefaultClientMiddleware(&configuration),
ClientMiddlewares.NewDefaultClientMiddleware(),
ClientMiddlewares.NewAuthClientMiddleware(configuration.ApiKey),
ClientMiddlewares.NewLoggerClientMiddleware(),
}
httpApiClient := &HTTPApiClient.HTTPApiClient{Client: &http.Client{Timeout: 10 * time.Second}, Middlewares: clientMiddlewares}
apiClient = httpApiClient
csm := ServerMiddlewares.NewCachingDefaultServerMiddleware(&configuration)
lsm := ServerMiddlewares.NewLoggerServerMiddleware()
log.Println(csm)
log.Println(lsm)
translationHandler = &TranslationHandler{
Config: &configuration,
ServerMiddlewares: []ServerMiddlewares.ServerMiddleware{csm},
ApiClient: apiClient,
}
groupListHandler = &GroupListHandler{Config: &configuration, ServerMiddlewares: []ServerMiddlewares.ServerMiddleware{lsm}}
versionHandler = &VersionHandler{Config: &configuration}
}
func main() {
http.HandleFunc(configuration.EEndpoints.GroupsEndpoint, func(w http.ResponseWriter, r *http.Request) {
groupListHandler.HandleRequest(w, r)
})
http.HandleFunc(configuration.EEndpoints.TranslationEndpoint, func(w http.ResponseWriter, r *http.Request) {
translationHandler.HandleRequest(w, r)
})
http.HandleFunc(configuration.EEndpoints.VersionEndpoint, func(w http.ResponseWriter, r *http.Request) {
versionHandler.HandleRequest(w, r)
})
if configuration.Security.NeedsHTTPS {
log.Fatal(http.ListenAndServeTLS(":8080", configuration.Security.ServerCertPath, configuration.Security.ServerKeyPath, nil))
} else {
log.Fatal(http.ListenAndServe(":8081", nil))
}
}