Add proper command line flags and version support

This commit is contained in:
2016-11-16 23:17:36 +00:00
parent b372ad688a
commit d5debfc6ca
3 changed files with 65 additions and 9 deletions

1
VERSION Normal file
View File

@@ -0,0 +1 @@
0.0.1

49
main.go
View File

@@ -1,25 +1,37 @@
package main
import (
"fmt"
"log"
"os"
"github.com/jimeh/ozu.io/shortener"
"github.com/jimeh/ozu.io/storage/goleveldbstore"
"github.com/jimeh/ozu.io/web"
"gopkg.in/alecthomas/kingpin.v2"
)
func getPort() string {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// Version gets populated with version at build-time.
var Version string
var defaultPort = "8080"
return port
var (
port = kingpin.Flag("port", "Port to listen to.").Short('p').
Default(defaultPort).String()
bind = kingpin.Flag("bind", "Bind address.").Short('b').
Default("0.0.0.0").String()
dir = kingpin.Flag("dir", "Directory to store database file.").
Short('d').Default("ozuio_database").String()
version = kingpin.Flag("version", "Print version info.").
Short('v').Bool()
)
func printVersion() {
fmt.Println("ozuio " + Version)
}
func main() {
store, err := goleveldbstore.New("ozuio_database")
func startServer() {
store, err := goleveldbstore.New(*dir)
if err != nil {
log.Fatal(err)
}
@@ -28,5 +40,24 @@ func main() {
s := shortener.New(store)
server := web.NewServer(s)
log.Fatal(server.ListenAndServe(":" + getPort()))
if *port == defaultPort {
envPort := os.Getenv("PORT")
if envPort != "" {
*port = envPort
}
}
address := *bind + ":" + *port
fmt.Println("Listening on " + address)
log.Fatal(server.ListenAndServe(address))
}
func main() {
kingpin.Parse()
if *version {
printVersion()
} else {
startServer()
}
}

24
vendor/vendor.json vendored
View File

@@ -2,6 +2,24 @@
"comment": "",
"ignore": "test",
"package": [
{
"checksumSHA1": "KmjnydoAbofMieIWm+it5OWERaM=",
"path": "github.com/alecthomas/template",
"revision": "a0175ee3bccc567396460bf5acd36800cb10c49c",
"revisionTime": "2016-04-05T07:15:01Z"
},
{
"checksumSHA1": "3wt0pTXXeS+S93unwhGoLIyGX/Q=",
"path": "github.com/alecthomas/template/parse",
"revision": "a0175ee3bccc567396460bf5acd36800cb10c49c",
"revisionTime": "2016-04-05T07:15:01Z"
},
{
"checksumSHA1": "fCc3grA7vIxfBru7R3SqjcW+oLI=",
"path": "github.com/alecthomas/units",
"revision": "2efee857e7cfd4f3d0138cc3cbb1b4966962b93a",
"revisionTime": "2015-10-22T06:55:26Z"
},
{
"checksumSHA1": "5rPfda8jFccr3A6heL+JAmi9K9g=",
"path": "github.com/davecgh/go-spew/spew",
@@ -199,6 +217,12 @@
"path": "github.com/valyala/fasthttp/fasthttputil",
"revision": "881ac52b00ef21896de49a8aa185467c8a9ae49c",
"revisionTime": "2016-07-16T14:36:32Z"
},
{
"checksumSHA1": "SeYI7DRWrd0Ku+CLavuwIz3EEmQ=",
"path": "gopkg.in/alecthomas/kingpin.v2",
"revision": "e9044be3ab2a8e11d4e1f418d12f0790d57e8d70",
"revisionTime": "2016-08-29T10:30:05Z"
}
],
"rootPath": "github.com/jimeh/ozu.io"