Add ability to query the article index

This commit is contained in:
Jason Streifling 2025-02-12 17:55:44 +01:00
parent 0cd964343b
commit d6c58cf532
3 changed files with 21 additions and 1 deletions

View File

@ -57,7 +57,7 @@ func newConfig() *Config {
MaxImgWidth: 1920, MaxImgWidth: 1920,
PDFDir: "/var/www/cpolis/pdfs", PDFDir: "/var/www/cpolis/pdfs",
Port: ":1664", Port: ":1664",
Version: "v0.16.2", Version: "v0.17.0",
WebDir: "/var/www/cpolis/web", WebDir: "/var/www/cpolis/web",
} }
} }

View File

@ -1,6 +1,7 @@
package calls package calls
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
@ -109,3 +110,21 @@ func ServeClicks(db *b.DB) http.HandlerFunc {
} }
} }
} }
func QueryArticles(i *b.Index) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
result, err := i.Query(r.PathValue("query"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintf("error encoding results to JSON: %v", err), http.StatusInternalServerError)
return
}
}
}

View File

@ -60,6 +60,7 @@ func main() {
mux.HandleFunc("GET /article/delete/{id}", f.DeleteArticle(config, db, sessions)) mux.HandleFunc("GET /article/delete/{id}", f.DeleteArticle(config, db, sessions))
mux.HandleFunc("GET /article/edit/{id}", f.EditArticle(config, db, sessions)) mux.HandleFunc("GET /article/edit/{id}", f.EditArticle(config, db, sessions))
mux.HandleFunc("GET /article/publish/{id}", f.PublishArticle(config, db, sessions, index)) mux.HandleFunc("GET /article/publish/{id}", f.PublishArticle(config, db, sessions, index))
mux.HandleFunc("GET /article/query/{query}", c.QueryArticles(index))
mux.HandleFunc("GET /article/reject/{id}", f.RejectArticle(config, db, sessions)) mux.HandleFunc("GET /article/reject/{id}", f.RejectArticle(config, db, sessions))
mux.HandleFunc("GET /article/review-delete/{id}", f.ReviewArticle(config, db, sessions, "delete", "Artikel löschen", "Löschen")) mux.HandleFunc("GET /article/review-delete/{id}", f.ReviewArticle(config, db, sessions, "delete", "Artikel löschen", "Löschen"))
mux.HandleFunc("GET /article/review-edit/{id}", f.ReviewArticle(config, db, sessions, "allow-edit", "Artikel bearbeiten", "Bearbeiten erlauben")) mux.HandleFunc("GET /article/review-edit/{id}", f.ReviewArticle(config, db, sessions, "allow-edit", "Artikel bearbeiten", "Bearbeiten erlauben"))