about summary refs log tree commit diff stats
path: root/subx/apps/assort.subx
Commit message (Expand)AuthorAgeFilesLines
* 5115Kartik Agaram2019-04-221-1/+1
* 5106Kartik Agaram2019-04-171-1/+2
* 5105Kartik Agaram2019-04-161-188/+0
* 5101Kartik Agaram2019-04-161-1/+1
* 5097Kartik Agaram2019-04-161-4/+5
* 5092Kartik Agaram2019-04-151-0/+245
* 5090Kartik Agaram2019-04-131-180/+36
* 5087Kartik Agaram2019-04-121-1/+2
* 5085 - 'assort' phase done!Kartik Agaram2019-04-121-407/+497
* 5080Kartik Agaram2019-04-111-37/+37
* 5063Kartik Agaram2019-04-071-69/+0
* 5062Kartik Agaram2019-04-061-391/+460
* 5061Kartik Agaram2019-04-061-1/+1
* 5060Kartik Agaram2019-04-061-10/+221
* 5059Kartik Agaram2019-04-051-1/+1
* 5055 - new phase: merge fragment of segmentsKartik Agaram2019-04-041-0/+1102
font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
package main

import (
	"log"
	"net/http"
	"strings"

	"github.com/gorilla/mux"
)

func apiErrCheck(err error, r *http.Request) {
	if err != nil {
		uip := getIPFromCtx(r.Context())
		log.Printf("%v :: %v %v :: %v\n", uip, r.Method, r.URL, err)
	}
}

func apiErrCheck500(err error, w http.ResponseWriter, r *http.Request) {
	if err != nil {
		uip := getIPFromCtx(r.Context())
		log.Printf("%v :: %v %v :: %v\n", uip, r.Method, r.URL, err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// apiUserQuery is called via apiEndpointHandler when
// the endpoint is "users" and r.FormValue("q") is not empty.
// It queries the registry cache for users or user URLs
// matching the term supplied via r.FormValue("q")
func apiEndpointQuery(w http.ResponseWriter, r *http.Request) {
	query := r.FormValue("q")
	urls := r.FormValue("url")
	var out []string
	var out2 []string
	var err error

	vars := mux.Vars(r)
	endpoint := vars["endpoint"]

	// Handle user URL queries first, then nickname queries.
	// Concatenate both outputs if they're both set.
	// Also handle mention queries and status queries.
	// If we made it this far and 'default' is matched,
	// something went very wrong.
	switch endpoint {
	case "users":
		if urls != "" {
			out2, err = twtxtCache.QueryUser(urls)
			out = append(out, out2...)
			apiErrCheck(err, r)
		}
		if query != "" {
			out2, err = twtxtCache.QueryUser(query)
			out = append(out, out2...)
			apiErrCheck(err, r)
		}

	case "mentions":
		out, err = twtxtCache.QueryInStatus(query)
		apiErrCheck(err, r)

	case "tweets":
		out, err = twtxtCache.QueryInStatus(query)
		apiErrCheck(err, r)

	default:
		http.Error(w, "500", http.StatusInternalServerError)
	}

	// iterate over the output. if there aren't
	// explicit newlines, add them.
	var data []byte
	for _, e := range out {
		data = append(data, []byte(e)...)
		if !strings.HasSuffix(e, "\n") {
			data = append(data, byte('\n'))
		}
	}

	w.Header().Set("Content-Type", txtutf8)
	_, err = w.Write(data)
	apiErrCheck500(err, w, r)
}