@@ -2,6 +2,7 @@ package main
22
33import (
44 "encoding/json"
5+ "flag"
56 "fmt"
67 "io"
78 "net/http"
@@ -10,18 +11,62 @@ import (
1011 "time"
1112
1213 speakerdeck "github.com/luxas/speakerdeck-scraper"
14+ "github.com/luxas/speakerdeck-scraper/location"
15+ "github.com/luxas/speakerdeck-scraper/scraper"
1316 log "github.com/sirupsen/logrus"
1417)
1518
16- // TODO: Add sample usage text at the "/" endpoint
17- // TODO: Add support for the location extension
19+ const (
20+ prefix = `/api`
21+ welcomeText = `
22+ <h1>Welcome to this Speakerdeck API server!</h1>
23+ <span>Available paths are:</span>
24+ <ul>
25+ <li>/api/users/{user-handle}</li>
26+ <li>/api/talks/{user-handle}</li>
27+ <li>/api/talks/{user-handle}/{talk-id}</li>
28+ </ul>
29+ <br />
30+ <span>Created by Lucas Käldström. Source code at: <a href="https://github.com/luxas/speakerdeck-scraper">github.com/luxas/speakerdeck-scraper</a></span>
31+ `
32+ )
33+
34+ var (
35+ validPaths = regexp .MustCompile (`^` + prefix + `/(talks|users)/([a-zA-Z0-9/-]+)$` )
36+
37+ address = flag .String ("address" , "0.0.0.0" , "What address to expose the API on" )
38+ port = flag .Int ("port" , 8080 , "What port to expose the API on" )
39+ mapsAPIKey = flag .String ("maps-api-key" , "" , "Google Maps API key with the Geocoding API usage set" )
40+
41+ locationExt * location.LocationExtension
42+ )
43+
44+ func main () {
45+ flag .Parse ()
46+ http .HandleFunc ("/" , makeHandler (helpHandler ))
47+ http .HandleFunc (prefix + "/users/" , makeHandler (usersHandler ))
48+ http .HandleFunc (prefix + "/talks/" , makeHandler (talksHandler ))
1849
19- const prefix = `/api`
50+ if len (* mapsAPIKey ) > 0 {
51+ var err error
52+ locationExt , err = location .NewLocationExtension (* mapsAPIKey )
53+ if err != nil {
54+ log .Fatal (err )
55+ }
56+ log .Printf ("Initialized the LocationExtension!" )
57+ }
2058
21- var validPaths = regexp .MustCompile (`^` + prefix + `/(talks|users)/([a-zA-Z0-9/-]+)$` )
59+ addrPort := fmt .Sprintf ("%s:%d" , * address , * port )
60+ log .Printf ("Starting Speakerdeck API on %s..." , addrPort )
61+ log .Fatal (http .ListenAndServe (addrPort , nil ))
62+ }
2263
2364func makeHandler (fn func (http.ResponseWriter , * http.Request , string ) (int , error )) http.HandlerFunc {
2465 return func (w http.ResponseWriter , r * http.Request ) {
66+ if r .URL .Path == "/" {
67+ helpHandler (w , r , "" )
68+ return
69+ }
2570 m := validPaths .FindStringSubmatch (r .URL .Path )
2671 if m == nil {
2772 http .NotFound (w , r )
@@ -44,6 +89,11 @@ func encodeJSON(w io.Writer, data interface{}) error {
4489 return e .Encode (data )
4590}
4691
92+ func helpHandler (w http.ResponseWriter , r * http.Request , _ string ) (int , error ) {
93+ w .Write ([]byte (welcomeText ))
94+ return http .StatusOK , nil
95+ }
96+
4797func usersHandler (w http.ResponseWriter , r * http.Request , userID string ) (int , error ) {
4898 if strings .Contains (userID , "/" ) {
4999 return http .StatusBadRequest , fmt .Errorf ("invalid user name, can't contain /" )
@@ -71,7 +121,14 @@ func talksHandler(w http.ResponseWriter, r *http.Request, talkStr string) (int,
71121 talkID = parts [1 ]
72122 }
73123
74- talks , err := speakerdeck .ScrapeTalks (userID , talkID , nil )
124+ var opts * scraper.ScrapeOptions
125+ if locationExt != nil {
126+ opts = & scraper.ScrapeOptions {
127+ Extensions : []scraper.Extension {locationExt },
128+ }
129+ }
130+
131+ talks , err := speakerdeck .ScrapeTalks (userID , talkID , opts )
75132 if err != nil {
76133 return http .StatusInternalServerError , err
77134 }
@@ -81,12 +138,3 @@ func talksHandler(w http.ResponseWriter, r *http.Request, talkStr string) (int,
81138 }
82139 return http .StatusOK , nil
83140}
84-
85- func main () {
86- http .HandleFunc (prefix + "/users/" , makeHandler (usersHandler ))
87- http .HandleFunc (prefix + "/talks/" , makeHandler (talksHandler ))
88-
89- log .Printf ("Starting Speakerdeck API..." )
90- // TODO: Parameterize this
91- log .Fatal (http .ListenAndServe (":8080" , nil ))
92- }
0 commit comments