mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
add CLI functionality to main package, with create_user command
This commit is contained in:
parent
265e7a2973
commit
679dc190ae
35
ana.go
35
ana.go
@ -1,13 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/dannyvankooten/ana/commands"
|
||||||
"github.com/dannyvankooten/ana/db"
|
"github.com/dannyvankooten/ana/db"
|
||||||
"github.com/dannyvankooten/ana/api"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/gorilla/handlers"
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,31 +15,11 @@ func main() {
|
|||||||
log.Fatal("Error loading .env file")
|
log.Fatal("Error loading .env file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setup database connection
|
||||||
conn := db.SetupDatabaseConnection()
|
conn := db.SetupDatabaseConnection()
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
db.Seed(1000)
|
// parse & run cli commands
|
||||||
|
commands.Parse()
|
||||||
// register routes
|
commands.Run()
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
29
commands/commands.go
Normal file
29
commands/commands.go
Normal 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
17
commands/create-user.go
Normal 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
36
commands/start-server.go
Normal 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))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user