/
main.go
57 lines (43 loc) · 1.16 KB
/
main.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
package main
import (
"net/http"
"os"
"github.com/dancannon/gorethink"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"golang.org/x/net/context"
)
var logger log.Logger
func main() {
// setup a logger
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.NewContext(logger).With("svc", "sensor").With("caller", log.DefaultCaller)
// setup rethinkdb session
session := getSession()
// build the context
ctx := context.Background()
// new service
svc := NewSensorService(session)
// wrap it in the logging middleware
svc = loggingMiddleware(logger)(svc)
// bind the service to HTTP with the context
// with it's matching encoder/decoder
recordHandler := httptransport.NewServer(
ctx,
makeRecordEndpoint(svc),
decodeRecordRequest,
encodeResponse,
)
// assign an endpoint route
http.Handle("/sensor/record", recordHandler)
// bind the listener
logger.Log("msg", "HTTP", "addr", ":5000")
logger.Log("err", http.ListenAndServe(":5000", nil))
}
func getSession() *gorethink.Session {
session, _ := gorethink.Connect(gorethink.ConnectOpts{
Address: "localhost:28015",
Database: "sensord",
})
return session
}