-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
102 lines (89 loc) · 2.08 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/Ahimta/tweeters-stats-golang/auth"
"github.com/Ahimta/tweeters-stats-golang/config"
"github.com/Ahimta/tweeters-stats-golang/handlers"
"github.com/Ahimta/tweeters-stats-golang/middleware"
"github.com/Ahimta/tweeters-stats-golang/services"
"github.com/Ahimta/tweeters-stats-golang/usecases"
newrelic "github.com/newrelic/go-agent"
)
func main() {
c, err := config.New(
os.Getenv("CONSUMER_KEY"),
os.Getenv("CONSUMER_SECRET"),
os.Getenv("CALLBACK_URL"),
os.Getenv("PORT"),
os.Getenv("HOMEPAGE"),
os.Getenv("HOST"),
os.Getenv("PROTOCOL"),
os.Getenv("CORS_DOMAIN"),
)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var app newrelic.Application
if licenseKey := os.Getenv("NEW_RELIC_LICENSE_KEY"); licenseKey != "" {
config := newrelic.NewConfig("tweeters-stats-golang", licenseKey)
app, err = newrelic.NewApplication(config)
if err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
}
oauthClient, err := auth.NewOauth1Client(
c.ConsumerKey,
c.ConsumerSecret,
c.CallbackURL,
)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
tweetsService := services.NewTweetsService(oauthClient)
mux := http.NewServeMux()
route(mux, app, "/health-check", handlers.HealthCheck())
route(mux, app, "/", handlers.Homepage("index.html"))
route(mux, app, "/login/twitter", handlers.Login(usecases.Login, oauthClient))
route(
mux,
app,
"/oauth/twitter/callback",
handlers.OauthTwitter(
usecases.Oauth1Callback,
c,
oauthClient,
),
)
route(mux, app, "/logout", handlers.Logout())
route(
mux,
app,
"/tweeters-stats",
handlers.TweetersStats(
usecases.TweetersStats,
tweetsService,
),
)
fmt.Printf("Server running on %s://%s\n", c.Protocol, c.Host)
http.ListenAndServe(
fmt.Sprintf(":%s", c.Port),
middleware.Apply(mux, os.Stdout, c),
)
}
func route(
mux *http.ServeMux,
app newrelic.Application,
path string,
handler http.HandlerFunc,
) {
if app == nil {
mux.HandleFunc(path, handler)
return
}
mux.HandleFunc(newrelic.WrapHandleFunc(app, path, handler))
}