-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (61 loc) · 2.01 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/rs/cors"
"github.com/talkative-ai/brahman/routes"
"github.com/talkative-ai/core/db"
"github.com/talkative-ai/core/redis"
"github.com/talkative-ai/core/router"
"github.com/talkative-ai/go-alexa/skillserver"
"github.com/gorilla/mux"
)
func wrapRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.String() != "/healthz" {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
}
handler.ServeHTTP(w, r)
})
}
func main() {
// Initialize database and redis connections
// TODO: Make it a bit clearer that this is happening, and more maintainable
err := db.InitializeDB()
if err != nil {
fmt.Println(err)
return
}
defer db.Instance.Close()
_, err = redis.ConnectRedis()
if err != nil {
fmt.Println(err)
return
}
defer redis.Instance.Close()
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
r := mux.NewRouter()
router.ApplyRoute(r, routes.PostGoogle)
router.ApplyRoute(r, routes.PostDemo)
router.ApplyRoute(r, routes.PostGoogleAuth)
router.ApplyRoute(r, routes.PostGoogleAuthToken)
skillserver.SetEchoPrefix("/ai/v1/alexa/")
skillserver.Init(map[string]interface{}{
routes.PostAlexa.Path: skillserver.EchoApplication{
Handler: routes.PostAlexaHandler,
},
}, r)
c := cors.New(cors.Options{
AllowedOrigins: []string{"https://talkative.ai", "https://harihara.ngrok.io", "http://brahman.ngrok.io", "https://brahman.ngrok.io", "https://workbench.talkative.ai", "http://localhost:3000", "http://localhost:8080", "http://localhost:3001"},
AllowCredentials: true,
AllowedHeaders: []string{"x-token", "accept", "content-type"},
ExposedHeaders: []string{"etag", "x-token"},
AllowedMethods: []string{"GET", "PATCH", "POST", "PUT"},
})
http.Handle("/", c.Handler(r))
log.Println("Brahman starting server on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", wrapRequest(http.DefaultServeMux)))
}