Browse Source

minor refactorings

master
Christian Müller 8 years ago
parent
commit
81a148d3a3
  1. 17
      server.go
  2. 7
      stats.go
  3. 13
      storage.go
  4. 20
      test/main.go

17
server.go

@ -6,7 +6,6 @@ import (
"html/template" "html/template"
"io" "io"
"io/ioutil" "io/ioutil"
"math"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -80,7 +79,7 @@ func main() {
return c.String(code, statuses[code]) return c.String(code, statuses[code])
} }
defer incViews(n, db) defer incViews(n, db)
fraud := fraudelent(n) fraud := n.fraudelent()
if fraud { if fraud {
n.Ads = mdTmplHTML(ads) n.Ads = mdTmplHTML(ads)
} }
@ -94,7 +93,7 @@ func main() {
var content string var content string
if code == http.StatusOK { if code == http.StatusOK {
defer incViews(n, db) defer incViews(n, db)
if fraudelent(n) { if n.fraudelent() {
code = http.StatusForbidden code = http.StatusForbidden
content = statuses[code] content = statuses[code]
c.Logger().Warnf("/%s/export failed (code: %d)", id, code) c.Logger().Warnf("/%s/export failed (code: %d)", id, code)
@ -209,18 +208,6 @@ func main() {
e.Logger.Fatal(e.StartServer(s)) e.Logger.Fatal(e.StartServer(s))
} }
func fraudelent(n *Note) bool {
res := rexpLink.FindAllString(n.Text, -1)
if len(res) < 3 {
return false
}
stripped := rexpLink.ReplaceAllString(n.Text, "")
l1 := len(n.Text)
l2 := len(stripped)
return n.Views > 100 &&
int(math.Ceil(100*float64(l1-l2)/float64(l1))) > fraudThreshold
}
func checkRecaptcha(c echo.Context, captchaResp string) bool { func checkRecaptcha(c echo.Context, captchaResp string) bool {
resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", url.Values{ resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", url.Values{
"secret": []string{os.Getenv("RECAPTCHA_SECRET")}, "secret": []string{os.Getenv("RECAPTCHA_SECRET")},

7
stats.go

@ -47,10 +47,9 @@ func flush(db *sql.DB) (int, error) {
func incViews(n *Note, db *sql.DB) { func incViews(n *Note, db *sql.DB) {
views := n.Views views := n.Views
if val, ok := stats.Load(n.ID); ok { if viewsCached, found := stats.Load(n.ID); found {
intVal, ok := val.(int) if val, ok := viewsCached.(int); ok {
if ok { views = val
views = intVal
} }
} }
stats.Store(n.ID, views+1) stats.Store(n.ID, views+1)

13
storage.go

@ -7,6 +7,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"html/template" "html/template"
"math"
"math/rand" "math/rand"
"net/http" "net/http"
"regexp" "regexp"
@ -31,6 +32,18 @@ type Note struct {
Content, Ads template.HTML Content, Ads template.HTML
} }
func (n *Note) fraudelent() bool {
res := rexpLink.FindAllString(n.Text, -1)
if len(res) < 3 {
return false
}
stripped := rexpLink.ReplaceAllString(n.Text, "")
l1 := len(n.Text)
l2 := len(stripped)
return n.Views > 100 &&
int(math.Ceil(100*float64(l1-l2)/float64(l1))) > fraudThreshold
}
func save(c echo.Context, db *sql.DB, n *Note) (*Note, error) { func save(c echo.Context, db *sql.DB, n *Note) (*Note, error) {
if n.Password != "" { if n.Password != "" {
clean := n.Password clean := n.Password

20
test/main.go

@ -67,8 +67,6 @@ func main() {
ExpectJson("Payload", "Bad request: note length not accepted") ExpectJson("Payload", "Bad request: note length not accepted")
testNote := "# Hello World!\nThis is a _test_ note!" testNote := "# Hello World!\nThis is a _test_ note!"
testNoteHTML := "<h1>Hello World!</h1>\n<p>This is a <em>test</em> note!</p>"
var id string
tooLongNote := testNote tooLongNote := testNote
for len(tooLongNote) < 50000 { for len(tooLongNote) < 50000 {
@ -84,6 +82,7 @@ func main() {
ExpectJson("Success", false). ExpectJson("Success", false).
ExpectJson("Payload", "Bad request: note length not accepted") ExpectJson("Payload", "Bad request: note length not accepted")
var id string
frisby.Create("Test publishing: correct inputs; no password"). frisby.Create("Test publishing: correct inputs; no password").
Post(service+"/"). Post(service+"/").
SetData("tos", "on"). SetData("tos", "on").
@ -100,6 +99,7 @@ func main() {
id = noteID id = noteID
}) })
testNoteHTML := "<h1>Hello World!</h1>\n<p>This is a <em>test</em> note!</p>"
frisby.Create("Test retrieval of new note"). frisby.Create("Test retrieval of new note").
Get(service + "/" + id). Get(service + "/" + id).
Send(). Send().
@ -118,18 +118,18 @@ func main() {
ExpectHeader("Content-type", "text/plain; charset=UTF-8"). ExpectHeader("Content-type", "text/plain; charset=UTF-8").
ExpectContent(testNote) ExpectContent(testNote)
// TODO: fix this frisby.Create("Test opening fake service on note").
// frisby.Create("Test opening fake service on note"). Get(service + "/" + id + "/asd").
// Get(service + "/" + id + "/asd"). Send().
// Send(). ExpectStatus(404).
// ExpectStatus(404). ExpectContent("Not Found")
// PrintBody().
// ExpectContent("Not found") // TODO: fix this
// frisby.Create("Test opening fake service on note 2"). // frisby.Create("Test opening fake service on note 2").
// Get(service + "/" + id + "/exports"). // Get(service + "/" + id + "/exports").
// Send(). // Send().
// ExpectStatus(404). // ExpectStatus(404).
// ExpectContent("Not found") // ExpectContent("Not Found")
frisby.Create("Test stats of new note"). frisby.Create("Test stats of new note").
Get(service + "/" + id + "/stats"). Get(service + "/" + id + "/stats").

Loading…
Cancel
Save