-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacedex.go
90 lines (77 loc) · 2.45 KB
/
spacedex.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
/*
Spacedex, the new and improved Docker index.
*/
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/ricallinson/forgery"
"github.com/ricallinson/stackr"
"github.com/Southern/logger"
"github.com/Southern/middleware"
"github.com/spacedock-io/index/config"
"github.com/spacedock-io/registry/db"
"github.com/spacedock-io/index/models"
_ "github.com/lib/pq"
"os"
)
func main() {
// Initialize new CLI app
app := cli.NewApp()
app.Name = "spacedex"
app.Author = "Spacedock"
app.Email = "hello@spacedock.io"
app.Usage = "Run a standalone Docker index"
app.Version = "0.0.1"
app.Flags = []cli.Flag {
// No default value here, so that our <env>.config.json file will override
// it.
cli.StringFlag{"port, p", "", "Port to listen on"},
cli.StringFlag{"env, e", "dev", "Default environment"},
cli.StringFlag{"config, c", "", "Configuration directory"},
}
app.Action = func (c *cli.Context) {
env := c.String("env")
dir := c.String("config")
// Default to dev if someone enters a blank string
if len(env) == 0 {
env = "dev"
}
if len(dir) > 0 {
config.Dir = dir
}
config.Global = config.Load(env)
config.Logger = logger.New()
// Initialize new Forgery server
server := f.CreateServer()
server.Use(func(req *stackr.Request, res *stackr.Response, next func()) {
config.Logger.Log(fmt.Sprintf("%s %s", req.Method, req.Url))
next()
})
server.Use(f.ErrorHandler())
server.Use(middleware.BodyParser)
server.Use(func(req *stackr.Request, res *stackr.Response, next func()) {
res.SetHeader("X-Docker-Registry-Version", "0.7.4")
next()
})
port := c.Int("port")
if port == 0 {
// Bug(Colton): Not quite sure why port is being picked up as Float64 at
// the moment. Still looking into this. It may be intended functionality.
port = int(config.Global.Get("port").Float64())
}
config.Logger.Debug = config.Global.Get("logger.debug").Bool(false)
config.Logger.Exit = config.Global.Get("logger.exit").Bool(false)
db := db.New(config.Global)
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Access{})
db.AutoMigrate(&models.Email{})
db.AutoMigrate(&models.Repo{})
db.AutoMigrate(&models.Token{})
db.AutoMigrate(&models.Image{})
Routes(server)
config.Logger.Log("Index listening on port " + fmt.Sprint(port))
server.Listen(port)
}
app.Run(os.Args)
}