Skip to content

Commit b4d471e

Browse files
committed
refactor and allow sorting by creation date
1 parent 9e37164 commit b4d471e

File tree

7 files changed

+69
-59
lines changed

7 files changed

+69
-59
lines changed

Makefile

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
HASH=$(shell git describe --always)
22
LDFLAGS=-ldflags "-s -w -X main.Version=${HASH}"
33

4-
build:
4+
prereq:
55
go get -d -v github.com/tdewolff/minify/...
66
go install -v github.com/tdewolff/minify/cmd/minify
77
go get -d -v github.com/jteeuwen/go-bindata/...
88
go install -v github.com/jteeuwen/go-bindata/go-bindata
9+
10+
bundle:
911
rm -rf assets
1012
cp -r static assets
1113
cd assets && gzip -9 -r *
@@ -15,21 +17,13 @@ build:
1517
cp templates/header.html assets/header.html
1618
cp templates/viewedit.html assets/viewedit.html
1719
cp templates/prism.js assets/prism.js
18-
# minify static/css/rwtxt.css | gzip -9 > assets/rwtxt.css
19-
# minify static/css/normalize.css | gzip -9 > assets/normalize.css
20-
# minify static/css/dropzone.css | gzip -9 > assets/dropzone.css
21-
# minify static/js/rwtxt.js | gzip -9 > assets/rwtxt.js
22-
# # gzip -9 -c static/js/rwtxt.js > assets/rwtxt.js
23-
# minify static/js/dropzone.js | gzip -9 > assets/dropzone.js
24-
# minify static/css/prism.css | gzip -9 > assets/prism.css
25-
# minify static/js/prism.js | gzip -9 > assets/prism.js
26-
# gzip -9 -c static/img/logo.png > assets/logo.png
27-
# cp -r static/img/favicon assets/
28-
# cd assets/favicon && gzip -9 *
20+
21+
exec: prereq bundle
2922
go-bindata -pkg rwtxt -nocompress assets assets/img assets/js assets/css assets/img/favicon
30-
go get -v --tags "fts4" ${LDFLAGS} ./...
23+
cd cmd/rwtxt && go build -v --tags "fts4" ${LDFLAGS} && cp rwtxt ../../
3124

32-
exec: build
25+
quick: bundle
26+
go-bindata -pkg rwtxt -nocompress assets assets/img assets/js assets/css assets/img/favicon
3327
cd cmd/rwtxt && go build -v --tags "fts4" ${LDFLAGS} && cp rwtxt ../../
3428

3529
run: build

cmd/rwtxt/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ var (
2121
func main() {
2222
var (
2323
err error
24-
export = flag.Bool("export", false, "export uploads to {{TIMESTAMP}}-uploads.zip and posts to {{TIMESTAMP}}-posts.zip")
24+
export = flag.Bool("export", false, "export uploads to {{TIMESTAMP}}-uploads.zip and posts to {{TIMESTAMP}}-posts.zip")
2525
resizeWidth = flag.Int("resizewidth", -1, "image width to resize on the fly")
2626
resizeOnUpload = flag.Bool("resizeonupload", false, "resize on upload")
2727
resizeOnRequest = flag.Bool("resizeonrequest", false, "resize on request")
2828
debug = flag.Bool("debug", false, "debug mode")
2929
showVersion = flag.Bool("v", false, "show version")
3030
profileMemory = flag.Bool("memprofile", false, "profile memory")
3131
database = flag.String("db", "rwtxt.db", "name of the database")
32-
listen = flag.String("listen", rwtxt.DefaultBind, "interface:port to listen on")
32+
listen = flag.String("listen", ":8152", "interface:port to listen on")
3333
private = flag.Bool("private", false, "private setup (allows listing of public notes)")
34+
created = flag.Bool("created", false, "order by date created rather than date modified")
3435
)
3536
flag.Parse()
3637

@@ -83,19 +84,19 @@ func main() {
8384
return
8485
}
8586

86-
config := rwtxt.Config{Private: *private,
87+
config := rwtxt.Config{
88+
Bind: *listen,
89+
Private: *private,
8790
ResizeWidth: *resizeWidth,
8891
ResizeOnRequest: *resizeOnRequest,
8992
ResizeOnUpload: *resizeOnUpload,
93+
OrderByCreated: *created,
9094
}
9195

9296
rwt, err := rwtxt.New(fs, config)
9397
if err != nil {
9498
panic(err)
9599
}
96-
if listen != nil && *listen != "" {
97-
rwt.Bind = *listen
98-
}
99100

100101
err = rwt.Serve()
101102
if err != nil {

pkg/db/db.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,17 +1092,22 @@ func (fs *FileSystem) SetSimilar(id string, similarids []string) (err error) {
10921092
}
10931093

10941094
// GetAll returns all the files for a given domain
1095-
func (fs *FileSystem) GetAll(domain string) (files []File, err error) {
1095+
func (fs *FileSystem) GetAll(domain string, created ...bool) (files []File, err error) {
10961096
fs.Lock()
10971097
defer fs.Unlock()
1098-
files, err = fs.getAllFromPreparedQuery(`
1099-
SELECT fs.id,fs.slug,fs.created,fs.modified,fts.data,fs.history,fs.views FROM fs
1098+
q := `SELECT fs.id,fs.slug,fs.created,fs.modified,fts.data,fs.history,fs.views FROM fs
11001099
INNER JOIN fts ON fs.id=fts.id
11011100
INNER JOIN domains ON fs.domainid=domains.id
11021101
WHERE
11031102
domains.name = ?
11041103
AND LENGTH(fts.data) > 0
1105-
ORDER BY fs.modified DESC`, domain)
1104+
`
1105+
if len(created) > 0 && created[0] {
1106+
q += "ORDER BY fs.created DESC"
1107+
} else {
1108+
q += "ORDER BY fs.modified DESC"
1109+
}
1110+
files, err = fs.getAllFromPreparedQuery(q, domain)
11061111
for i := range files {
11071112
files[i].Domain = domain
11081113
}
@@ -1126,17 +1131,24 @@ func (fs *FileSystem) GetSimilar(fileid string) (files []File, err error) {
11261131
}
11271132

11281133
// GetTopX returns the info from a file
1129-
func (fs *FileSystem) GetTopX(domain string, num int) (files []File, err error) {
1134+
func (fs *FileSystem) GetTopX(domain string, num int, created ...bool) (files []File, err error) {
11301135
fs.Lock()
11311136
defer fs.Unlock()
1132-
return fs.getAllFromPreparedQuery(`
1137+
q := `
11331138
SELECT fs.id,fs.slug,fs.created,fs.modified,fts.data,fs.history,fs.views FROM fs
11341139
INNER JOIN fts ON fs.id=fts.id
11351140
INNER JOIN domains ON fs.domainid=domains.id
11361141
WHERE
11371142
domains.name = ?
11381143
AND LENGTH(fts.data) > 0
1139-
ORDER BY fs.modified DESC LIMIT ?`, domain, num)
1144+
`
1145+
if len(created) > 0 && created[0] {
1146+
q += "ORDER BY fs.created DESC"
1147+
} else {
1148+
q += "ORDER BY fs.modified DESC"
1149+
}
1150+
q += " LIMIT ?"
1151+
return fs.getAllFromPreparedQuery(q, domain, num)
11401152
}
11411153

11421154
// GetTopX returns the info from a file

rwtxt.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,35 @@ import (
1818
const DefaultBind = ":8152"
1919

2020
type RWTxt struct {
21-
Bind string // interface:port to listen on, defaults to DefaultBind.
22-
config Config
21+
Config Config
2322
viewEditTemplate *template.Template
2423
mainTemplate *template.Template
2524
loginTemplate *template.Template
2625
listTemplate *template.Template
2726
prismTemplate []string
2827
fs *db.FileSystem
2928
wsupgrader websocket.Upgrader
30-
ResizeWidth int
31-
ResizeOnUpload bool
32-
ResizeOnRequest bool
3329
}
3430

3531
type Config struct {
32+
Bind string // interface:port to listen on, defaults to DefaultBind.
3633
Private bool
3734
ResizeWidth int
3835
ResizeOnUpload bool
3936
ResizeOnRequest bool
37+
OrderByCreated bool
4038
}
4139

42-
func New(fs *db.FileSystem, config Config) (*RWTxt, error) {
40+
func New(fs *db.FileSystem, configUser ...Config) (*RWTxt, error) {
41+
config := Config{
42+
Bind: ":8152",
43+
}
44+
if len(configUser) > 0 {
45+
config = configUser[0]
46+
}
4347
rwt := &RWTxt{
44-
Bind: DefaultBind,
45-
fs: fs,
46-
config: config,
47-
ResizeOnUpload: config.ResizeOnUpload,
48-
ResizeOnRequest: config.ResizeOnRequest,
49-
ResizeWidth: config.ResizeWidth,
48+
Config: config,
49+
fs: fs,
5050
wsupgrader: websocket.Upgrader{
5151
ReadBufferSize: 1024,
5252
WriteBufferSize: 1024,
@@ -128,9 +128,9 @@ func (rwt *RWTxt) Serve() (err error) {
128128
}
129129
}
130130
}()
131-
log.Infof("listening on %v", rwt.Bind)
131+
log.Infof("listening on %v", rwt.Config.Bind)
132132
http.HandleFunc("/", rwt.Handler)
133-
return http.ListenAndServe(rwt.Bind, nil)
133+
return http.ListenAndServe(rwt.Config.Bind, nil)
134134
}
135135

136136
func (rwt *RWTxt) isSignedIn(w http.ResponseWriter, r *http.Request, domain string) (signedin bool, domainkey string, defaultDomain string, domainList []string, domainKeys map[string]string) {
@@ -248,7 +248,7 @@ Disallow: /`))
248248
return tr.handleUploads(w, r, tr.Page)
249249
} else if tr.Domain != "" && tr.Page == "" {
250250
if r.URL.Query().Get("q") != "" {
251-
if tr.Domain == "public" && !rwt.config.Private {
251+
if tr.Domain == "public" && !rwt.Config.Private {
252252
return tr.handleMain(w, r, "can't search public")
253253
}
254254
return tr.handleSearch(w, r, tr.Domain, r.URL.Query().Get("q"))
@@ -258,11 +258,11 @@ Disallow: /`))
258258
} else if tr.Domain != "" && tr.Page != "" {
259259
log.Debugf("[%s/%s]", tr.Domain, tr.Page)
260260
if tr.Page == "list" {
261-
if tr.Domain == "public" && !rwt.config.Private {
261+
if tr.Domain == "public" && !rwt.Config.Private {
262262
return tr.handleMain(w, r, "can't list public")
263263
}
264264

265-
files, _ := rwt.fs.GetAll(tr.Domain)
265+
files, _ := rwt.fs.GetAll(tr.Domain, tr.RWTxtConfig.OrderByCreated)
266266
for i := range files {
267267
files[i].Data = ""
268268
files[i].DataHTML = template.HTML("")

template_render.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"database/sql"
88
"encoding/json"
99
"fmt"
10-
"github.com/disintegration/imaging"
1110
"html/template"
1211
"image/jpeg"
1312
"io"
@@ -18,6 +17,8 @@ import (
1817
"strings"
1918
"time"
2019

20+
"github.com/disintegration/imaging"
21+
2122
log "github.com/cihub/seelog"
2223
"github.com/schollz/rwtxt/pkg/db"
2324
"github.com/schollz/rwtxt/pkg/utils"
@@ -57,6 +58,7 @@ type TemplateRender struct {
5758
Languages []string
5859
LanguageJS []template.JS
5960
rwt *RWTxt
61+
RWTxtConfig Config
6062
}
6163

6264
type Payload struct {
@@ -110,7 +112,8 @@ func init() {
110112

111113
func NewTemplateRender(rwt *RWTxt) *TemplateRender {
112114
tr := &TemplateRender{
113-
rwt: rwt,
115+
rwt: rwt,
116+
RWTxtConfig: rwt.Config,
114117
}
115118
return tr
116119
}
@@ -204,10 +207,10 @@ func (tr *TemplateRender) handleMain(w http.ResponseWriter, r *http.Request, mes
204207
signedin = false
205208
}
206209
tr.SignedIn = signedin
207-
tr.DomainIsPrivate = !ispublic && (tr.Domain != "public" || tr.rwt.config.Private)
208-
tr.PrivateEnvironment = tr.rwt.config.Private
210+
tr.DomainIsPrivate = !ispublic && (tr.Domain != "public" || tr.rwt.Config.Private)
211+
tr.PrivateEnvironment = tr.rwt.Config.Private
209212
tr.DomainExists = domainErr == nil
210-
tr.Files, err = tr.rwt.fs.GetTopX(tr.Domain, 10)
213+
tr.Files, err = tr.rwt.fs.GetTopX(tr.Domain, 10, tr.RWTxtConfig.OrderByCreated)
211214
if err != nil {
212215
log.Debug(err)
213216
}
@@ -513,10 +516,10 @@ func (tr *TemplateRender) handleUploads(w http.ResponseWriter, r *http.Request,
513516
http.Error(w, err.Error(), http.StatusBadRequest)
514517
return
515518
}
516-
log.Debug("ResizeOnRequest", tr.rwt.ResizeOnRequest)
517-
log.Debug("ResizeWidth", tr.rwt.ResizeWidth)
519+
log.Debug("ResizeOnRequest", tr.rwt.Config.ResizeOnRequest)
520+
log.Debug("ResizeWidth", tr.rwt.Config.ResizeWidth)
518521
log.Debug("name", name)
519-
if tr.rwt.ResizeWidth > 0 && tr.rwt.ResizeOnRequest && (strings.Contains(strings.ToLower(name), ".jpg") || strings.Contains(strings.ToLower(name), ".jpeg")) {
522+
if tr.rwt.Config.ResizeWidth > 0 && tr.rwt.Config.ResizeOnRequest && (strings.Contains(strings.ToLower(name), ".jpg") || strings.Contains(strings.ToLower(name), ".jpeg")) {
520523
// Get resized image
521524
name, data, _, err = tr.rwt.fs.GetResizedImage(id)
522525
if err != nil && err != sql.ErrNoRows {
@@ -554,7 +557,7 @@ func (tr *TemplateRender) handleUploads(w http.ResponseWriter, r *http.Request,
554557
return err
555558
}
556559

557-
img = imaging.Resize(img, tr.rwt.ResizeWidth, 0, imaging.Lanczos)
560+
img = imaging.Resize(img, tr.rwt.Config.ResizeWidth, 0, imaging.Lanczos)
558561

559562
var bufout bytes.Buffer
560563
gw := gzip.NewWriter(&bufout)
@@ -614,15 +617,15 @@ func (tr *TemplateRender) handleUpload(w http.ResponseWriter, r *http.Request) (
614617
}
615618
defer file.Close()
616619

617-
if tr.rwt.ResizeWidth > 0 && tr.rwt.ResizeOnUpload && (strings.Contains(strings.ToLower(info.Filename), ".jpg") || strings.Contains(strings.ToLower(info.Filename), ".jpeg")) {
620+
if tr.rwt.Config.ResizeWidth > 0 && tr.rwt.Config.ResizeOnUpload && (strings.Contains(strings.ToLower(info.Filename), ".jpg") || strings.Contains(strings.ToLower(info.Filename), ".jpeg")) {
618621
log.Debug("process jpg upload")
619622
img, err := jpeg.Decode(file)
620623
if err != nil {
621624
http.Error(w, err.Error(), http.StatusBadRequest)
622625
return err
623626
}
624627

625-
img = imaging.Resize(img, tr.rwt.ResizeWidth, 0, imaging.Lanczos)
628+
img = imaging.Resize(img, tr.rwt.Config.ResizeWidth, 0, imaging.Lanczos)
626629

627630
var bufout bytes.Buffer
628631
err = jpeg.Encode(&bufout, img, nil)
@@ -702,7 +705,7 @@ func (tr *TemplateRender) handleExport(w http.ResponseWriter, r *http.Request) (
702705
if !tr.SignedIn {
703706
return tr.handleMain(w, r, "must sign in")
704707
}
705-
files, _ := tr.rwt.fs.GetAll(tr.Domain)
708+
files, _ := tr.rwt.fs.GetAll(tr.Domain, tr.RWTxtConfig.OrderByCreated)
706709
for i := range files {
707710
files[i].DataHTML = template.HTML("")
708711
}

templates/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ <h1>{{.NumResults}} results for '{{.Search}}'</h1>
99
{{range .Files}}
1010
<p>
1111
<a href="/{{$.Domain}}/{{.ID}}">{{.Slug}}</a>
12-
<small class="grey">{{.Modified.Format "Jan 2 3:04pm 2006"}}</small>
12+
<small class="grey">{{ if $.RWTxtConfig.OrderByCreated}}{{.Created.Format "Jan 2 3:04pm 2006"}}{{else}}{{.Modified.Format "Jan 2 3:04pm 2006"}}{{end}}</small>
1313

1414
<blockquote><em>{{.DataHTML}}</em></blockquote>
1515
</p>

templates/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h2>Most recent <small>(<a href="/{{.Domain}}/list">all posts</a>)</small></h2>
5353
{{range .Files}}
5454
<li>
5555
<a href="/{{$.Domain}}/{{if eq (len .Slug) 0}}{{.ID}}{{else}}{{.Slug}}{{end}}">{{if eq (len .Slug) 0}}{{.ID}}{{else}}{{.Slug}}{{end}}</a>
56-
&nbsp;<small class="grey">{{.Modified.Format "Mon Jan 2 2006"}}</small>
56+
&nbsp;<small class="grey">{{ if $.RWTxtConfig.OrderByCreated}}{{.Created.Format "Jan 2 3:04pm 2006"}}{{else}}{{.Modified.Format "Jan 2 3:04pm 2006"}}{{end}}</small>
5757
</li>
5858
{{end}}
5959
</ul>
@@ -65,7 +65,7 @@ <h2>Most edited</h2>
6565
{{range .MostActiveList}}
6666
<li>
6767
<a href="/{{$.Domain}}/{{if eq (len .Slug) 0}}{{.ID}}{{else}}{{.Slug}}{{end}}">{{if eq (len .Slug) 0}}{{.ID}}{{else}}{{.Slug}}{{end}}</a>
68-
&nbsp;<small class="grey">{{.Modified.Format "Mon Jan 2 2006"}}</small>
68+
&nbsp;<small class="grey">{{ if $.RWTxtConfig.OrderByCreated}}{{.Created.Format "Jan 2 3:04pm 2006"}}{{else}}{{.Modified.Format "Jan 2 3:04pm 2006"}}{{end}}</small>
6969
</li>
7070
{{end}}
7171
</ul>

0 commit comments

Comments
 (0)