|
| 1 | +//go:generate go-bindata -prefix ../../migrations/ -pkg migrations -o ../../migrations/migrations.go ../../migrations/ |
| 2 | +//go:generate go-bindata -prefix ../../static/ -pkg static -o ../../static/static.go ../../static/... |
| 3 | + |
1 | 4 | package main
|
2 | 5 |
|
3 | 6 | import (
|
4 |
| - "io/ioutil" |
5 |
| - golog "log" |
6 | 7 | "net/http"
|
7 | 8 | "os"
|
8 | 9 | "os/signal"
|
9 | 10 | "syscall"
|
10 | 11 |
|
11 |
| - "github.com/DavidHuie/gomigrate" |
12 | 12 | log "github.com/Sirupsen/logrus"
|
13 | 13 | "github.com/brocaar/loraserver"
|
14 | 14 | application "github.com/brocaar/loraserver/application/mqttpubsub"
|
15 | 15 | gateway "github.com/brocaar/loraserver/gateway/mqttpubsub"
|
| 16 | + "github.com/brocaar/loraserver/migrations" |
| 17 | + "github.com/brocaar/loraserver/static" |
16 | 18 | "github.com/brocaar/lorawan"
|
17 | 19 | "github.com/codegangsta/cli"
|
| 20 | + "github.com/elazarl/go-bindata-assetfs" |
18 | 21 | _ "github.com/lib/pq"
|
| 22 | + "github.com/rubenv/sql-migrate" |
19 | 23 | )
|
20 | 24 |
|
21 | 25 | var version string // set by the compiler
|
22 | 26 |
|
23 |
| -func init() { |
24 |
| - golog.SetOutput(ioutil.Discard) |
25 |
| -} |
26 |
| - |
27 | 27 | func run(c *cli.Context) {
|
28 | 28 | // parse the NetID
|
29 | 29 | var netID lorawan.NetID
|
@@ -57,13 +57,16 @@ func run(c *cli.Context) {
|
57 | 57 | // auto-migrate the database
|
58 | 58 | if c.Bool("db-automigrate") {
|
59 | 59 | log.Info("applying database migrations")
|
60 |
| - migrator, err := gomigrate.NewMigrator(db.DB, gomigrate.Postgres{}, c.String("db-migrations-path")) |
61 |
| - if err != nil { |
62 |
| - log.Fatalf("could not create the migrator: %s", err) |
| 60 | + m := &migrate.AssetMigrationSource{ |
| 61 | + Asset: migrations.Asset, |
| 62 | + AssetDir: migrations.AssetDir, |
| 63 | + Dir: "", |
63 | 64 | }
|
64 |
| - if err = migrator.Migrate(); err != nil { |
65 |
| - log.Fatalf("could run the migrations: %s", err) |
| 65 | + n, err := migrate.Exec(db.DB, "postgres", m, migrate.Up) |
| 66 | + if err != nil { |
| 67 | + log.Fatalf("migrations failed: %s", err) |
66 | 68 | }
|
| 69 | + log.WithField("count", n).Info("migrations applied") |
67 | 70 | }
|
68 | 71 |
|
69 | 72 | ctx := loraserver.Context{
|
@@ -91,7 +94,12 @@ func run(c *cli.Context) {
|
91 | 94 |
|
92 | 95 | // setup static file server (for the gui)
|
93 | 96 | log.WithField("path", "/").Info("registering gui handler")
|
94 |
| - http.Handle("/", http.FileServer(http.Dir("static"))) |
| 97 | + http.Handle("/", http.FileServer(&assetfs.AssetFS{ |
| 98 | + Asset: static.Asset, |
| 99 | + AssetDir: static.AssetDir, |
| 100 | + AssetInfo: static.AssetInfo, |
| 101 | + Prefix: "", |
| 102 | + })) |
95 | 103 |
|
96 | 104 | // start the http server
|
97 | 105 | go func() {
|
@@ -119,11 +127,6 @@ func main() {
|
119 | 127 | EnvVar: "DB_AUTOMIGRATE",
|
120 | 128 | },
|
121 | 129 | cli.StringFlag{
|
122 |
| - Name: "db-migrations-path", |
123 |
| - Value: "./migrations", |
124 |
| - Usage: "path to the directory containing the database migrations", |
125 |
| - EnvVar: "DB_MIGRATIONS_PATH", |
126 |
| - }, cli.StringFlag{ |
127 | 130 | Name: "net-id",
|
128 | 131 | Usage: "network identifier (NetID, 3 bytes) encoded as HEX (e.g. 010203)",
|
129 | 132 | EnvVar: "NET_ID",
|
|
0 commit comments