-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
48 lines (41 loc) · 1.28 KB
/
server.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
package main
import (
"net/http"
"path/filepath"
"github.com/machinebox/sdk-go/textbox"
"github.com/machinebox/twitterfeed"
"github.com/matryer/way"
)
// Server is the app server.
type Server struct {
assets string
tweetReader *twitterfeed.TweetReader
textbox *textbox.Client
router *way.Router
}
// New makes a new Server.
func NewServer(assets string, tweetReader *twitterfeed.TweetReader, textbox *textbox.Client) *Server {
srv := &Server{
assets: assets,
tweetReader: tweetReader,
textbox: textbox,
router: way.NewRouter(),
}
srv.router.HandleFunc(http.MethodGet, "/analysis", srv.handleAnalysis)
srv.router.Handle(http.MethodGet, "/assets/", Static("/assets/", assets))
srv.router.HandleFunc(http.MethodGet, "/", srv.handleIndex)
return srv
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}
func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(s.assets, "index.html"))
}
// Static gets a static file server for the specified path.
func Static(stripPrefix, dir string) http.Handler {
h := http.StripPrefix(stripPrefix, http.FileServer(http.Dir(dir)))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
})
}