diff --git a/restapi/restapi.go b/restapi/restapi.go index 7910b0d..f23499b 100644 --- a/restapi/restapi.go +++ b/restapi/restapi.go @@ -40,6 +40,10 @@ func enableCors(w *http.ResponseWriter) { (*w).Header().Set("Access-Control-Allow-Origin", "*") } +func setJsonType(w *http.ResponseWriter) { + (*w).Header().Set("Content-Type", "application/json") +} + func runMetrics() { hashtagtotal := runTrendingHashtags() wordstotal := make(map[string]interface{}) @@ -153,11 +157,14 @@ ORDER BY 2 DESC LIMIT 20;` // GET handlers func getTrending(w http.ResponseWriter, r *http.Request) { enableCors(&w) + setJsonType(&w) + fmt.Fprintf(w, "%s", metricsText) } func getSearch(w http.ResponseWriter, r *http.Request) { enableCors(&w) + setJsonType(&w) searchkeys, exists_search := r.URL.Query()["s"] offsetkeys, exists_offset := r.URL.Query()["o"] @@ -176,15 +183,23 @@ func getSearch(w http.ResponseWriter, r *http.Request) { if exists_search && searchKey != "" { 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 { - 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 { 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 { - 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) { enableCors(&w) + setJsonType(&w) instanceKeys, exists := r.URL.Query()["i"] var instance string @@ -285,6 +301,7 @@ func getInstanceStats(w http.ResponseWriter, r *http.Request) { func getActorStats(w http.ResponseWriter, r *http.Request) { enableCors(&w) + setJsonType(&w) actorKeys, exists := r.URL.Query()["a"] var actor string @@ -315,6 +332,7 @@ func getActorStats(w http.ResponseWriter, r *http.Request) { func getGlobalStats(w http.ResponseWriter, r *http.Request) { enableCors(&w) + setJsonType(&w) globalstatsjson := &GlobalStatsJson{} globalstatsjson.Timestamp = time.Now()