|
|
|
@ -1,8 +1,8 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
|
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"html/template" |
|
|
|
"html/template" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
@ -19,8 +19,6 @@ import ( |
|
|
|
"github.com/labstack/gommon/log" |
|
|
|
"github.com/labstack/gommon/log" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const fraudThreshold = 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var TEST_MODE = false |
|
|
|
var TEST_MODE = false |
|
|
|
|
|
|
|
|
|
|
|
type Template struct{ templates *template.Template } |
|
|
|
type Template struct{ templates *template.Template } |
|
|
|
@ -41,14 +39,14 @@ func main() { |
|
|
|
|
|
|
|
|
|
|
|
TEST_MODE = os.Getenv("TEST_MODE") != "" |
|
|
|
TEST_MODE = os.Getenv("TEST_MODE") != "" |
|
|
|
|
|
|
|
|
|
|
|
var ads []byte |
|
|
|
|
|
|
|
adsFName := os.Getenv("ADS") |
|
|
|
adsFName := os.Getenv("ADS") |
|
|
|
|
|
|
|
var ads template.HTML |
|
|
|
if adsFName != "" { |
|
|
|
if adsFName != "" { |
|
|
|
var err error |
|
|
|
data, err := ioutil.ReadFile(adsFName) |
|
|
|
ads, err = ioutil.ReadFile(adsFName) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
e.Logger.Errorf("couldn't read file %s: %v", adsFName, err) |
|
|
|
e.Logger.Errorf("couldn't read file %s: %v", adsFName, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ads = mdTmplHTML(data) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
go flushStatsLoop(e.Logger, db) |
|
|
|
go flushStatsLoop(e.Logger, db) |
|
|
|
@ -79,11 +77,8 @@ func main() { |
|
|
|
return c.String(code, statuses[code]) |
|
|
|
return c.String(code, statuses[code]) |
|
|
|
} |
|
|
|
} |
|
|
|
defer incViews(n, db) |
|
|
|
defer incViews(n, db) |
|
|
|
fraud := n.fraudelent() |
|
|
|
n.Ads = ads |
|
|
|
if fraud { |
|
|
|
c.Logger().Debugf("/%s delivered (fraud: %t)", id, n.Fraud()) |
|
|
|
n.Ads = mdTmplHTML(ads) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
c.Logger().Debugf("/%s delivered (fraud: %t)", id, fraud) |
|
|
|
|
|
|
|
return c.Render(code, "Note", n) |
|
|
|
return c.Render(code, "Note", n) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
@ -93,7 +88,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 n.fraudelent() { |
|
|
|
if n.Fraud() { |
|
|
|
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) |
|
|
|
@ -112,12 +107,11 @@ func main() { |
|
|
|
c.Logger().Errorf("/%s/stats failed (code: %d)", id, code) |
|
|
|
c.Logger().Errorf("/%s/stats failed (code: %d)", id, code) |
|
|
|
return c.String(code, statuses[code]) |
|
|
|
return c.String(code, statuses[code]) |
|
|
|
} |
|
|
|
} |
|
|
|
n.prepare() |
|
|
|
stats := fmt.Sprintf("Published: %s\n Views: %d", n.Published, n.Views) |
|
|
|
buf := bytes.NewBuffer([]byte{}) |
|
|
|
if !n.Edited.IsZero() { |
|
|
|
e.Renderer.Render(buf, "Stats", n, c) |
|
|
|
stats = fmt.Sprintf("Published: %s\n Edited: %s\n Views: %d", n.Published, n.Edited, n.Views) |
|
|
|
n.Content = template.HTML(buf.String()) |
|
|
|
} |
|
|
|
c.Logger().Debugf("/%s/stats delivered", id) |
|
|
|
return c.String(code, stats) |
|
|
|
return c.Render(code, "Note", n) |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
e.GET("/:id/edit", func(c echo.Context) error { |
|
|
|
e.GET("/:id/edit", func(c echo.Context) error { |
|
|
|
|