Adding debugging and content type

This commit is contained in:
farhan 2025-01-30 02:11:26 +00:00
parent e361ed6b12
commit 476477d778

View File

@ -40,6 +40,10 @@ func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*") (*w).Header().Set("Access-Control-Allow-Origin", "*")
} }
func setJsonType(w *http.ResponseWriter) {
(*w).Header().Set("Content-Type", "application/json")
}
func runMetrics() { func runMetrics() {
hashtagtotal := runTrendingHashtags() hashtagtotal := runTrendingHashtags()
wordstotal := make(map[string]interface{}) wordstotal := make(map[string]interface{})
@ -153,11 +157,14 @@ ORDER BY 2 DESC LIMIT 20;`
// GET handlers // GET handlers
func getTrending(w http.ResponseWriter, r *http.Request) { func getTrending(w http.ResponseWriter, r *http.Request) {
enableCors(&w) enableCors(&w)
setJsonType(&w)
fmt.Fprintf(w, "%s", metricsText) fmt.Fprintf(w, "%s", metricsText)
} }
func getSearch(w http.ResponseWriter, r *http.Request) { func getSearch(w http.ResponseWriter, r *http.Request) {
enableCors(&w) enableCors(&w)
setJsonType(&w)
searchkeys, exists_search := r.URL.Query()["s"] searchkeys, exists_search := r.URL.Query()["s"]
offsetkeys, exists_offset := r.URL.Query()["o"] offsetkeys, exists_offset := r.URL.Query()["o"]
@ -176,15 +183,23 @@ func getSearch(w http.ResponseWriter, r *http.Request) {
if exists_search && searchKey != "" { if exists_search && searchKey != "" {
if offsetKey == -1 { if offsetKey == -1 {
rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document, activities.instance FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) ORDER BY activities.id DESC LIMIT 10", searchKey) queryString := "SELECT activities.id, activities.document, actors.document, activities.instance FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) ORDER BY activities.id DESC LIMIT 10"
log.Println("Running query: \"", queryString, "\" ", searchKey)
rows, err = pool.Query(context.Background(), queryString, searchKey)
} else { } else {
rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document, activities.instance, FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) AND activities.id < $2 ORDER BY activities.id DESC LIMIT 10", searchKey, offsetKey) queryString := "SELECT activities.id, activities.document, actors.document, activities.instance, FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) AND activities.id < $2 ORDER BY activities.id DESC LIMIT 10"
log.Println("Running query: \"", queryString, "\" ", searchKey, offsetKey)
rows, err = pool.Query(context.Background(), queryString, searchKey, offsetKey)
} }
} else { } else {
if offsetKey == -1 { if offsetKey == -1 {
rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document, activities.instance FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' ORDER BY activities.id DESC LIMIT 10") queryString := "SELECT activities.id, activities.document, actors.document, activities.instance FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' ORDER BY activities.id DESC LIMIT 10"
log.Println("Running query: \"", queryString, "\" ", queryString)
rows, err = pool.Query(context.Background(), queryString)
} else { } else {
rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document, activities.instance FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' AND activities.id < $1 ORDER BY activities.id DESC LIMIT 10", offsetKey) queryString := "SELECT activities.id, activities.document, actors.document, activities.instance FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' AND activities.id < $1 ORDER BY activities.id DESC LIMIT 10"
log.Println("Running query: \"", queryString, "\" ", offsetKey)
rows, err = pool.Query(context.Background(), queryString, offsetKey)
} }
} }
@ -246,6 +261,7 @@ func getSearch(w http.ResponseWriter, r *http.Request) {
func getInstanceStats(w http.ResponseWriter, r *http.Request) { func getInstanceStats(w http.ResponseWriter, r *http.Request) {
enableCors(&w) enableCors(&w)
setJsonType(&w)
instanceKeys, exists := r.URL.Query()["i"] instanceKeys, exists := r.URL.Query()["i"]
var instance string var instance string
@ -285,6 +301,7 @@ func getInstanceStats(w http.ResponseWriter, r *http.Request) {
func getActorStats(w http.ResponseWriter, r *http.Request) { func getActorStats(w http.ResponseWriter, r *http.Request) {
enableCors(&w) enableCors(&w)
setJsonType(&w)
actorKeys, exists := r.URL.Query()["a"] actorKeys, exists := r.URL.Query()["a"]
var actor string var actor string
@ -315,6 +332,7 @@ func getActorStats(w http.ResponseWriter, r *http.Request) {
func getGlobalStats(w http.ResponseWriter, r *http.Request) { func getGlobalStats(w http.ResponseWriter, r *http.Request) {
enableCors(&w) enableCors(&w)
setJsonType(&w)
globalstatsjson := &GlobalStatsJson{} globalstatsjson := &GlobalStatsJson{}
globalstatsjson.Timestamp = time.Now() globalstatsjson.Timestamp = time.Now()