add CLI functionality to main package, with create_user command

This commit is contained in:
Danny van Kooten 2016-12-08 20:45:26 +01:00
parent 265e7a2973
commit 679dc190ae
4 changed files with 88 additions and 29 deletions

35
ana.go
View File

@ -1,13 +1,10 @@
package main
import (
"net/http"
"os"
"log"
"github.com/dannyvankooten/ana/commands"
"github.com/dannyvankooten/ana/db"
"github.com/dannyvankooten/ana/api"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"github.com/joho/godotenv"
)
@ -18,31 +15,11 @@ func main() {
log.Fatal("Error loading .env file")
}
// setup database connection
conn := db.SetupDatabaseConnection()
defer conn.Close()
db.Seed(1000)
// register routes
r := mux.NewRouter()
r.HandleFunc("/collect", api.CollectHandler).Methods("GET")
r.Handle("/api/session", api.LoginHandler).Methods("POST")
r.Handle("/api/session", api.LogoutHandler).Methods("DELETE")
r.Handle("/api/visits/count", api.Authorize(api.GetVisitsCountHandler)).Methods("GET")
r.Handle("/api/visits/count/group/{period}", api.Authorize(api.GetVisitsPeriodCountHandler)).Methods("GET")
r.Handle("/api/visits/count/realtime", api.Authorize(api.GetVisitsRealtimeCountHandler)).Methods("GET")
r.Handle("/api/visits", api.Authorize(api.GetVisitsHandler)).Methods("GET")
r.Handle("/api/pageviews/count", api.Authorize(api.GetPageviewsCountHandler)).Methods("GET")
r.Handle("/api/pageviews/count/group/{period}", api.Authorize(api.GetPageviewsPeriodCountHandler)).Methods("GET")
r.Handle("/api/pageviews", api.Authorize(api.GetPageviewsHandler)).Methods("GET")
r.Handle("/api/languages", api.Authorize(api.GetLanguagesHandler)).Methods("GET")
r.Handle("/api/screen-resolutions", api.Authorize(api.GetScreenResolutionsHandler)).Methods("GET")
r.Handle("/api/countries", api.Authorize(api.GetCountriesHandler)).Methods("GET")
r.Handle("/api/browsers", api.Authorize(api.GetBrowsersHandler)).Methods("GET")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
r.Path("/tracker.js").Handler(http.FileServer(http.Dir("./static/js/")))
r.Handle("/", http.FileServer(http.Dir("./views/")))
http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, r))
// parse & run cli commands
commands.Parse()
commands.Run()
}

29
commands/commands.go Normal file
View File

@ -0,0 +1,29 @@
package commands
import(
"flag"
)
var runCreateUserCommand bool
var runStartServerCommand bool
var emailArg string
var passwordArg string
func Parse() {
// parse commands
flag.BoolVar(&runCreateUserCommand, "create_user", false, "Create a new user")
flag.BoolVar(&runStartServerCommand, "start_server", true, "Start the API web server")
flag.StringVar(&emailArg, "email", "", "Email address")
flag.StringVar(&passwordArg, "password", "", "Password")
flag.Parse()
}
func Run() {
if runCreateUserCommand {
CreateUser()
}
if runStartServerCommand {
StartServer()
}
}

17
commands/create-user.go Normal file
View File

@ -0,0 +1,17 @@
package commands
import(
"github.com/dannyvankooten/ana/db"
"golang.org/x/crypto/bcrypt"
"log"
)
func CreateUser() {
if emailArg == "" || passwordArg == "" {
log.Fatal("Please supply -email and -password values")
}
stmt2, _ := db.Conn.Prepare("INSERT INTO users(email, password) VALUES(?, ?)")
hash, _ := bcrypt.GenerateFromPassword([]byte(passwordArg), 10)
stmt2.Exec(emailArg, hash)
}

36
commands/start-server.go Normal file
View File

@ -0,0 +1,36 @@
package commands
import(
"net/http"
"os"
"github.com/dannyvankooten/ana/api"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"log"
)
func StartServer() {
// register routes
r := mux.NewRouter()
r.HandleFunc("/collect", api.CollectHandler).Methods("GET")
r.Handle("/api/session", api.LoginHandler).Methods("POST")
r.Handle("/api/session", api.LogoutHandler).Methods("DELETE")
r.Handle("/api/visits/count", api.Authorize(api.GetVisitsCountHandler)).Methods("GET")
r.Handle("/api/visits/count/group/{period}", api.Authorize(api.GetVisitsPeriodCountHandler)).Methods("GET")
r.Handle("/api/visits/count/realtime", api.Authorize(api.GetVisitsRealtimeCountHandler)).Methods("GET")
r.Handle("/api/visits", api.Authorize(api.GetVisitsHandler)).Methods("GET")
r.Handle("/api/pageviews/count", api.Authorize(api.GetPageviewsCountHandler)).Methods("GET")
r.Handle("/api/pageviews/count/group/{period}", api.Authorize(api.GetPageviewsPeriodCountHandler)).Methods("GET")
r.Handle("/api/pageviews", api.Authorize(api.GetPageviewsHandler)).Methods("GET")
r.Handle("/api/languages", api.Authorize(api.GetLanguagesHandler)).Methods("GET")
r.Handle("/api/screen-resolutions", api.Authorize(api.GetScreenResolutionsHandler)).Methods("GET")
r.Handle("/api/countries", api.Authorize(api.GetCountriesHandler)).Methods("GET")
r.Handle("/api/browsers", api.Authorize(api.GetBrowsersHandler)).Methods("GET")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
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))
}