forked from jsdir/blarg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
129 lines (109 loc) · 2.94 KB
/
Copy pathserver.go
File metadata and controls
129 lines (109 loc) · 2.94 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/gob"
"os"
"time"
"github.com/garyburd/go-oauth/oauth"
"github.com/garyburd/redigo/redis"
"github.com/pjebs/tokbox"
"gopkg.in/boj/redistore.v1"
)
const (
Development = iota
Production
)
// Environment Variables:
// BLARG_ENV
// TWITTER_KEY
// TWITTER_SECRET_KEY
// REDIS_PORT_6379_TCP_ADDR
// REDIS_PORT_6379_TCP_POST
// REDIS_PASSWORD
type Server struct {
RediStore *redistore.RediStore
RedisPool *redis.Pool
OAuthClient oauth.Client
State State
ClientBaseUrl string
ForceHTTPS bool
TokBox *tokbox.Tokbox
TokBoxKey string
}
func newPool(addr, password string) *redis.Pool {
return &redis.Pool{
// Maximum number of idle connections in the pool.
MaxIdle: 100,
// Maximum number of connections allocated by the pool at a given time.
// When zero, there is no limit on the number of connections in the pool.
MaxActive: 100,
// Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
IdleTimeout: 240 * time.Second,
// Dial is an application supplied function for creating and configuring a
// connection
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", addr)
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
return c, nil
},
}
}
func NewServer() Server {
// Get environment
environment := Development
if os.Getenv("BLARG_ENV") == "production" {
environment = Production
}
// Redis
redisAddr := os.Getenv("REDIS_PORT_6379_TCP_ADDR") +
":" + os.Getenv("REDIS_PORT_6379_TCP_PORT")
redisPool := newPool(redisAddr, os.Getenv("REDIS_PASSWORD"))
// Redis store
rediStore, err := redistore.NewRediStoreWithPool(redisPool, []byte("secret string "))
if err != nil {
panic(err)
}
// TokBox
tokBoxKey := os.Getenv("TOKBOX_KEY")
tb := tokbox.New(
tokBoxKey,
os.Getenv("TOKBOX_SECRET_KEY"),
)
// OAuth
gob.Register(&oauth.Credentials{})
oauthClient := oauth.Client{
TemporaryCredentialRequestURI: "https://api.twitter.com/oauth/request_token",
ResourceOwnerAuthorizationURI: "https://api.twitter.com/oauth/authenticate",
TokenRequestURI: "https://api.twitter.com/oauth/access_token",
}
oauthClient.Credentials.Token = os.Getenv("TWITTER_KEY")
oauthClient.Credentials.Secret = os.Getenv("TWITTER_SECRET_KEY")
// State
state := NewLocalState()
// Config
clientBaseUrl := "http://localhost:8000"
forceHTTPS := false
if environment == Production {
clientBaseUrl = "https://blarg-im.herokuapp.com"
forceHTTPS = true
}
return Server{
RedisPool: redisPool,
RediStore: rediStore,
OAuthClient: oauthClient,
State: &state,
ClientBaseUrl: clientBaseUrl,
ForceHTTPS: forceHTTPS,
TokBox: tb,
TokBoxKey: tokBoxKey,
}
}