2018-05-14 12:11:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
2018-05-15 11:55:10 +00:00
|
|
|
"github.com/usefathom/fathom/pkg/config"
|
2018-05-14 12:11:52 +00:00
|
|
|
"github.com/usefathom/fathom/pkg/datastore"
|
|
|
|
)
|
|
|
|
|
2018-05-15 11:55:10 +00:00
|
|
|
type App struct {
|
|
|
|
*cli.App
|
|
|
|
database datastore.Datastore
|
|
|
|
config *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
var app *App
|
2018-05-14 12:11:52 +00:00
|
|
|
|
2018-05-15 09:07:27 +00:00
|
|
|
func main() {
|
2018-05-15 11:55:10 +00:00
|
|
|
app = &App{cli.NewApp(), nil, nil}
|
2018-05-14 12:11:52 +00:00
|
|
|
app.Name = "Fathom"
|
|
|
|
app.Usage = "simple & transparent website analytics"
|
|
|
|
app.Version = "1.0.0"
|
|
|
|
app.HelpName = "fathom"
|
2018-05-15 09:07:27 +00:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: ".env",
|
|
|
|
Usage: "Load configuration from `FILE`",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Before = before
|
|
|
|
app.After = after
|
2018-05-14 12:11:52 +00:00
|
|
|
app.Commands = []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "server",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "start the fathom web server",
|
|
|
|
Action: server,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
EnvVar: "FATHOM_SERVER_ADDR",
|
2018-05-14 12:54:16 +00:00
|
|
|
Name: "addr,port",
|
2018-05-14 12:11:52 +00:00
|
|
|
Usage: "server address",
|
|
|
|
Value: ":8080",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
EnvVar: "FATHOM_DEBUG",
|
2018-05-15 09:07:27 +00:00
|
|
|
Name: "debug, d",
|
2018-05-14 12:11:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "register",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "register a new admin user",
|
|
|
|
ArgsUsage: "<email> <password>",
|
|
|
|
Action: register,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-05-15 09:07:27 +00:00
|
|
|
os.Exit(1)
|
2018-05-14 12:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-15 09:07:27 +00:00
|
|
|
|
|
|
|
func before(c *cli.Context) error {
|
2018-05-15 11:55:10 +00:00
|
|
|
app.config = config.Parse(c.String("config"))
|
|
|
|
app.database = datastore.New(app.config.Database)
|
2018-05-15 11:30:37 +00:00
|
|
|
|
2018-05-15 09:07:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func after(c *cli.Context) error {
|
2018-05-15 11:55:10 +00:00
|
|
|
app.database.Close()
|
2018-05-15 09:07:27 +00:00
|
|
|
return nil
|
|
|
|
}
|