-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradingpostserver.go
46 lines (38 loc) · 983 Bytes
/
tradingpostserver.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
package tradingpostserver
import (
"fmt"
"net/http"
"github.com/royvandewater/trading-post/auth0creds"
mgo "gopkg.in/mgo.v2"
)
// TradingPostServer defines the interface for a
// trading post server
type TradingPostServer interface {
// Run causes the server to start serving up.
// The server will run forever unless some error
// occurs
Run() error
}
// New instantiates a new TradingPostServer instance
func New(auth0Creds auth0creds.Auth0Creds, mongoDBURL string, port int) TradingPostServer {
return &httpServer{
auth0Creds: auth0Creds,
port: port,
mongoDBURL: mongoDBURL,
}
}
type httpServer struct {
auth0Creds auth0creds.Auth0Creds
mongoDBURL string
port int
}
func (server *httpServer) Run() error {
mongoDB, err := mgo.Dial(server.mongoDBURL)
if err != nil {
return err
}
defer mongoDB.Close()
addr := fmt.Sprintf(":%v", server.port)
router := newRouter(server.auth0Creds, mongoDB)
return http.ListenAndServe(addr, router)
}