dont gitignore main pkg...

This commit is contained in:
Danny 2018-05-14 14:11:52 +02:00
parent 68ab44c57d
commit 89e321a383
7 changed files with 165 additions and 3 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
node_modules
.env
fathom
coverage.out
build
fathom.db
fathom
!cmd/fathom

View File

@ -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).

View File

@ -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 <email> <password>`
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).

48
cmd/fathom/config.go Normal file
View File

@ -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)
}

54
cmd/fathom/fathom.go Normal file
View File

@ -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: "<email> <password>",
Action: register,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

30
cmd/fathom/register.go Normal file
View File

@ -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
}

30
cmd/fathom/server.go Normal file
View File

@ -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
}