Skip to content

Commit a8d92b1

Browse files
committed
0.8.2 memory leak fixed + geolocation crash fixed + docs errata
1 parent 3a83832 commit a8d92b1

File tree

13 files changed

+101
-101
lines changed

13 files changed

+101
-101
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
![Version](https://img.shields.io/badge/version-0.8.1-orange.svg)
2+
![Version](https://img.shields.io/badge/version-0.8.2-orange.svg)
33
![Maintained YES](https://img.shields.io/badge/Maintained%3F-yes-green.svg)
44
![Ask Me Anything !](https://img.shields.io/badge/Ask%20me-anything-1abc9c.svg)
55

66
# ![logo](https://github.com/jolav/codetabs/blob/master/www/_public/icons/ct/ct64r.png?raw=true) **ONLINE TOOLS ([codetabs.com](https://codetabs.com))**
77

8-
**version 0.8.1**
8+
**version 0.8.2**
99

1010
1. [Count LOC (lines of code) online from github/gitlab repos or zipped uploaded folder](#count-loc-online)
1111
2. [CORS proxy](#cors-proxy)
@@ -238,9 +238,9 @@ Scale : Set width:height , if one parameter is -1 it will automatically determin
238238
- [ ] **WWW** clean unused parts, css, etc
239239
- [ ] **WWW** change web design
240240

241-
- [ ] **ALL** Update Tests
241+
- [ ] **ALL** Fix the tests. They are outdated and unusable.
242242

243-
- [ ] **LOC** Save Historical Data
243+
- [X] **LOC** Save Historical Data
244244
- [X] **LOC** Gitlab
245245
- [ ] **LOC** Bitbucket
246246
- [X] **LOC** Use same colours for languages as github
@@ -250,7 +250,7 @@ Scale : Set width:height , if one parameter is -1 it will automatically determin
250250
- [X] **LOC** box to ignore patterns such as ./vendor
251251
- [ ] **LOC** update line count when hiding languages
252252

253-
- [ ] **STARS** Save Historical Data (unstar = problem)
253+
- [X] **STARS** Save Historical Data (unstar = problem)
254254
- [X] **STARS** Gitlab
255255
- [ ] **STARS** Optimize doing far fewer requests. Extrapolate data
256256

alexa/alexa.go

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ const (
2323
DATA_FILE_URL = "https://s3.amazonaws.com/alexa-static/top-1m.csv.zip"
2424
)
2525

26+
var a alexa
27+
2628
type alexa struct {
2729
config config
2830
alexaList map[string]int
29-
output output
3031
}
3132

3233
type config struct {
@@ -36,12 +37,12 @@ type config struct {
3637
DataFileURL string
3738
}
3839

39-
type output struct {
40+
type outputAlexa struct {
4041
Web string `json:"web"`
4142
Rank int `json:"rank"`
4243
}
4344

44-
func (a *alexa) Router(w http.ResponseWriter, r *http.Request) {
45+
func Router(w http.ResponseWriter, r *http.Request) {
4546
params := strings.Split(strings.ToLower(r.URL.Path), "/")
4647
path := params[1:len(params)]
4748
if path[len(path)-1] == "" { // remove last empty slot after /
@@ -52,38 +53,41 @@ func (a *alexa) Router(w http.ResponseWriter, r *http.Request) {
5253
u.BadRequest(w, r)
5354
return
5455
}
56+
oa := outputAlexa{
57+
Web: "",
58+
Rank: 0,
59+
}
5560
r.ParseForm()
56-
web := r.Form.Get("web")
57-
if web == "" {
61+
oa.Web = r.Form.Get("web")
62+
if oa.Web == "" {
5863
u.BadRequest(w, r)
5964
return
6065
}
61-
a.doAlexaRequest(w, web)
66+
oa.doAlexaRequest(w)
6267
}
6368

64-
func (a *alexa) doAlexaRequest(w http.ResponseWriter, web string) {
65-
a.output.Web = web
66-
a.output.Rank = a.alexaList[a.output.Web]
67-
if a.output.Rank != 0 {
68-
u.SendJSONToClient(w, a.output, 200)
69+
func (oa *outputAlexa) doAlexaRequest(w http.ResponseWriter) {
70+
oa.Rank = a.alexaList[oa.Web]
71+
if oa.Rank != 0 {
72+
u.SendJSONToClient(w, oa, 200)
6973
return
7074
}
71-
if strings.HasPrefix(a.output.Web, "www.") {
72-
a.output.Web = a.output.Web[4:len(a.output.Web)]
73-
a.output.Rank = a.alexaList[a.output.Web]
74-
if a.output.Rank != 0 {
75-
u.SendJSONToClient(w, a.output, 200)
75+
if strings.HasPrefix(oa.Web, "www.") {
76+
oa.Web = oa.Web[4:len(oa.Web)]
77+
oa.Rank = a.alexaList[oa.Web]
78+
if oa.Rank != 0 {
79+
u.SendJSONToClient(w, oa, 200)
7680
return
7781
}
7882
}
79-
if !strings.HasPrefix(a.output.Web, "www.") {
80-
a.output.Rank = a.alexaList["www."+a.output.Web]
81-
if a.output.Rank != 0 {
82-
u.SendJSONToClient(w, a.output, 200)
83+
if !strings.HasPrefix(oa.Web, "www.") {
84+
oa.Rank = a.alexaList["www."+oa.Web]
85+
if oa.Rank != 0 {
86+
u.SendJSONToClient(w, oa, 200)
8387
return
8488
}
8589
}
86-
msg := fmt.Sprintf("%s not in alexa top 1 million", a.output.Web)
90+
msg := fmt.Sprintf("%s not in alexa top 1 million", oa.Web)
8791
u.ErrorResponse(w, msg)
8892
return
8993
}
@@ -108,7 +112,8 @@ func (a *alexa) loadDataInMemory() {
108112
}
109113
}
110114

111-
func (a *alexa) OnceADayTask() {
115+
func OnceADayTask() {
116+
a = newAlexa(false)
112117
t := time.Now()
113118
n := time.Date(t.Year(), t.Month(), t.Day(), 3, 10, 10, 0, t.Location())
114119
d := n.Sub(t)
@@ -157,7 +162,7 @@ func (a *alexa) unzipCsv() {
157162
}
158163
}
159164

160-
func NewAlexa(test bool) alexa {
165+
func newAlexa(test bool) alexa {
161166
a := alexa{
162167
config: config{
163168
DataFilePath: DATA_FILE_PATH,
@@ -166,10 +171,6 @@ func NewAlexa(test bool) alexa {
166171
DataFileURL: DATA_FILE_URL,
167172
},
168173
alexaList: make(map[string]int),
169-
output: output{
170-
Web: "",
171-
Rank: 0,
172-
},
173174
}
174175
if test {
175176
a.config.DataFilePath = DATA_FILE_PATH_TEST

alexa/alexa_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestAlexaApi(t *testing.T) {
1616
validFormat = "Bad request, valid format is 'api.codetabs.com/v1/{service}?{param}=value' .Please read our docs at https://codetabs.com"
1717
)
1818

19-
a := NewAlexa(true)
19+
//a := NewAlexa(true)
2020

2121
type alexaTestOutput struct {
2222
Domain string `json:"domain"`
@@ -65,7 +65,7 @@ func TestAlexaApi(t *testing.T) {
6565
}
6666
if pass {
6767
rr := httptest.NewRecorder()
68-
handler := http.HandlerFunc(a.Router)
68+
handler := http.HandlerFunc(Router)
6969

7070
handler.ServeHTTP(rr, req)
7171
if rr.Code != test.statusCode {

geolocation/geolocation.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ type geoData struct {
3939
Longitude float32 `json:"longitude"`
4040
}
4141

42-
func (g *geoip) Router(w http.ResponseWriter, r *http.Request) {
42+
func Router(w http.ResponseWriter, r *http.Request) {
4343
params := strings.Split(strings.ToLower(r.URL.Path), "/")
4444
path := params[1:len(params)]
4545
if path[len(path)-1] == "" { // remove last empty slot after /
4646
path = path[:len(path)-1]
4747
}
4848
//log.Printf("Going ....%s %s %d", path, r.Method, len(path))
49-
if len(path) < 2 || path[0] != "v1" {
49+
if len(path) < 3 || path[0] != "v1" {
5050
u.BadRequest(w, r)
5151
return
5252
}
@@ -55,6 +55,7 @@ func (g *geoip) Router(w http.ResponseWriter, r *http.Request) {
5555
u.BadRequest(w, r)
5656
return
5757
}
58+
g := newGeoLocation(false)
5859
r.ParseForm()
5960
target := strings.ToLower(r.Form.Get("q"))
6061
if target == "" {
@@ -64,7 +65,7 @@ func (g *geoip) Router(w http.ResponseWriter, r *http.Request) {
6465
}
6566

6667
func (g *geoip) doGeoRequest(w http.ResponseWriter, format, target string) {
67-
g.cleanGeoData()
68+
//g.cleanGeoData()
6869
addr, err := net.LookupIP(target)
6970
if err != nil {
7071
msg := fmt.Sprintf("%s is a unknown host, not a valid IP or hostname", target)
@@ -84,13 +85,13 @@ func (g *geoip) getGeoDataFromDB() {
8485
db, err := ip2location.OpenDB(g.config.dbFilePath)
8586
if err != nil {
8687
log.Println("ERROR GEOIP 1 =", err)
87-
g.cleanGeoData()
88+
//g.cleanGeoData()
8889
return
8990
}
9091
results, err := db.Get_all(g.geoData.Ip)
9192
if err != nil {
9293
log.Println("ERROR GEOIP 2 =", err)
93-
g.cleanGeoData()
94+
//g.cleanGeoData()
9495
return
9596
}
9697
//u.PrettyPrintStruct(results)
@@ -109,7 +110,7 @@ func (g *geoip) cleanGeoData() {
109110
g.geoData = geoData{}
110111
}
111112

112-
func NewGeoLocation(test bool) geoip {
113+
func newGeoLocation(test bool) geoip {
113114
g := geoip{
114115
config: config{
115116
dbFilePath: DB_FILE_PATH,

geolocation/geolocation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestGeoipApi(t *testing.T) {
15-
g := NewGeoLocation(true)
15+
//g := newGeoLocation(true)
1616

1717
for _, test := range geoipTests {
1818
var to = geoipTestOutput{}
@@ -32,7 +32,7 @@ func TestGeoipApi(t *testing.T) {
3232
}
3333
if pass {
3434
rr := httptest.NewRecorder()
35-
handler := http.HandlerFunc(g.Router)
35+
handler := http.HandlerFunc(Router)
3636
handler.ServeHTTP(rr, req)
3737
if rr.Code != test.statusCode {
3838
t.Errorf("%s got %v want %v\n", test.endpoint, rr.Code, test.statusCode)

headers/headers.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import (
1010
u "github.com/jolav/codetabs/_utils"
1111
)
1212

13+
type headersRequest struct {
14+
headers
15+
domain string
16+
}
17+
1318
type headers []header
1419

1520
type header map[string]string
1621

17-
func (h *headers) Router(w http.ResponseWriter, r *http.Request) {
22+
func Router(w http.ResponseWriter, r *http.Request) {
1823
params := strings.Split(strings.ToLower(r.URL.Path), "/")
1924
path := params[1:len(params)]
2025
if path[len(path)-1] == "" { // remove last empty slot after /
@@ -25,17 +30,18 @@ func (h *headers) Router(w http.ResponseWriter, r *http.Request) {
2530
u.BadRequest(w, r)
2631
return
2732
}
33+
hr := newHeadersRequest(false)
2834
r.ParseForm()
29-
domain := r.Form.Get("domain")
30-
if domain == "" || len(path) != 2 {
35+
hr.domain = r.Form.Get("domain")
36+
if hr.domain == "" || len(path) != 2 {
3137
u.BadRequest(w, r)
3238
return
3339
}
34-
doHeadersRequest(w, r, domain)
40+
hr.doHeadersRequest(w, r)
3541
}
3642

37-
func doHeadersRequest(w http.ResponseWriter, r *http.Request, domain string) {
38-
hs := headers{}
43+
func (hr *headersRequest) doHeadersRequest(
44+
w http.ResponseWriter, r *http.Request) {
3945
notMoreRedirections := false
4046
count := 0
4147
const curl = "curl -fsSI "
@@ -46,28 +52,28 @@ func doHeadersRequest(w http.ResponseWriter, r *http.Request, domain string) {
4652
}
4753

4854
for !notMoreRedirections && count < 10 {
49-
rawData, err := u.GenericCommandSH(curl + domain)
55+
rawData, err := u.GenericCommandSH(curl + hr.domain)
5056
if err != nil {
5157
msg := fmt.Sprintf("ERROR %s -> %s %s",
5258
r.URL.RequestURI(),
5359
curlStatus[err.Error()],
54-
domain,
60+
hr.domain,
5561
)
5662
u.ErrorResponse(w, msg)
5763
return
5864
}
59-
parseHeadString(string(rawData), &hs)
60-
if hs[count]["Location"] == "" {
65+
parseHeadString(string(rawData), &hr.headers)
66+
if hr.headers[count]["Location"] == "" {
6167
notMoreRedirections = true
6268
//fmt.Println(`No more redirections`)
6369
} else {
64-
domain = hs[count]["Location"]
70+
hr.domain = hr.headers[count]["Location"]
6571
//fmt.Println(`Redirecting to ... `, domain)
6672
}
6773
count++
6874
}
6975

70-
u.SendJSONToClient(w, hs, 200)
76+
u.SendJSONToClient(w, hr.headers, 200)
7177
return
7278
}
7379

@@ -100,7 +106,10 @@ func parseHeadString(rawData string, hs *headers) {
100106
*hs = append(*hs, myheader)
101107
}
102108

103-
func NewHeaders(test bool) headers {
104-
h := headers{}
105-
return h
109+
func newHeadersRequest(test bool) headersRequest {
110+
hr := headersRequest{
111+
headers: []header{},
112+
domain: "",
113+
}
114+
return hr
106115
}

loc/loc.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type sourceReader interface {
5959
exceedsSize(http.ResponseWriter, *loc) bool
6060
}
6161

62-
func (l *loc) Router(w http.ResponseWriter, r *http.Request) {
62+
func Router(w http.ResponseWriter, r *http.Request) {
6363
params := strings.Split(strings.ToLower(r.URL.Path), "/")
6464
path := params[1:len(params)]
6565
if path[len(path)-1] == "" { // remove last empty slot after /
@@ -70,15 +70,8 @@ func (l *loc) Router(w http.ResponseWriter, r *http.Request) {
7070
u.BadRequest(w, r)
7171
return
7272
}
73-
// clean
74-
l.repo = ""
75-
l.branch = ""
76-
l.ignored = []string{}
77-
l.source = ""
78-
l.date = ""
79-
l.size = 0
80-
l.languagesIN = []languageIN{}
81-
l.languagesOUT = []languageOUT{}
73+
74+
l := newLoc(false)
8275

8376
if r.Method == "POST" {
8477
l.orderInt++
@@ -349,7 +342,7 @@ func (l *loc) storeData() {
349342
//go store.SaveDataLoc(d)
350343
}
351344

352-
func NewLoc(test bool) loc {
345+
func newLoc(test bool) loc {
353346
l := loc{
354347
order: "0",
355348
orderInt: 0,

0 commit comments

Comments
 (0)