/
server.go
110 lines (87 loc) · 2.88 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
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
package main
import (
"github.com/codegangsta/negroni"
"flag"
"os"
"encoding/json"
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/unrolled/render"
"github.com/meatballhat/negroni-logrus"
"github.com/go-zoo/bone"
"github.com/garyburd/redigo/redis"
)
// Initial structure of configuration that is expected from conf.json file
type Configuration struct {
MirageEndpoint string
ExternalSystem string
}
// AppConfig stores application configuration
var AppConfig Configuration
// Client structure to be injected into functions to perform HTTP calls
type Client struct {
HTTPClient *http.Client
}
// HTTPClientHandler used for passing http client connection and template
// information back to handlers, mostly for testing purposes
type HTTPClientHandler struct {
http Client
r *render.Render
pool *redis.Pool
}
var (
redisAddress = flag.String("redis-address", ":6379", "Address to the Redis server")
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis")
// like ./twitter-proxy -port=":8080" would start on port 8080
port = flag.String("port", ":8300", "Server port")
)
func main() {
// Output to stderr instead of stdout, could also be a file.
log.SetOutput(os.Stderr)
log.SetFormatter(&log.TextFormatter{})
// getting configuration
file, err := os.Open("conf.json")
if err != nil {
log.Panic("Failed to open configuration file, quiting server.")
}
decoder := json.NewDecoder(file)
AppConfig = Configuration{}
err = decoder.Decode(&AppConfig)
if err != nil {
log.WithFields(log.Fields{"Error": err.Error()}).Panic("Failed to read configuration")
}
// app starting
log.WithFields(log.Fields{
"MirageEndpoint": AppConfig.MirageEndpoint,
"ExternalSystemEndpoint": AppConfig.ExternalSystem,
}).Info("app is starting")
flag.Parse() // parse the flags
// getting base template and handler struct
r := render.New(render.Options{Layout: "layout"})
// getting redis client for state storing
redisPool := redis.NewPool(func() (redis.Conn, error) {
c, err := redis.Dial("tcp", *redisAddress)
if err != nil {
log.WithFields(log.Fields{"Error": err.Error()}).Panic("Failed to create Redis connection pool!")
return nil, err
}
return c, err
}, *maxConnections)
defer redisPool.Close()
h := HTTPClientHandler{http: Client{&http.Client{}}, r: r, pool: redisPool}
mux := getBoneRouter(h)
n := negroni.Classic()
n.Use(negronilogrus.NewMiddleware())
n.UseHandler(mux)
n.Run(*port)
}
func getBoneRouter(h HTTPClientHandler) *bone.Mux {
mux := bone.New()
mux.Get("/1.1/search/tweets.json", http.HandlerFunc(h.tweetSearchEndpoint))
mux.Get("/admin", http.HandlerFunc(h.adminHandler))
mux.Post("/admin/state", http.HandlerFunc(h.stateHandler))
mux.Get("/admin/state", http.HandlerFunc(h.getStateHandler))
// handling static files
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
return mux
}