From 89e321a383af0753bd5d2a5754538e6240da4096 Mon Sep 17 00:00:00 2001 From: Danny Date: Mon, 14 May 2018 14:11:52 +0200 Subject: [PATCH] dont gitignore main pkg... --- .gitignore | 3 ++- NOTES.md | 1 - README.md | 2 +- cmd/fathom/config.go | 48 +++++++++++++++++++++++++++++++++++++ cmd/fathom/fathom.go | 54 ++++++++++++++++++++++++++++++++++++++++++ cmd/fathom/register.go | 30 +++++++++++++++++++++++ cmd/fathom/server.go | 30 +++++++++++++++++++++++ 7 files changed, 165 insertions(+), 3 deletions(-) delete mode 100644 NOTES.md create mode 100644 cmd/fathom/config.go create mode 100644 cmd/fathom/fathom.go create mode 100644 cmd/fathom/register.go create mode 100644 cmd/fathom/server.go diff --git a/.gitignore b/.gitignore index f110302..eaf1229 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules .env -fathom coverage.out build fathom.db +fathom +!cmd/fathom diff --git a/NOTES.md b/NOTES.md deleted file mode 100644 index 2587459..0000000 --- a/NOTES.md +++ /dev/null @@ -1 +0,0 @@ -- Client-side data needs to keep track of new visitor (first pageview that day) vs. new session (new visitor or last pageview > 30 mins ago). diff --git a/README.md b/README.md index 6aab7f1..d6ecccc 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ For getting a development version of Fathom up & running, please go through the 1. Get code: `git clone git@github.com:usefathom/fathom.git $GOPATH/src/github.com/usefathom/fathom` 1. Compile into binary & prepare assets: `make all` 1. Register your user account: `fathom register ` -1. Start the webserver: `fathom server --port=8080` and then visit **http://localhost:8080** to access your analytics dashboard. +1. Start the webserver: `fathom server` and then visit **http://localhost:8080** to access your analytics dashboard. To start tracking, include the following JavaScript on your site and replace `yourfathom.com` with the URL to your Fathom instance (twice). diff --git a/cmd/fathom/config.go b/cmd/fathom/config.go new file mode 100644 index 0000000..e6ad491 --- /dev/null +++ b/cmd/fathom/config.go @@ -0,0 +1,48 @@ +package main + +import ( + log "github.com/sirupsen/logrus" + "math/rand" + "os" + + "github.com/joho/godotenv" + "github.com/kelseyhightower/envconfig" + "github.com/usefathom/fathom/pkg/datastore" +) + +type Config struct { + Database *datastore.Config + + Secret string +} + +func parseConfig() *Config { + var cfg Config + godotenv.Load() + err := envconfig.Process("Fathom", &cfg) + if err != nil { + log.Fatalf("Error parsing Fathom config from environment: %s", err) + } + + // alias sqlite to sqlite3 + if cfg.Database.Driver == "sqlite" { + cfg.Database.Driver = "sqlite3" + } + + // if secret key is empty, use a randomly generated one to ease first-time installation + if cfg.Secret == "" { + cfg.Secret = randomString(40) + os.Setenv("FATHOM_SECRET", cfg.Secret) + } + + return &cfg +} + +func randomString(len int) string { + bytes := make([]byte, len) + for i := 0; i < len; i++ { + bytes[i] = byte(65 + rand.Intn(25)) //A=65 and Z = 65+25 + } + + return string(bytes) +} diff --git a/cmd/fathom/fathom.go b/cmd/fathom/fathom.go new file mode 100644 index 0000000..186e88f --- /dev/null +++ b/cmd/fathom/fathom.go @@ -0,0 +1,54 @@ +package main + +import ( + "log" + "os" + + "github.com/urfave/cli" + "github.com/usefathom/fathom/pkg/datastore" +) + +func main() { + cfg := parseConfig() + db := datastore.Init(cfg.Database) + defer db.Close() + + app := cli.NewApp() + app.Name = "Fathom" + app.Usage = "simple & transparent website analytics" + app.Version = "1.0.0" + app.HelpName = "fathom" + app.Flags = []cli.Flag{} + 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", + Name: "addr", + Usage: "server address", + Value: ":8080", + }, + cli.BoolFlag{ + EnvVar: "FATHOM_DEBUG", + Name: "debug", + }, + }, + }, + { + Name: "register", + Aliases: []string{"r"}, + Usage: "register a new admin user", + ArgsUsage: " ", + Action: register, + }, + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} diff --git a/cmd/fathom/register.go b/cmd/fathom/register.go new file mode 100644 index 0000000..896372b --- /dev/null +++ b/cmd/fathom/register.go @@ -0,0 +1,30 @@ +package main + +import ( + "github.com/urfave/cli" + "github.com/usefathom/fathom/pkg/datastore" + "github.com/usefathom/fathom/pkg/models" + "golang.org/x/crypto/bcrypt" + "log" +) + +func register(c *cli.Context) error { + if c.NArg() < 2 { + cli.ShowSubcommandHelp(c) + return nil + } + + hash, _ := bcrypt.GenerateFromPassword([]byte(c.String("password")), 10) + user := &models.User{ + Email: c.String("email"), + Password: string(hash), + } + err := datastore.SaveUser(user) + + if err != nil { + log.Printf("Error creating user: %s", err) + } else { + log.Printf("User %s #%d created.\n", user.Email, user.ID) + } + return nil +} diff --git a/cmd/fathom/server.go b/cmd/fathom/server.go new file mode 100644 index 0000000..c25779c --- /dev/null +++ b/cmd/fathom/server.go @@ -0,0 +1,30 @@ +package main + +import ( + log "github.com/sirupsen/logrus" + "github.com/urfave/cli" + "net/http" + + "github.com/gorilla/handlers" + "github.com/usefathom/fathom/pkg/api" +) + +func server(c *cli.Context) error { + var h http.Handler + h = api.Routes() + + if c.Bool("debug") { + log.SetLevel(log.DebugLevel) + h = handlers.LoggingHandler(log.StandardLogger().Writer(), h) + } else { + log.SetLevel(log.WarnLevel) + } + + addr := c.String("addr") + log.Infof("Server listening on %s", addr) + err := http.ListenAndServe(addr, h) + if err != nil { + log.Errorln(err) + } + return nil +}