Skip to content

Commit 026cd03

Browse files
author
Irae Hueck Costa
committed
```
Refactor Makefile and improve code formatting ```
1 parent 068a768 commit 026cd03

File tree

11 files changed

+48
-56
lines changed

11 files changed

+48
-56
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tests:
1818

1919
.PHONY: format
2020
format:
21-
find -type f -name \*.go | xargs -L1 go fmt
21+
find backend -type f -name \*.go | xargs -L1 go fmt
2222
prettier --html-whitespace-sensitivity ignore --write .
2323

2424
.PHONY: logs

backend/endpoints/dump.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import (
55
"time"
66

77
"github.com/gomodule/redigo/redis"
8-
"github.com/ihucos/counter.dev/utils"
98
"github.com/ihucos/counter.dev/lib"
109
"github.com/ihucos/counter.dev/models"
10+
"github.com/ihucos/counter.dev/utils"
1111
)
1212

1313
type UserDump struct {
14-
Id string `json:"id"`
15-
Token string `json:"token"`
16-
UUID string `json:"uuid"`
17-
IsSubscribed bool `json:"isSubscribed"`
18-
Prefs map[string]string `json:"prefs"`
14+
Id string `json:"id"`
15+
Token string `json:"token"`
16+
UUID string `json:"uuid"`
17+
IsSubscribed bool `json:"isSubscribed"`
18+
Prefs map[string]string `json:"prefs"`
1919
}
2020

2121
type SitesDumpVal struct {
@@ -127,7 +127,7 @@ func init() {
127127

128128
user.TouchDump()
129129

130-
archive := make(map[string]lib.QueryArchiveResult)
130+
archive := make(map[string]lib.QueryArchiveResult)
131131
now := utils.TimeNow(utcOffset)
132132
var err error
133133

@@ -148,7 +148,6 @@ func init() {
148148
oldestArchiveDate, err := ctx.App.QueryArchiveOldestDate(user.Id)
149149
ctx.CatchError(err)
150150

151-
152151
ctx.SendEventSourceData(EventSourceData{
153152
Type: "oldest-archive-date",
154153
Payload: oldestArchiveDate})

backend/endpoints/lang.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package endpoints
32

43
import (

backend/endpoints/recover.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func init() {
2222
ctx.LogEvent("recovery")
2323

2424
ctx.NoAutoCleanup()
25-
go func(){
25+
go func() {
2626
defer ctx.Cleanup()
2727
err = claimedUser.PasswordRecovery(ctx.App.Config.SMTPSecret)
2828
if err != nil {

backend/endpoints/subscribed.go

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package endpoints
32

43
import (

backend/endpoints/track.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func init() {
113113
if country == "" {
114114
country = ctx.R.Header.Get("CF-IPCountry")
115115
}
116-
116+
117117
if country != "" && country != "XX" {
118118
visit["country"] = strings.ToLower(country)
119119
}

backend/lib/app.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"net/http"
88
"os"
99
"path/filepath"
10+
"regexp"
1011
"runtime"
1112
"strings"
1213
"time"
13-
"regexp"
1414

1515
"log"
1616

@@ -44,7 +44,7 @@ func (ah appAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4444
ctx := ah.App.NewContext(w, r)
4545
go func() {
4646
<-r.Context().Done()
47-
if ! ctx.noAutoCleanup {
47+
if !ctx.noAutoCleanup {
4848
ctx.Cleanup()
4949
}
5050
}()
@@ -132,7 +132,7 @@ func NewApp() *App {
132132
} else {
133133
prefix = "./static"
134134
}
135-
} else if r.Host == "counter.dev"|| r.Host == "counter" || r.Host == "simple-web-analytics.com" {
135+
} else if r.Host == "counter.dev" || r.Host == "counter" || r.Host == "simple-web-analytics.com" {
136136
prefix = "/state/static/master"
137137
} else if r.Host == "www.counter.dev" || r.Host == "www.simple-web-analytics.com" {
138138
prefix = "/state/static/master"

backend/lib/archive.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ func (app *App) archiveHotVisitsPartForce(cursor int) int {
8686
app.Logger.Printf(
8787
"archiving visits failed, will retry later: %s\n", err)
8888
time.Sleep(30 * time.Second)
89-
app.Logger.Printf( "archiving visits will retry now")
89+
app.Logger.Printf("archiving visits will retry now")
9090
cursor, err = app.archiveHotVisitsPart(cursor)
9191
if err == nil {
92-
app.Logger.Printf( "archiving visits recovered")
92+
app.Logger.Printf("archiving visits recovered")
9393
return cursor
9494
}
9595
panic(err)
@@ -213,7 +213,6 @@ func (app *App) QueryArchive(queryArgs QueryArchiveArgs) (QueryArchiveResult, er
213213
return visits, nil
214214
}
215215

216-
217216
func (app *App) QueryArchiveOldestDate(userId string) (string, error) {
218217
var date string
219218
query := app.DB.Model(&Record{}).Select(

backend/lib/config.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
)
88

99
type Config struct {
10-
RedisUrl string
11-
Bind string
12-
CookieSecret []byte
13-
PasswordSalt []byte
14-
ArchiveDatabase string
15-
ArchiveMaxAge time.Duration
10+
RedisUrl string
11+
Bind string
12+
CookieSecret []byte
13+
PasswordSalt []byte
14+
ArchiveDatabase string
15+
ArchiveMaxAge time.Duration
1616
MailgunSecretApiKey string
17-
SMTPSecret string
17+
SMTPSecret string
1818
}
1919

2020
func env(env string) string {
@@ -32,7 +32,6 @@ func envDefault(env string, fallback string) string {
3232
return v
3333
}
3434

35-
3635
func envDuration(envName string) time.Duration {
3736
strVal := env(envName)
3837
duration, err := time.ParseDuration(strVal)
@@ -44,13 +43,13 @@ func envDuration(envName string) time.Duration {
4443

4544
func NewConfigFromEnv() Config {
4645
return Config{
47-
RedisUrl: envDefault("WEBSTATS_REDIS_URL", "redis://localhost:6379"),
48-
Bind: envDefault("WEBSTATS_BIND", ":8000"),
49-
CookieSecret: []byte(env("WEBSTATS_COOKIE_SECRET")),
50-
PasswordSalt: []byte(env("WEBSTATS_PASSWORD_SALT")),
51-
ArchiveDatabase: env("WEBSTATS_ARCHIVE_DATABASE"),
52-
ArchiveMaxAge: envDuration("WEBSTATS_ARCHIVE_MAX_AGE"),
53-
MailgunSecretApiKey: envDefault("WEBSTATS_MAILGUN_SECRET_API_KEY", "dummy"),
54-
SMTPSecret: envDefault("WEBSTATS_SMTP_SECRET", "dummy"),
46+
RedisUrl: envDefault("WEBSTATS_REDIS_URL", "redis://localhost:6379"),
47+
Bind: envDefault("WEBSTATS_BIND", ":8000"),
48+
CookieSecret: []byte(env("WEBSTATS_COOKIE_SECRET")),
49+
PasswordSalt: []byte(env("WEBSTATS_PASSWORD_SALT")),
50+
ArchiveDatabase: env("WEBSTATS_ARCHIVE_DATABASE"),
51+
ArchiveMaxAge: envDuration("WEBSTATS_ARCHIVE_MAX_AGE"),
52+
MailgunSecretApiKey: envDefault("WEBSTATS_MAILGUN_SECRET_API_KEY", "dummy"),
53+
SMTPSecret: envDefault("WEBSTATS_SMTP_SECRET", "dummy"),
5554
}
5655
}

backend/lib/ctx.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ type UserDataResp struct {
1919
}
2020

2121
type Ctx struct {
22-
W http.ResponseWriter
23-
R *http.Request
24-
OpenConns []io.Closer
25-
App *App
22+
W http.ResponseWriter
23+
R *http.Request
24+
OpenConns []io.Closer
25+
App *App
2626
noAutoCleanup bool
2727
}
2828

@@ -205,7 +205,6 @@ func (ctx *Ctx) CheckMethod(methods ...string) {
205205
}
206206
}
207207

208-
209208
func (ctx *Ctx) SendEventSourceData(data interface{}) {
210209
jsonBin, err := json.Marshal(data)
211210
ctx.CatchError(err)
@@ -217,7 +216,6 @@ func (ctx *Ctx) SendEventSourceData(data interface{}) {
217216
f.Flush()
218217
}
219218

220-
221219
func (ctx *Ctx) NoAutoCleanup() {
222220
ctx.noAutoCleanup = true
223221
}

backend/models/site.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package models
22

33
import (
44
"fmt"
5+
"gorm.io/gorm"
56
"math/rand"
67
"net/url"
7-
"time"
88
"strings"
9-
"gorm.io/gorm"
9+
"time"
1010

1111
"github.com/gomodule/redigo/redis"
1212
"github.com/ihucos/counter.dev/utils"
@@ -62,24 +62,24 @@ func NewVisitItemKey(key string) VisitItemKey {
6262
}
6363

6464
func (vik VisitItemKey) RedisType() string {
65-
for _, i := range fieldsHash {
66-
if i == vik.Field {
67-
return "hash"
68-
}
69-
}
70-
for _, i := range fieldsZet {
71-
if i == vik.Field {
72-
return "zet"
73-
}
74-
}
75-
return ""
65+
for _, i := range fieldsHash {
66+
if i == vik.Field {
67+
return "hash"
68+
}
69+
}
70+
for _, i := range fieldsZet {
71+
if i == vik.Field {
72+
return "zet"
73+
}
74+
}
75+
return ""
7676
}
7777

7878
type Site struct {
7979
redis redis.Conn
8080
id string
8181
userId string
82-
db *gorm.DB
82+
db *gorm.DB
8383
}
8484

8585
// taken from here at August 2020:
@@ -241,7 +241,6 @@ func (site Site) delHotVisits() error {
241241
return nil
242242
}
243243

244-
245244
func (site Site) delArchiveVisits() error {
246245
err := site.db.Table("records").Where("origin = ?", site.id).Delete(nil).Error
247246
if err != nil {

0 commit comments

Comments
 (0)