-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
81 lines (74 loc) · 2.25 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
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"time"
"github.com/intervention-engine/fhir/models"
"github.com/intervention-engine/fhir/upload"
"github.com/intervention-engine/riskservice/assessments"
"github.com/intervention-engine/riskservice/server"
"github.com/intervention-engine/riskservice/service"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"gopkg.in/mgo.v2"
)
func main() {
// Check for a linked MongoDB container if we are running in Docker
mongoHost := os.Getenv("MONGO_PORT_27017_TCP_ADDR")
if mongoHost == "" {
mongoHost = "localhost"
}
registerURL := flag.String("registerURL", "", "Register a FHIR Subscription to the specified URL")
registerENV := flag.String("registerENV", "", "Register a FHIR Subscription to the the Docker environment variable IE_PORT_3001_TCP*")
flag.Parse()
parsedURL := *registerURL
if parsedURL != "" {
registerServer(parsedURL)
}
if registerENV != nil {
registerServer(fmt.Sprintf("http://%s:%s", os.Getenv("IE_PORT_3001_TCP_ADDR"), os.Getenv("IE_PORT_3001_TCP_PORT")))
}
e := echo.New()
session, err := mgo.Dial(mongoHost)
if err != nil {
panic("Can't connect to the database")
}
defer session.Close()
basePieURL := discoverSelf() + "pies"
db := session.DB("riskservice")
svc := service.NewReferenceRiskService(db)
svc.RegisterPlugin(assessments.NewCHA2DS2VAScPlugin())
svc.RegisterPlugin(assessments.NewSimplePlugin())
fnDelayer := server.NewFunctionDelayer(3 * time.Second)
server.RegisterRoutes(e, db, basePieURL, svc, fnDelayer)
e.Use(middleware.Logger())
e.Run(":9000")
}
func discoverSelf() string {
var ip net.IP
var selfURL string
host, err := os.Hostname()
if err != nil {
panic(err)
}
addrs, err := net.LookupIP(host)
if err != nil {
log.Println("Unable to lookup IP based on hostname, defaulting to localhost.")
selfURL = "http://localhost:9000/"
}
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
ip = ipv4
selfURL = "http://" + ip.String() + ":9000/"
}
}
return selfURL
}
func registerServer(registerString string) {
channel := &models.SubscriptionChannelComponent{Type: "rest-hook", Endpoint: discoverSelf() + "calculate"}
sub := models.Subscription{Channel: channel}
upload.UploadResource(&sub, registerString)
}