Allow for `-port` CLI argument.

This commit is contained in:
Danny van Kooten 2016-12-24 11:00:29 +02:00
parent d8e074191f
commit c07b220c2d
3 changed files with 15 additions and 9 deletions

View File

@ -19,7 +19,7 @@ For getting a development version of Ana up & running, please go through the fol
3. Compile into binary: `./do bin`
4. Create your user account: `./ana -create_user -email="johndoe@email.com" -password="...."`
5. Run default Gulp task to build static assets: `gulp`
6. Start the webserver: `./ana` & visit **localhost:8080** to access your analytics dashboard.
6. Start the webserver: `./ana -start_server -port=8080` & visit **localhost:8080** to access your analytics dashboard.
To start tracking, include the following JavaScript on your site and replace `ana.dev` with the URL to your Ana instance. You should also set-up a cronjob to run the `./ana -archive_data` command periodically (eg every day).

View File

@ -13,21 +13,25 @@ var idArg int
var emailArg string
var passwordArg string
var nArg int
var portArg int
// Parse CLI arguments
func Parse() {
// parse commands
flag.BoolVar(&runCreateUserCommand, "create_user", false, "Create a new user")
flag.BoolVar(&runDeleteUserCommand, "delete_user", false, "Deletes a user")
flag.BoolVar(&runStartServerCommand, "start_server", true, "Start the API web server")
flag.BoolVar(&runStartServerCommand, "start_server", true, "Start the API web server, listen on -port")
flag.BoolVar(&runSeedDataCommand, "seed_data", false, "Seed the database -n times")
flag.BoolVar(&runArchiveDataCommand, "archive_data", false, "Archives data into daily aggregated totals")
flag.StringVar(&emailArg, "email", "", "Email address")
flag.StringVar(&passwordArg, "password", "", "Password")
flag.IntVar(&idArg, "id", 0, "Object ID")
flag.IntVar(&nArg, "n", 0, "Number")
flag.IntVar(&portArg, "port", 8080, "Port")
flag.Parse()
}
// Run parsed CLI command. Defaults to starting the HTTP server.
func Run() {
if runCreateUserCommand {
createUser()
@ -38,6 +42,6 @@ func Run() {
} else if runArchiveDataCommand {
archiveData()
} else if runStartServerCommand {
startServer()
startServer(portArg)
}
}

View File

@ -1,15 +1,17 @@
package commands
import (
"github.com/dannyvankooten/ana/api"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"fmt"
"log"
"net/http"
"os"
"github.com/dannyvankooten/ana/api"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func startServer() {
func startServer(port int) {
// register routes
r := mux.NewRouter()
r.HandleFunc("/collect", api.CollectHandler).Methods("GET")
@ -31,6 +33,6 @@ func startServer() {
r.Path("/tracker.js").Handler(http.FileServer(http.Dir("./static/js/")))
r.Handle("/", http.FileServer(http.Dir("./views/")))
log.Print("API server is now listening on :8080")
http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, r))
log.Printf("API server is now listening on :%d", port)
http.ListenAndServe(fmt.Sprintf(":%d", port), handlers.LoggingHandler(os.Stdout, r))
}