Allow overriding of database path

This commit is contained in:
Herman Schaaf
2021-11-12 14:03:11 +00:00
parent 60688a70a6
commit 24f5b40272
2 changed files with 14 additions and 4 deletions

View File

@@ -28,16 +28,16 @@ make install
Now run:
```
make start
GRC_DATABASE_PATH=./db make start
```
and you should see
```
Running on 127.0.0.1:8000...
Running on :8000...
```
Navigate to that URL and you should see the Go Report Card front page.
Navigate to `localhost:8000` and you should see the Go Report Card front page.
### Command Line Interface

12
main.go
View File

@@ -21,6 +21,8 @@ import (
var (
addr = flag.String("http", ":8000", "HTTP listen address")
databasePath = flag.String("db", getEnv("GRC_DATABASE_PATH", "/usr/local/badger"), "path to local badger database")
//go:embed assets/*
embedFS embed.FS
)
@@ -103,13 +105,21 @@ func (m metrics) instrument(path string, h http.HandlerFunc) (string, http.Handl
}
}
func getEnv(name, def string) string {
got := os.Getenv(name)
if got == "" {
return def
}
return got
}
func main() {
flag.Parse()
if err := os.MkdirAll("_repos/src/github.com", 0755); err != nil && !os.IsExist(err) {
log.Fatal("ERROR: could not create repos dir: ", err)
}
db, err := badger.Open(badger.DefaultOptions("./badger").WithTruncate(true))
db, err := badger.Open(badger.DefaultOptions(*databasePath).WithTruncate(true))
if err != nil {
log.Fatal("ERROR: could not open badger db: ", err)
}