-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
117 lines (104 loc) · 3.26 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
package main
import (
"flag"
"fmt"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"golang.org/x/sync/errgroup"
"log"
"net/http"
"os"
)
var (
serveHost = flag.String("serve_host", getEnv("SERVER_HOST", ""),
"Host to serve requests incoming to Instagram Provider")
servePort = flag.String("serve_port", getEnv("SERVER_PORT", "8080"),
"Port to serve requests incoming to Instagram Provider")
dcUserId = flag.String("dc_user_id", getEnv("DC_USER_ID", ""), "")
sessionID = flag.String("session_id", getEnv("SESSION_ID", ""), "")
csrfToken = flag.String("csrf_token", getEnv("CSRF_TOKEN", ""), "")
youTubeApiKey = flag.String("youtube_api_key", getEnv("YOUTUBE_API_KEY", ""), "")
g errgroup.Group
instagramHandler InstagramHandler
youtubeHandler YoutubeHandler
monitoringHandler MonitoringHandler
twitterHandler TwitterHandler
)
func main() {
instagramHandler = newInstagramHandler(*dcUserId, *sessionID, *csrfToken)
youtubeHandler = newYouTubeHandler(*youTubeApiKey)
monitoringHandler = NewMonitoringHandler(youtubeHandler, instagramHandler, twitterHandler)
twitterHandler = NewTwitterHandler()
medias, _ := instagramHandler.PrivateAPIManager.GetAllPostMedia("nc_ficus")
log.Print(medias[0].DisplayUrl)
mainEndpoints := &http.Server{
Addr: fmt.Sprintf("%s:%s", *serveHost, *servePort),
Handler: handler(),
}
g.Go(func() error {
return mainEndpoints.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func handler() http.Handler {
r := mux.NewRouter()
handler := Handler()
r.Handle("/api/instagram/posts/{username}/{last}",
handlers.LoggingHandler(
os.Stdout,
handler(instagramHandler.handlePostsRequest())),
).Methods("GET")
r.Handle("/api/instagram/posts/{username}",
handlers.LoggingHandler(
os.Stdout,
handler(instagramHandler.handlePostsRequest())),
).Methods("GET")
r.Handle("/api/instagram/stories/{username}",
handlers.LoggingHandler(
os.Stdout,
handler(instagramHandler.handleStoriesRequest())),
).Methods("GET")
r.Handle("/api/instagram/stories/{username}/{last}",
handlers.LoggingHandler(
os.Stdout,
handler(instagramHandler.handleStoriesRequest())),
).Methods("GET")
r.Handle("/api/youtube/{channel}",
handlers.LoggingHandler(
os.Stdout,
handler(youtubeHandler.fetchLastVideos())),
).Methods("GET")
r.Handle("/api/twitter/{user}",
handlers.LoggingHandler(
os.Stdout,
handler(twitterHandler.fetchNewTweets())),
).Methods("GET")
r.Handle("/health",
handlers.LoggingHandler(
os.Stdout,
handler(monitoringHandler.handleHealthRequest())),
).Methods("GET")
return JsonContentType(handlers.CompressHandler(r))
}
func Handler() func(func(w http.ResponseWriter, r *http.Request)) http.Handler {
return func(f func(w http.ResponseWriter, r *http.Request)) http.Handler {
h := http.HandlerFunc(f)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
})
}
}
func JsonContentType(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
h.ServeHTTP(w, r)
})
}