Add ability to listen on public interface.

Add ability to handle unknown keys.
Update Readme.
This commit is contained in:
Ferdinand Muetsch 2019-05-21 14:02:04 +02:00
parent f7d2a39a00
commit 86fe3a4bae
7 changed files with 22 additions and 8 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
launch.json launch.json
.vscode .vscode
.env .env
wakapi

View File

@ -1,4 +1,4 @@
# wakapi # 📈 wakapi - A WakaTime-compatible backend for coding statistics
## Usage ## Usage
* Create an empty MySQL database * Create an empty MySQL database
@ -15,14 +15,21 @@
* `USE yourdatabasename;` * `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`) * `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 ## Todo
* Persisted summaries / aggregations (for performance) * Persisted summaries / aggregations (for performance)
* User sign up and log in * User sign up and log in
* Additional endpoints for retrieving statistics data * Additional endpoints for retrieving statistics data
* Enhanced UI
* Loading spinner
* Responsiveness
* Dockerize * Dockerize
* Unit tests * 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!** **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 ## License

View File

@ -1,2 +1,3 @@
[server] [server]
listen = 127.0.0.1
port = 3000 port = 3000

10
main.go
View File

@ -43,6 +43,7 @@ func readConfig() *models.Config {
log.Fatal(fmt.Sprintf("Fail to read file: %v", err)) 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")) port, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil { if err != nil {
port = cfg.Section("server").Key("port").MustInt() port = cfg.Section("server").Key("port").MustInt()
@ -50,6 +51,7 @@ func readConfig() *models.Config {
return &models.Config{ return &models.Config{
Port: port, Port: port,
Addr: addr,
DbHost: dbHost, DbHost: dbHost,
DbUser: dbUser, DbUser: dbUser,
DbPassword: dbPassword, DbPassword: dbPassword,
@ -65,6 +67,8 @@ func main() {
// Connect to database // Connect to database
db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config)) db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
db.LogMode(false) db.LogMode(false)
db.DB().SetMaxIdleConns(1)
db.DB().SetMaxOpenConns(10)
if err != nil { if err != nil {
// log.Fatal("Could not connect to database.") // log.Fatal("Could not connect to database.")
log.Fatal(err) log.Fatal(err)
@ -98,10 +102,10 @@ func main() {
// API Routes // API Routes
heartbeats := apiRouter.Path("/heartbeat").Subrouter() heartbeats := apiRouter.Path("/heartbeat").Subrouter()
heartbeats.Methods("POST").HandlerFunc(heartbeatHandler.Post) heartbeats.Methods(http.MethodPost).HandlerFunc(heartbeatHandler.Post)
aggreagations := apiRouter.Path("/summary").Subrouter() aggreagations := apiRouter.Path("/summary").Subrouter()
aggreagations.Methods("GET").HandlerFunc(summaryHandler.Get) aggreagations.Methods(http.MethodGet).HandlerFunc(summaryHandler.Get)
// Sub-Routes Setup // Sub-Routes Setup
router.PathPrefix("/api").Handler(negroni.Classic(). 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"))))) router.PathPrefix("/").Handler(negroni.Classic().With(negroni.Wrap(http.FileServer(http.Dir("./static")))))
// Listen HTTP // Listen HTTP
portString := "127.0.0.1:" + strconv.Itoa(config.Port) portString := config.Addr + ":" + strconv.Itoa(config.Port)
s := &http.Server{ s := &http.Server{
Handler: router, Handler: router,
Addr: portString, Addr: portString,

View File

@ -2,6 +2,7 @@ package models
type Config struct { type Config struct {
Port int Port int
Addr string
DbHost string DbHost string
DbUser string DbUser string
DbPassword string DbPassword string

View File

@ -77,7 +77,7 @@ func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, summaryTy
} }
if key == "" { if key == "" {
continue key = "unknown"
} }
if _, ok := durations[key]; !ok { if _, ok := durations[key]; !ok {

View File

@ -11,7 +11,7 @@
} }
.grid-container { .grid-container {
width: 60%; width: 75%;
display: grid; display: grid;
grid-template-areas: grid-template-areas:
'header header header' 'header header header'