forked from gliderlabs/logspout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logspout.go
80 lines (66 loc) · 1.57 KB
/
logspout.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/fsouza/go-dockerclient"
"github.com/gliderlabs/logspout/router"
)
var Version string
func debug(v ...interface{}) {
if getopt("DEBUG", "") != "" {
log.Println(v...)
}
}
func assert(err error, context string) {
if err != nil {
log.Fatal(context+": ", err)
}
}
func getopt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "--version" {
fmt.Println(Version)
os.Exit(0)
}
port := getopt("PORT", "8000")
endpoint := getopt("DOCKER_HOST", "unix:///tmp/docker.sock")
routespath := getopt("ROUTESPATH", "/mnt/routes")
client, err := docker.NewClient(endpoint)
assert(err, "docker")
pump := router.NewLogsPump(client)
routes := router.NewRouteManager(pump)
var uris string
if os.Getenv("ROUTE_URIS") != "" {
uris = os.Getenv("ROUTE_URIS")
}
if len(os.Args) > 1 {
uris = os.Args[1]
}
if uris != "" {
for _, uri := range strings.Split(uris, ",") {
log.Println("routing all to " + uri)
routes.AddFromUri(uri)
}
}
if _, err := os.Stat(routespath); err == nil {
log.Println("loading and persisting routes in " + routespath)
assert(routes.Load(router.RouteFileStore(routespath)), "persistor")
}
for name, handler := range router.HttpHandlers.All() {
h := handler(routes, pump)
http.Handle("/"+name, h)
http.Handle("/"+name+"/", h)
}
go pump.Pump()
log.Printf("logspout %s serving http on :%s", Version, port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}