Skip to content

Commit e5a5259

Browse files
authored
Merge pull request #250 from carlosms/go-cli
Use go-cli, add version/serve subcommands
2 parents 0718ceb + 507edbf commit e5a5259

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+8682
-36
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ RUN apt-get update && \
99
ADD ./build/bin /bin
1010

1111
ENTRYPOINT ["/bin/gitbase-web"]
12+
CMD ["serve"]

Gopkg.lock

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ Download the binary from our [releases section](https://github.com/src-d/gitbase
6363
```bash
6464
$ export GITBASEPG_DB_CONNECTION="root@tcp(<gitbase-ip>:3306)/none"
6565
$ export GITBASEPG_BBLFSH_SERVER_URL="<bblfshd-ip>:9432"
66-
$ ./gitbase-web
66+
$ ./gitbase-web serve
6767
```
6868

6969
# Configuration
7070

71-
Any of the previous execution methods accept configuration through the following environment variables.
72-
73-
| Variable | Default value | Meaning |
74-
| -- | -- | -- |
75-
| `GITBASEPG_HOST` | `0.0.0.0` | IP address to bind the HTTP server |
76-
| `GITBASEPG_PORT` | `8080` | Port to bind the HTTP server |
77-
| `GITBASEPG_SERVER_URL` | | URL used to access the application in the form `HOSTNAME[:PORT]`. Leave it unset to allow connections from any proxy or public address |
78-
| `GITBASEPG_DB_CONNECTION` | `root@tcp(localhost:3306)/none?maxAllowedPacket=4194304` | gitbase connection string. Use the DSN (Data Source Name) format described in the [Go MySQL Driver docs](https://github.com/go-sql-driver/mysql#dsn-data-source-name). |
79-
| `GITBASEPG_BBLFSH_SERVER_URL` | `127.0.0.1:9432` | Address where bblfsh server is listening |
80-
| `GITBASEPG_ENV` | `production` | Sets the log level. Use `dev` to enable debug log messages |
81-
| `GITBASEPG_SELECT_LIMIT` | `100` | Default `LIMIT` forced on all the SQL queries done from the UI. Set it to 0 to remove any limit |
82-
| `GITBASEPG_FOOTER_HTML` | | Allows to add any custom html to the page footer. It must be a string encoded in base64. Use it, for example, to add your analytics tracking code snippet |
71+
Any of the previous execution methods accept configuration through the following environment variables or CLI arguments.
72+
73+
| Variable | Argument | Default value | Meaning |
74+
| -- | -- | -- | -- |
75+
| `GITBASEPG_HOST` | `--host` | `0.0.0.0` | IP address to bind the HTTP server |
76+
| `GITBASEPG_PORT` | `--port` | `8080` | Port to bind the HTTP server |
77+
| `GITBASEPG_SERVER_URL` | `--server` | | URL used to access the application in the form `HOSTNAME[:PORT]`. Leave it unset to allow connections from any proxy or public address |
78+
| `GITBASEPG_DB_CONNECTION` | `--db` | `root@tcp(localhost:3306)/none?maxAllowedPacket=4194304` | gitbase connection string. Use the DSN (Data Source Name) format described in the [Go MySQL Driver docs](https://github.com/go-sql-driver/mysql#dsn-data-source-name). |
79+
| `GITBASEPG_BBLFSH_SERVER_URL` | `--bblfsh` | `127.0.0.1:9432` | Address where bblfsh server is listening |
80+
| `GITBASEPG_ENV` | `--env` | `production` | Sets the log level. Use `dev` to enable debug log messages |
81+
| `GITBASEPG_SELECT_LIMIT` | `--select-limit` | `100` | Default `LIMIT` forced on all the SQL queries done from the UI. Set it to 0 to remove any limit |
82+
| `GITBASEPG_FOOTER_HTML` | `--footer` | | Allows to add any custom html to the page footer. It must be a string encoded in base64. Use it, for example, to add your analytics tracking code snippet |
8383

8484
# Contribute
8585

cmd/gitbase-web/main.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,60 @@ import (
1010
"github.com/src-d/gitbase-web/server/service"
1111

1212
_ "github.com/go-sql-driver/mysql"
13-
"github.com/kelseyhightower/envconfig"
13+
"gopkg.in/src-d/go-cli.v0"
1414
)
1515

1616
// version will be replaced automatically by the CI build.
1717
// See https://github.com/src-d/ci/blob/v1/Makefile.main#L56
18-
var version = "dev"
18+
var (
19+
name = "gitbase-web"
20+
version = "undefined"
21+
build = "undefined"
22+
)
23+
24+
var app = cli.New(name, version, build, "gitbase web client")
1925

2026
// Note: maxAllowedPacket must be explicitly set for go-sql-driver/mysql v1.3.
2127
// Otherwise gitbase will be asked for the max_allowed_packet column and the
2228
// query will fail.
2329
// The next release should make this parameter optional for us:
2430
// https://github.com/go-sql-driver/mysql/pull/680
25-
type appConfig struct {
26-
Env string `envconfig:"ENV" default:"production"`
27-
Host string `envconfig:"HOST" default:"0.0.0.0"`
28-
Port int `envconfig:"PORT" default:"8080"`
29-
ServerURL string `envconfig:"SERVER_URL"`
30-
DBConn string `envconfig:"DB_CONNECTION" default:"root@tcp(localhost:3306)/none?maxAllowedPacket=4194304"`
31-
SelectLimit int `envconfig:"SELECT_LIMIT" default:"100"`
32-
BblfshServerURL string `envconfig:"BBLFSH_SERVER_URL" default:"127.0.0.1:9432"`
33-
FooterHTML string `envconfig:"FOOTER_HTML"`
31+
type ServeCommand struct {
32+
cli.PlainCommand `name:"serve" short-description:"serve the app" long-description:"starts serving the application"`
33+
Env string `long:"env" env:"GITBASEPG_ENV" default:"production" description:"Sets the log level. Use 'dev' to enable debug log messages"`
34+
Host string `long:"host" env:"GITBASEPG_HOST" default:"0.0.0.0" description:"IP address to bind the HTTP server"`
35+
Port int `long:"port" env:"GITBASEPG_PORT" default:"8080" description:"Port to bind the HTTP server"`
36+
ServerURL string `long:"server" env:"GITBASEPG_SERVER_URL" description:"URL used to access the application in the form 'HOSTNAME[:PORT]'. Leave it unset to allow connections from any proxy or public address"`
37+
DBConn string `long:"db" env:"GITBASEPG_DB_CONNECTION" default:"root@tcp(localhost:3306)/none?maxAllowedPacket=4194304" description:"gitbase connection string. Use the DSN (Data Source Name) format described in the Go MySQL Driver docs: https://github.com/go-sql-driver/mysql#dsn-data-source-name"`
38+
SelectLimit int `long:"select-limit" env:"GITBASEPG_SELECT_LIMIT" default:"100" description:"Default 'LIMIT' forced on all the SQL queries done from the UI. Set it to 0 to remove any limit"`
39+
BblfshServerURL string `long:"bblfsh" env:"GITBASEPG_BBLFSH_SERVER_URL" default:"127.0.0.1:9432" description:"Address where bblfsh server is listening"`
40+
FooterHTML string `long:"footer" env:"GITBASEPG_FOOTER_HTML" description:"Allows to add any custom html to the page footer. It must be a string encoded in base64. Use it, for example, to add your analytics tracking code snippet"`
3441
}
3542

36-
func main() {
37-
// main configuration
38-
var conf appConfig
39-
envconfig.MustProcess("GITBASEPG", &conf)
40-
43+
func (c *ServeCommand) Execute(args []string) error {
4144
// logger
42-
logger := service.NewLogger(conf.Env)
45+
logger := service.NewLogger(c.Env)
4346

4447
// database
45-
db, err := sql.Open("mysql", conf.DBConn)
48+
db, err := sql.Open("mysql", c.DBConn)
4649
if err != nil {
4750
logger.Fatalf("error opening the database: %s", err)
4851
}
4952
defer db.Close()
5053

51-
static := handler.NewStatic("build/public", conf.ServerURL, conf.SelectLimit, conf.FooterHTML)
54+
static := handler.NewStatic("build/public", c.ServerURL, c.SelectLimit, c.FooterHTML)
5255

5356
// start the router
54-
router := server.Router(logger, static, version, db, conf.BblfshServerURL)
55-
logger.Infof("listening on %s:%d", conf.Host, conf.Port)
56-
err = http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, conf.Port), router)
57+
router := server.Router(logger, static, version, db, c.BblfshServerURL)
58+
logger.Infof("listening on %s:%d", c.Host, c.Port)
59+
err = http.ListenAndServe(fmt.Sprintf("%s:%d", c.Host, c.Port), router)
5760
logger.Fatal(err)
61+
62+
return nil
63+
}
64+
65+
func main() {
66+
app.AddCommand(&ServeCommand{})
67+
68+
app.RunMain()
5869
}

docs/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Instead of rebuilding the frontend and restarting the backend every time you do
9090
In one terminal run the Go backend:
9191

9292
```bash
93-
$ GITBASEPG_ENV=dev go run cmd/gitbase-web/main.go
93+
$ GITBASEPG_ENV=dev go run cmd/gitbase-web/main.go serve
9494
```
9595

9696
In another terminal, run the frontend:

vendor/github.com/jessevdk/go-flags/LICENSE

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/jessevdk/go-flags/arg.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/jessevdk/go-flags/closest.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)