Skip to content

Commit 7e95e34

Browse files
committed
0.8.0 database to save loc + stars
1 parent e5b6da4 commit 7e95e34

File tree

14 files changed

+346
-8
lines changed

14 files changed

+346
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ _private.js
1414
_private.json
1515
a_private.go
1616
banned.list
17+
codetabsData.db3
18+
*.db3
1719
#
1820

1921
#

README.md

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

2-
![Version](https://img.shields.io/badge/version-0.7.20-orange.svg)
2+
![Version](https://img.shields.io/badge/version-0.8.0-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.7.20**
8+
**version 0.8.0**
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)

geolocation/geolocation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (g *geoip) getGeoDataFromDB() {
9393
g.cleanGeoData()
9494
return
9595
}
96-
u.PrettyPrintStruct(results)
96+
//u.PrettyPrintStruct(results)
9797
g.geoData.Country_code = results.Country_short
9898
g.geoData.Country_name = results.Country_long
9999
g.geoData.Region_name = results.Region

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/jolav/codetabs
22

33
go 1.14
44

5-
require github.com/ip2location/ip2location-go/v9 v9.1.0
5+
require (
6+
github.com/ip2location/ip2location-go/v9 v9.1.0
7+
github.com/mattn/go-sqlite3 v1.14.16 // indirect
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/ip2location/ip2location-go/v9 v9.1.0 h1:DVlKxIcjA7CsA0cgzbidwe6eKzpbkS313LsH9ceutxI=
22
github.com/ip2location/ip2location-go/v9 v9.1.0/go.mod h1:s5SV6YZL10TpfPpXw//7fEJC65G/yH7Oh+Tjq9JcQEQ=
3+
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
4+
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

loc/loc.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"os"
1212
"strconv"
1313
"strings"
14+
"time"
1415

1516
u "github.com/jolav/codetabs/_utils"
17+
"github.com/jolav/codetabs/store"
1618
)
1719

1820
const (
@@ -84,6 +86,7 @@ func (l *loc) Router(w http.ResponseWriter, r *http.Request) {
8486
l.doLocUploadRequest(w, r)
8587
return
8688
}
89+
8790
r.ParseForm()
8891
l.branch = r.Form.Get("branch")
8992
if r.Form.Get("ignored") != "" {
@@ -134,6 +137,7 @@ func (l *loc) doLocRepoRequest(w http.ResponseWriter, r *http.Request) {
134137
total2.Lines += v.Lines
135138
}
136139
l.languagesOUT = append(l.languagesOUT, total2)
140+
l.storeData()
137141
u.SendJSONToClient(w, l.languagesOUT, 200)
138142
return
139143
*/
@@ -218,7 +222,12 @@ func (l *loc) doLocRepoRequest(w http.ResponseWriter, r *http.Request) {
218222
total.Lines += v.Lines
219223
}
220224
l.languagesOUT = append(l.languagesOUT, total)
221-
225+
if l.branch == "" { // cant call gitlab with master
226+
l.branch = "master"
227+
}
228+
if len(l.ignored) == 0 && l.branch == "master" {
229+
l.storeData()
230+
}
222231
u.SendJSONToClient(w, l.languagesOUT, 200)
223232
u.GenericCommand(destroyTemporalDir)
224233
}
@@ -317,11 +326,29 @@ func (l *loc) doLocUploadRequest(w http.ResponseWriter, r *http.Request) {
317326
total.Lines += v.Lines
318327
}
319328
l.languagesOUT = append(l.languagesOUT, total)
320-
329+
//l.storeData()
321330
u.SendJSONToClient(w, l.languagesOUT, 200)
322331
u.GenericCommand(destroyTemporalDir)
323332
}
324333

334+
func (l *loc) storeData() {
335+
d := store.NewDataLoc()
336+
337+
dataJSON, err := json.Marshal(l.languagesOUT)
338+
if err != nil {
339+
log.Printf("ERROR Marshaling %s\n", err)
340+
d.Data = string(`{}`)
341+
} else {
342+
d.Data = string(dataJSON)
343+
}
344+
d.Date = time.Now().Format("2006-01-02 15:04:05.000")
345+
d.Repo = l.source + "/" + l.repo
346+
d.Source = l.source
347+
//u.PrettyPrintStruct(d)
348+
go d.SaveDataLoc()
349+
//go store.SaveDataLoc(d)
350+
}
351+
325352
func NewLoc(test bool) loc {
326353
l := loc{
327354
order: "0",

main.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020
lo "github.com/jolav/codetabs/loc"
2121
px "github.com/jolav/codetabs/proxy"
2222
st "github.com/jolav/codetabs/stars"
23+
"github.com/jolav/codetabs/store"
2324
vg "github.com/jolav/codetabs/video2gif"
2425
we "github.com/jolav/codetabs/weather"
2526
)
2627

27-
var version = "0.7.20"
28+
var version = "0.8.0"
2829
var when = "undefined"
2930

3031
type Conf struct {
@@ -45,6 +46,13 @@ func main() {
4546
checkMode(&c)
4647
//u.PrettyPrintStruct(c)
4748

49+
// Database
50+
db, err := store.NewDB()
51+
if err != nil {
52+
log.Fatal("Error connecting DataBase => ", err)
53+
}
54+
store.MyDB = db
55+
4856
// Custom Error Log File + Custom Hits Log File
4957
var mylog *os.File
5058
if c.Mode == "production" {
@@ -101,10 +109,31 @@ func mw(next http.HandlerFunc, service string, c Conf) http.HandlerFunc {
101109
return
102110
}
103111
}
112+
104113
next.ServeHTTP(w, r)
105114
})
106115
}
107116

117+
/*
118+
mux.HandleFunc("/vnstats/v1/", checkValid(
119+
func(w http.ResponseWriter, r *http.Request) {
120+
vnstatsRouter(w, r, a)
121+
}, a.c.APIKey),
122+
)
123+
124+
func checkValid(next http.HandlerFunc, test string) http.HandlerFunc {
125+
return func(w http.ResponseWriter, r *http.Request) {
126+
r.ParseForm()
127+
valid := r.Form.Get("test")
128+
if valid != test {
129+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
130+
return
131+
}
132+
next.ServeHTTP(w, r)
133+
}
134+
}
135+
*/
136+
108137
func checkFlags() {
109138
versionFlag := flag.Bool("v", false, "Show current version and exit")
110139
flag.Parse()

stars/github.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (s *stars) doGHStarsRequest(w http.ResponseWriter, r *http.Request) {
2727
}
2828
}
2929
if totalStars == 0 { // repo exists but has no stars
30+
s.storeData()
3031
u.SendJSONToClient(w, s.data, 200)
3132
return
3233
}
@@ -47,6 +48,7 @@ func (s *stars) doGHStarsRequest(w http.ResponseWriter, r *http.Request) {
4748
last.Quantity = totalStars
4849
s.data = append(s.data, last)
4950
}
51+
s.storeData()
5052
u.SendJSONToClient(w, s.data, 200)
5153
}
5254

stars/gitlab.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (s *stars) doGLStarsRequest(w http.ResponseWriter, r *http.Request) {
2727
}
2828
}
2929
if totalStars == 0 { // repo exists but has no stars
30+
s.storeData()
3031
u.SendJSONToClient(w, s.data, 200)
3132
return
3233
}
@@ -36,6 +37,7 @@ func (s *stars) doGLStarsRequest(w http.ResponseWriter, r *http.Request) {
3637
if len(s.stars) > 0 {
3738
s.convertGLStarsToData()
3839
}
40+
s.storeData()
3941
u.SendJSONToClient(w, s.data, 200)
4042
}
4143

stars/stars.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
package stars
44

55
import (
6+
"encoding/json"
67
"fmt"
8+
"log"
79
"net/http"
810
"strings"
911
"time"
1012

1113
u "github.com/jolav/codetabs/_utils"
14+
"github.com/jolav/codetabs/store"
1215
)
1316

1417
type star struct {
@@ -79,6 +82,24 @@ func (s *stars) Router(w http.ResponseWriter, r *http.Request) {
7982
}
8083
}
8184

85+
func (s *stars) storeData() {
86+
d := store.NewDataStars()
87+
88+
dataJSON, err := json.Marshal(s.data)
89+
if err != nil {
90+
log.Printf("ERROR Marshaling %s\n", err)
91+
d.Data = string(`{}`)
92+
} else {
93+
d.Data = string(dataJSON)
94+
}
95+
d.Date = time.Now().Format("2006-01-02 15:04:05.000")
96+
d.Repo = s.source + "/" + s.repo
97+
d.Source = s.source
98+
d.Total = len(s.stars)
99+
//u.PrettyPrintStruct(d)
100+
go d.SaveDataStars()
101+
}
102+
82103
func (s *stars) cleanStarsStruct() {
83104
s.stars = []*star{}
84105
s.headerLink = ""

0 commit comments

Comments
 (0)