2016-11-21 12:24:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-12-11 13:50:01 +00:00
|
|
|
"github.com/joho/godotenv"
|
2016-12-25 16:27:39 +00:00
|
|
|
"github.com/robfig/cron"
|
2018-04-24 08:28:23 +00:00
|
|
|
"github.com/usefathom/fathom/pkg/commands"
|
|
|
|
"github.com/usefathom/fathom/pkg/count"
|
|
|
|
"github.com/usefathom/fathom/pkg/datastore"
|
2017-01-24 19:28:22 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-04-24 08:28:23 +00:00
|
|
|
app = kingpin.New("fathom", "Simple website analytics.")
|
2017-01-24 19:28:22 +00:00
|
|
|
register = app.Command("register", "Register a new user.")
|
|
|
|
registerEmail = register.Arg("email", "Email for user.").Required().String()
|
|
|
|
registerPassword = register.Arg("password", "Password for user.").Required().String()
|
|
|
|
server = app.Command("server", "Start webserver.").Default()
|
|
|
|
serverPort = server.Flag("port", "Port to listen on.").Default("8080").Int()
|
2018-04-25 08:15:24 +00:00
|
|
|
serverWebRoot = server.Flag("webroot", "Root directory of static assets").Default("./").String()
|
2017-01-24 19:28:22 +00:00
|
|
|
archive = app.Command("archive", "Process unarchived data.")
|
|
|
|
seed = app.Command("seed", "Seed the database.")
|
|
|
|
seedN = seed.Flag("n", "Number of records to seed.").Int()
|
2016-11-21 12:24:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-01-24 19:28:22 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
// load .env file
|
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
2017-01-09 18:52:41 +00:00
|
|
|
log.Println("No .env file found")
|
2016-12-11 13:50:01 +00:00
|
|
|
}
|
2016-11-22 21:33:50 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
// setup database connection
|
2017-01-25 12:46:43 +00:00
|
|
|
db := datastore.Init()
|
|
|
|
defer db.Close()
|
2016-11-22 19:57:16 +00:00
|
|
|
|
2016-12-25 16:27:39 +00:00
|
|
|
// setup cron to run count.Archive every hour
|
|
|
|
c := cron.New()
|
|
|
|
c.AddFunc("@hourly", count.Archive)
|
|
|
|
c.Start()
|
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
// parse & run cli commands
|
2017-01-24 19:28:22 +00:00
|
|
|
app.Version("1.0")
|
|
|
|
app.UsageTemplate(kingpin.CompactUsageTemplate)
|
|
|
|
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
|
|
|
|
case "register":
|
|
|
|
commands.Register(*registerEmail, *registerPassword)
|
|
|
|
|
|
|
|
case "server":
|
2018-04-25 08:15:24 +00:00
|
|
|
commands.Server(*serverPort, *serverWebRoot)
|
2017-01-24 19:28:22 +00:00
|
|
|
|
|
|
|
case "archive":
|
|
|
|
commands.Archive()
|
|
|
|
|
|
|
|
case "seed":
|
|
|
|
commands.Seed(*seedN)
|
|
|
|
}
|
|
|
|
|
2016-11-21 12:24:50 +00:00
|
|
|
}
|