diff --git a/.gitignore b/.gitignore index 0f6ad8b..14ad0fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ launch.json .vscode -.env \ No newline at end of file +.env +wakapi \ No newline at end of file diff --git a/README.md b/README.md index dca3954..e21ac4f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# wakapi +# 📈 wakapi - A WakaTime-compatible backend for coding statistics ## Usage * Create an empty MySQL database @@ -15,14 +15,21 @@ * `USE yourdatabasename;` * `INSERT INTO users (id, api_key) VALUES ('your_cool_nickname', '728f084c-85e0-41de-aa2a-b6cc871200c1');` (the latter value is your api key from `~/.wakatime.cfg`) +## Best Practices +It is recommended to use wakapi behind a **reverse proxy**, like [Caddy](https://caddyserver.com) or _nginx_ to enable **TLS encryption** (HTTPS). +However, if you want to expose your wakapi instance to the public anyway, you need to set `listen = 0.0.0.0` in `config.ini` + ## Todo * Persisted summaries / aggregations (for performance) * User sign up and log in * Additional endpoints for retrieving statistics data +* Enhanced UI + * Loading spinner + * Responsiveness * Dockerize * Unit tests -## Important note +## Important Note **This is not an alternative to using WakaTime.** It is just a custom, non-commercial, self-hosted application to collect coding statistics using the already existing editor plugins provided by the WakaTime community. It was created for personal use only and with the purpose of keeping the sovereignity of your own data. However, if you like the official product, **please support the authors and buy an official WakaTime subscription!** ## License diff --git a/config.ini b/config.ini index 0af95eb..27b018b 100644 --- a/config.ini +++ b/config.ini @@ -1,2 +1,3 @@ [server] +listen = 127.0.0.1 port = 3000 \ No newline at end of file diff --git a/main.go b/main.go index 0660b3a..dcb3bf1 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ func readConfig() *models.Config { log.Fatal(fmt.Sprintf("Fail to read file: %v", err)) } + addr := cfg.Section("server").Key("listen").MustString("127.0.0.1") port, err := strconv.Atoi(os.Getenv("PORT")) if err != nil { port = cfg.Section("server").Key("port").MustInt() @@ -50,6 +51,7 @@ func readConfig() *models.Config { return &models.Config{ Port: port, + Addr: addr, DbHost: dbHost, DbUser: dbUser, DbPassword: dbPassword, @@ -65,6 +67,8 @@ func main() { // Connect to database db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config)) db.LogMode(false) + db.DB().SetMaxIdleConns(1) + db.DB().SetMaxOpenConns(10) if err != nil { // log.Fatal("Could not connect to database.") log.Fatal(err) @@ -98,10 +102,10 @@ func main() { // API Routes heartbeats := apiRouter.Path("/heartbeat").Subrouter() - heartbeats.Methods("POST").HandlerFunc(heartbeatHandler.Post) + heartbeats.Methods(http.MethodPost).HandlerFunc(heartbeatHandler.Post) aggreagations := apiRouter.Path("/summary").Subrouter() - aggreagations.Methods("GET").HandlerFunc(summaryHandler.Get) + aggreagations.Methods(http.MethodGet).HandlerFunc(summaryHandler.Get) // Sub-Routes Setup router.PathPrefix("/api").Handler(negroni.Classic(). @@ -113,7 +117,7 @@ func main() { router.PathPrefix("/").Handler(negroni.Classic().With(negroni.Wrap(http.FileServer(http.Dir("./static"))))) // Listen HTTP - portString := "127.0.0.1:" + strconv.Itoa(config.Port) + portString := config.Addr + ":" + strconv.Itoa(config.Port) s := &http.Server{ Handler: router, Addr: portString, diff --git a/models/config.go b/models/config.go index 436814a..f719ce9 100644 --- a/models/config.go +++ b/models/config.go @@ -2,6 +2,7 @@ package models type Config struct { Port int + Addr string DbHost string DbUser string DbPassword string diff --git a/services/summary.go b/services/summary.go index 36019ee..084b1e3 100644 --- a/services/summary.go +++ b/services/summary.go @@ -77,7 +77,7 @@ func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, summaryTy } if key == "" { - continue + key = "unknown" } if _, ok := durations[key]; !ok { diff --git a/static/index.html b/static/index.html index 2a0bb98..8a37749 100644 --- a/static/index.html +++ b/static/index.html @@ -11,7 +11,7 @@ } .grid-container { - width: 60%; + width: 75%; display: grid; grid-template-areas: 'header header header'