This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
longshore.go
141 lines (112 loc) · 4.03 KB
/
longshore.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main // import "github.com/newsdev/longshore"
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/newsdev/longshore/vendor/src/github.com/go-mgo/mgo"
"github.com/newsdev/longshore/builder"
)
// containment
var Config struct {
WebhookAddress, KeyAddress, CachePath, KeyPath, RegistryPrefix, Users, Branches string
// Slack configuration options.
Slack struct {
URL string
}
// MongoDB configuration options.
MongoDB struct {
Servers, Database, Username, Password string
SSL bool
}
}
func init() {
flag.StringVar(&Config.WebhookAddress, "w", ":5000", "webhook listen address")
flag.StringVar(&Config.KeyAddress, "k", "127.0.0.1:5001", "key listen address")
flag.StringVar(&Config.CachePath, "p", "/tmp/longshore/cache", "root path for git caches")
flag.StringVar(&Config.KeyPath, "q", "/tmp/longshore/keys", "root path for private keys")
flag.StringVar(&Config.RegistryPrefix, "r", "", "registry prefix")
flag.StringVar(&Config.Users, "u", "", "users")
flag.StringVar(&Config.Branches, "b", "master,develop", "branches")
// Slack configuration options.
flag.StringVar(&Config.Slack.URL, "slack-url", os.Getenv("SLACK_URL"), "slack webhook URL")
// MongoDB configuration options.
flag.StringVar(&Config.MongoDB.Servers, "mongodb-servers", os.Getenv("MONGODB_SERVERS"), "comma-seperated list of MongoDB server addresses")
flag.StringVar(&Config.MongoDB.Database, "mongodb-database", os.Getenv("MONGODB_DATABASE"), "MongoDB database to use")
flag.StringVar(&Config.MongoDB.Username, "mongodb-username", os.Getenv("MONGODB_USERNAME"), "MongoDB username")
flag.StringVar(&Config.MongoDB.Password, "mongodb-password", os.Getenv("MONGODB_PASSWORD"), "MongoDB password")
flag.BoolVar(&Config.MongoDB.SSL, "mongodb-ssl", false, "use SSL for MongoDB connections")
}
func status(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprint(w, "ok")
}
var f = status
func main() {
flag.Parse()
// Construct a builder configuration object.
config := &builder.Config{
CachePath: Config.CachePath,
KeyPath: Config.KeyPath,
RegistryPrefix: Config.RegistryPrefix,
Users: strings.Split(Config.Users, ","),
Branches: strings.Split(Config.Branches, ","),
SlackURL: Config.Slack.URL,
}
// Build a MongoDB DialInfo object from the provided flags if a server
// address was specified.
if Config.MongoDB.Servers != "" {
config.MongoDBDialInfo = &mgo.DialInfo{
Addrs: strings.Split(Config.MongoDB.Servers, ","),
Timeout: time.Second * 10,
Database: Config.MongoDB.Database,
Username: Config.MongoDB.Username,
Password: Config.MongoDB.Password,
}
// Check to see if we need to use a TLS dialer.
if Config.MongoDB.SSL {
tlsConfig := &tls.Config{}
config.MongoDBDialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), tlsConfig)
}
}
}
// Create a builder from the configuration.
b, err := builder.NewBuilder(config)
if err != nil {
log.Fatal(err)
}
// Create an errors channel so we can panic on any listening error from
// either server.
errs := make(chan error)
// Run the API.
go func() {
r := mux.NewRouter()
// Build a sub-router for POST endpoints.
p := r.Methods("POST").Subrouter()
p.HandleFunc("/", b.ServeWebhook).Headers("X-GitHub-Event", "push")
// Build a sub-router for GET endpoints.
g := r.Methods("GET").Subrouter()
g.HandleFunc("/status", status)
g.HandleFunc("/api/builds/github.com/{user}/{repository}", b.ServeBuilds)
server := &http.Server{Addr: Config.WebhookAddress, Handler: r}
errs <- server.ListenAndServe()
}()
// Run the key generator.
go func() {
r := mux.NewRouter()
// Build a sub-router for GET endpoints.
g := r.Methods("GET").Subrouter()
g.HandleFunc("/status", status)
g.HandleFunc("/{user}/{repository}", b.ServeKey)
server := &http.Server{Addr: Config.KeyAddress, Handler: r}
errs <- server.ListenAndServe()
}()
log.Fatal(<-errs)
}