-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (55 loc) · 1.85 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
package main
import (
"log"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog"
"github.com/go-chi/render"
"github.com/molson82/saturn-go/config"
"github.com/molson82/saturn-go/controller"
"github.com/newrelic/go-agent/v3/newrelic"
)
func routes(c *config.Config) *chi.Mux {
r := chi.NewRouter()
// Middleware
logger := httplog.NewLogger("sci-logger", httplog.Options{
JSON: true,
Concise: true,
LogLevel: "info",
})
r.Use(render.SetContentType(render.ContentTypeJSON),
middleware.RequestID,
middleware.RedirectSlashes,
httplog.RequestLogger(logger),
middleware.SetHeader("Cache-Control", "max-age=86400"),
middleware.Recoverer)
r.Route("/view", func(r chi.Router) {
r.Mount(newrelic.WrapHandle(c.NewRelicApp, "/index", controller.GetIndexPage(c)))
r.Mount(newrelic.WrapHandle(c.NewRelicApp, "/blog", controller.GetBlogPage(c)))
})
r.Route("/api", func(r chi.Router) {
r.Mount(newrelic.WrapHandle(c.NewRelicApp, "/callback", controller.CallbackRoutes(c)))
r.Mount(newrelic.WrapHandle(c.NewRelicApp, "/redis", controller.GetRedisRoutes(c)))
r.Mount(newrelic.WrapHandle(c.NewRelicApp, "/ws", controller.WebSocketRoutes(c)))
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/view/index", http.StatusSeeOther)
})
r.Mount(newrelic.WrapHandle(c.NewRelicApp, "/static/", http.StripPrefix(strings.TrimRight("/static/", "/"), http.FileServer(http.Dir("./static")))))
return r
}
func main() {
config := config.New()
router := routes(config)
//token, _ := model.GetOAuthAccessToken(config)
//log.Printf("Token: %v", token)
port := config.Constants.Port
log.Printf("PORT: %v\n", port)
if port == "" {
log.Fatal("Port must be set")
}
log.Printf("Golang API and Web App running...\n")
log.Fatal(http.ListenAndServe(":"+port, router))
}