-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasic-application.go
59 lines (47 loc) · 1.36 KB
/
basic-application.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/VoycerAG/gridfs-image-server/server"
"gopkg.in/mgo.v2"
)
var (
configurationFilepath *string
serverPort *int
host *string
newrelicKey *string
)
func init() {
configurationFilepath = flag.String("config", "configuration.json", "path to the configuration file")
serverPort = flag.Int("port", 8000, "the server port where we will serve images")
host = flag.String("host", "localhost:27017", "the database host with an optional port, localhost would suffice")
newrelicKey = flag.String("license", "", "your newrelic license key in order to enable monitoring")
}
func run(mongoHost, configFile, newrelicToken string, port int) {
session, err := mgo.Dial(*host)
if err != nil {
log.Fatal(err)
return
}
config, err := server.NewConfigFromFile(configFile)
if err != nil {
log.Fatal(err)
return
}
session.SetSyncTimeout(0)
session.SetMode(mgo.Eventual, true)
storage, err := server.NewGridfsStorage(session)
if err != nil {
log.Fatal(err)
return
}
imageServer := server.NewImageServerWithNewRelic(config, storage, newrelicToken)
handler := imageServer.Handler()
log.Printf("Server started. Listening on %d database host is %s\n", port, mongoHost)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), handler)
if err != nil {
log.Fatal(err)
}
}