forked from pjvds/promise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (51 loc) · 1.54 KB
/
Copy pathmain.go
File metadata and controls
60 lines (51 loc) · 1.54 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
package main
import (
log "code.google.com/p/log4go"
"github.com/pjvds/promise/controller"
"github.com/pjvds/promise/data/mongo"
"github.com/pjvds/promise/messaging"
"github.com/pjvds/promise/serialization"
"labix.org/v2/mgo"
"net/http"
"time"
)
func main() {
server, err := mgo.Dial("localhost")
if err != nil {
log.Error("Unable to dail to mongo: %v", err)
log.Error("Are you sure mongod is running?")
} else {
database := server.DB("promise")
session := mongo.NewMongoPromiseSession(server, database)
repoFac := mongo.NewMongoPromiseRepositoryFactory(session)
repo := repoFac.CreatePromiseTicketRepository()
marsh := serialization.NewJsonMarshaller()
bus := messaging.NewNsqBus()
ctrl := controller.NewPromiseTicketController(repo, bus, marsh)
Serve(&ServeInfo{
uri: ":8080",
promiseTicketController: ctrl,
})
}
// Let log4go flush
time.Sleep(time.Second)
}
type ServeInfo struct {
uri string
promiseTicketController *controller.PromiseTicketController
}
func Log(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Info("%s %s %s", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func Serve(info *ServeInfo) {
ctrl := info.promiseTicketController
http.Handle("/", http.FileServer(http.Dir("static")))
http.HandleFunc("/api/v1/promise", func(w http.ResponseWriter, r *http.Request) {
ctrl.Handle(w, r)
})
log.Info("Serving at " + info.uri)
log.Critical(http.ListenAndServe(info.uri, Log(http.DefaultServeMux)))
}