diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8e0ae73..c3e88d5 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "Build", "type": "shell", - "command": "go build" + "command": "go build --tags \"libsqlite3 linux sqlite_fts5\"" } ] } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 6dd509b..d4e285a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ ADD *.go /app/ ADD go.mod /app/ ADD go.sum /app/ WORKDIR /app -RUN go build --tags "libsqlite3 linux" +RUN go build --tags "libsqlite3 linux sqlite_fts5" FROM alpine:3.12 RUN apk add --no-cache sqlite-dev diff --git a/config.go b/config.go index b9dd4c2..080c3b6 100644 --- a/config.go +++ b/config.go @@ -49,11 +49,12 @@ type configBlog struct { Title string `mapstructure:"title"` Description string `mapstructure:"description"` Pagination int `mapstructure:"pagination"` + DefaultSection string `mapstructure:"defaultsection"` Sections map[string]*section `mapstructure:"sections"` Taxonomies []*taxonomy `mapstructure:"taxonomies"` Menus map[string]*menu `mapstructure:"menus"` Photos *photos `mapstructure:"photos"` - DefaultSection string `mapstructure:"defaultsection"` + Search *search `mapstructure:"search"` CustomPages []*customPage `mapstructure:"custompages"` Telegram *configTelegram `mapstructure:"telegram"` } @@ -88,6 +89,14 @@ type photos struct { Description string `mapstructure:"description"` } +type search struct { + Enabled bool `mapstructure:"enabled"` + Path string `mapstructure:"path"` + Title string `mapstructure:"title"` + Description string `mapstructure:"description"` + Placeholder string `mapstructure:"placeholder"` +} + type customPage struct { Path string `mapstructure:"path"` Template string `mapstructure:"template"` diff --git a/databaseMigrations.go b/databaseMigrations.go index 5eedb1a..87c6aed 100644 --- a/databaseMigrations.go +++ b/databaseMigrations.go @@ -15,17 +15,17 @@ func migrateDb() error { Name: "00001", Func: func(tx *sql.Tx) error { _, err := tx.Exec(` - CREATE TABLE posts (path text not null primary key, content text, published text, updated text, blog text not null, section text); - CREATE TABLE post_parameters (id integer primary key autoincrement, path text not null, parameter text not null, value text); - CREATE INDEX index_pp_path on post_parameters (path); - CREATE TRIGGER AFTER DELETE on posts BEGIN delete from post_parameters where path = old.path; END; - CREATE TABLE indieauthauth (time text not null, code text not null, me text not null, client text not null, redirect text not null, scope text not null); - CREATE TABLE indieauthtoken (time text not null, token text not null, me text not null, client text not null, scope text not null); - CREATE INDEX index_iat_token on indieauthtoken (token); - CREATE TABLE autocert (key text not null primary key, data blob not null, created text not null); - CREATE TABLE activitypub_followers (blog text not null, follower text not null, inbox text not null, primary key (blog, follower)); - CREATE TABLE webmentions (id integer primary key autoincrement, source text not null, target text not null, created integer not null, status text not null default "new", title text, content text, author text, type text, UNIQUE(source, target)); - CREATE INDEX index_wm_target on webmentions (target); + create table posts (path text not null primary key, content text, published text, updated text, blog text not null, section text); + create table post_parameters (id integer primary key autoincrement, path text not null, parameter text not null, value text); + create index index_pp_path on post_parameters (path); + create trigger after delete on posts begin delete from post_parameters where path = old.path; end; + create table indieauthauth (time text not null, code text not null, me text not null, client text not null, redirect text not null, scope text not null); + create table indieauthtoken (time text not null, token text not null, me text not null, client text not null, scope text not null); + create index index_iat_token on indieauthtoken (token); + create table autocert (key text not null primary key, data blob not null, created text not null); + create table activitypub_followers (blog text not null, follower text not null, inbox text not null, primary key (blog, follower)); + create table webmentions (id integer primary key autoincrement, source text not null, target text not null, created integer not null, status text not null default "new", title text, content text, author text, type text, unique(source, target)); + create index index_wm_target on webmentions (target); `) return err }, @@ -34,7 +34,7 @@ func migrateDb() error { Name: "00002", Func: func(tx *sql.Tx) error { _, err := tx.Exec(` - DROP TABLE autocert; + drop table autocert; `) return err }, @@ -43,8 +43,19 @@ func migrateDb() error { Name: "00003", Func: func(tx *sql.Tx) error { _, err := tx.Exec(` - DROP TRIGGER AFTER; - CREATE TRIGGER trigger_posts_delete_pp AFTER DELETE on posts BEGIN delete from post_parameters where path = old.path; END; + drop trigger AFTER; + create trigger trigger_posts_delete_pp after delete on posts begin delete from post_parameters where path = old.path; end; + `) + return err + }, + }, + &migrator.Migration{ + Name: "00004", + Func: func(tx *sql.Tx) error { + _, err := tx.Exec(` + create view view_posts_with_title as select id, path, title, content, published, updated, blog, section from (select p.rowid as id, p.path as path, pp.value as title, content, published, updated, blog, section from posts p left outer join post_parameters pp on p.path = pp.path where pp.parameter = 'title'); + create virtual table posts_fts using fts5(path unindexed, title, content, published unindexed, updated unindexed, blog unindexed, section unindexed, content=view_posts_with_title, content_rowid=id); + insert into posts_fts(posts_fts) values ('rebuild'); `) return err }, diff --git a/http.go b/http.go index 73e078c..1418373 100644 --- a/http.go +++ b/http.go @@ -228,6 +228,19 @@ func buildHandler() (http.Handler, error) { r.With(cacheMiddleware, minifier.Middleware).Get(photoPath+paginationPath, handler) } + // Search + if blogConfig.Search.Enabled { + searchPath := blogPath + blogConfig.Search.Path + handler := serveSearch(blog, searchPath) + r.With(cacheMiddleware, minifier.Middleware).Get(searchPath, handler) + r.With(cacheMiddleware, minifier.Middleware).Post(searchPath, handler) + searchResultPath := searchPath + "/" + searchPlaceholder + resultHandler := serveSearchResults(blog, searchResultPath) + r.With(cacheMiddleware, minifier.Middleware).Get(searchResultPath, resultHandler) + r.With(cacheMiddleware, minifier.Middleware).Get(searchResultPath+feedPath, resultHandler) + r.With(cacheMiddleware, minifier.Middleware).Get(searchResultPath+paginationPath, resultHandler) + } + // Blog var mw []func(http.Handler) http.Handler if appConfig.ActivityPub.Enabled { diff --git a/posts.go b/posts.go index 5860fb9..a2f4c3c 100644 --- a/posts.go +++ b/posts.go @@ -155,6 +155,14 @@ func servePhotos(blog string, path string) func(w http.ResponseWriter, r *http.R }) } +func serveSearchResults(blog string, path string) func(w http.ResponseWriter, r *http.Request) { + return serveIndex(&indexConfig{ + blog: blog, + path: path, + template: templateIndex, + }) +} + type indexConfig struct { blog string path string @@ -167,6 +175,10 @@ type indexConfig struct { func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { + search := chi.URLParam(r, "search") + if search != "" { + search = searchDecode(search) + } pageNoString := chi.URLParam(r, "page") pageNo, _ := strconv.Atoi(pageNoString) var sections []string @@ -183,6 +195,7 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { taxonomy: ic.tax, taxonomyValue: ic.taxValue, parameter: ic.parameter, + search: search, }}, appConfig.Blogs[ic.blog].Pagination) p.SetPage(pageNo) var posts []*post @@ -198,6 +211,8 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { } else if ic.section != nil { title = ic.section.Title description = ic.section.Description + } else if search != "" { + title = fmt.Sprintf("%s: %s", appConfig.Blogs[ic.blog].Search.Title, search) } // Check if feed if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed { @@ -217,6 +232,10 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { if len(template) == 0 { template = templateIndex } + path := ic.path + if strings.Contains(path, searchPlaceholder) { + path = strings.ReplaceAll(path, searchPlaceholder, searchEncode(search)) + } render(w, template, &renderData{ blogString: ic.blog, Canonical: appConfig.Server.PublicAddress + r.URL.Path, @@ -227,8 +246,8 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { HasPrev: p.HasPrev(), HasNext: p.HasNext(), First: ic.path, - Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage), - Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage), + Prev: fmt.Sprintf("%s/page/%d", path, prevPage), + Next: fmt.Sprintf("%s/page/%d", path, nextPage), }, }) } diff --git a/postsDb.go b/postsDb.go index 6f8a996..9fbc457 100644 --- a/postsDb.go +++ b/postsDb.go @@ -159,6 +159,7 @@ func (p *post) createOrReplace(new bool) error { return err } finishWritingToDb() + rebuildFTSIndex() if !postExists { defer p.postPostHooks() } else { @@ -176,10 +177,19 @@ func deletePost(path string) error { return err } _, err = appDbExec("delete from posts where path = @path", sql.Named("path", p.Path)) + if err != nil { + return err + } + rebuildFTSIndex() defer p.postDeleteHooks() return reloadRouter() } +func rebuildFTSIndex() { + _, _ = appDbExec("insert into posts_fts(posts_fts) values ('rebuild')") + return +} + func postExists(path string) bool { result := 0 row, err := appDbQueryRow("select exists(select 1 from posts where path = @path)", sql.Named("path", path)) @@ -203,6 +213,7 @@ func getPost(path string) (*post, error) { } type postsRequestConfig struct { + search string blog string path string limit int @@ -218,6 +229,10 @@ func buildQuery(config *postsRequestConfig) (query string, args []interface{}) { args = []interface{}{} defaultSelection := "select p.path as path, coalesce(content, ''), coalesce(published, ''), coalesce(updated, ''), coalesce(blog, ''), coalesce(section, ''), coalesce(parameter, ''), coalesce(value, '') " postsTable := "posts" + if config.search != "" { + postsTable = "posts_fts(@search)" + args = append(args, sql.Named("search", config.search)) + } if config.blog != "" { postsTable = "(select * from " + postsTable + " where blog = @blog)" args = append(args, sql.Named("blog", config.blog)) diff --git a/render.go b/render.go index ae970bc..eff0bf7 100644 --- a/render.go +++ b/render.go @@ -28,6 +28,7 @@ const templateError = "error" const templateIndex = "index" const templateTaxonomy = "taxonomy" const templatePhotos = "photos" +const templateSearch = "search" var templates map[string]*template.Template var templateFunctions template.FuncMap diff --git a/search.go b/search.go new file mode 100644 index 0000000..400dcee --- /dev/null +++ b/search.go @@ -0,0 +1,45 @@ +package main + +import ( + "encoding/base64" + "net/http" + "net/url" + "strings" +) + +const searchPlaceholder = "{search}" + +func serveSearch(blog string, path string) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + if q := r.Form.Get("q"); q != "" { + http.Redirect(w, r, path+"/"+searchEncode(q), http.StatusFound) + return + } + render(w, templateSearch, &renderData{ + blogString: blog, + Canonical: appConfig.Server.PublicAddress + path, + }) + } +} + +func searchEncode(search string) string { + return url.PathEscape(strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(search)), "/", "_")) +} + +func searchDecode(encoded string) string { + encoded, err := url.PathUnescape(encoded) + if err != nil { + return "" + } + encoded = strings.ReplaceAll(encoded, "_", "/") + db, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + return "" + } + return string(db) +} diff --git a/templates/photos.gohtml b/templates/photos.gohtml index 08ad5f3..25d5437 100644 --- a/templates/photos.gohtml +++ b/templates/photos.gohtml @@ -1,9 +1,5 @@ {{ define "title" }} - {{ if .Blog.Photos.Title }} - {{ .Blog.Photos.Title }} - {{ .Blog.Title }} - {{ else }} - {{ .Blog.Title }} - {{ end }} + {{ with .Blog.Photos.Title }}{{ . }} - {{ end }}{{ .Blog.Title }} {{ end }} {{ define "main" }} diff --git a/templates/search.gohtml b/templates/search.gohtml new file mode 100644 index 0000000..90ccb38 --- /dev/null +++ b/templates/search.gohtml @@ -0,0 +1,20 @@ +{{ define "title" }} + {{ with .Blog.Search.Title }}{{ . }} - {{ end }}{{ .Blog.Title }} +{{ end }} + +{{ define "main" }} +
+ {{ with .Blog.Search.Title }}

{{ . }}

{{ end }} + {{ with .Blog.Search.Description }}{{ md . }}{{ end }} + {{ if (or .Blog.Search.Title .Blog.Search.Description) }} +
+ {{ end }} +
+ + +
+{{ end }} + +{{ define "search" }} + {{ template "base" . }} +{{ end }} \ No newline at end of file diff --git a/templates/strings/de.yaml b/templates/strings/de.yaml index c845dab..e3b3909 100644 --- a/templates/strings/de.yaml +++ b/templates/strings/de.yaml @@ -9,4 +9,5 @@ translations: "Übersetzungen" share: "Teilen" speak: "Lies mir bitte vor." stopspeak: "Hör auf zu sprechen!" -oldcontent: "⚠️ Dieser Eintrag ist bereits über ein Jahr alt. Er ist möglicherweise nicht mehr aktuell. Meinungen können sich geändert haben." \ No newline at end of file +oldcontent: "⚠️ Dieser Eintrag ist bereits über ein Jahr alt. Er ist möglicherweise nicht mehr aktuell. Meinungen können sich geändert haben." +search: "Suchen" \ No newline at end of file diff --git a/templates/strings/default.yaml b/templates/strings/default.yaml index 52521dd..049e588 100644 --- a/templates/strings/default.yaml +++ b/templates/strings/default.yaml @@ -21,4 +21,5 @@ approve: "Approve" anoncomment: "You can also create an anonymous comment." interactions: "Interactions" send: "Send" -interactionslabel: "Have you published a response to this? Paste the URL here." \ No newline at end of file +interactionslabel: "Have you published a response to this? Paste the URL here." +search: "Search" \ No newline at end of file