all repos — honk @ b2331226c4f79b8840cfb3eb55cb508b72dc638e

my fork of honk

masto_util.go (view raw)

 1package main
 2
 3import (
 4	"bytes"
 5	"crypto/rand"
 6	"fmt"
 7	"log"
 8	"net/http"
 9	"time"
10
11	"humungus.tedunangst.com/r/webs/junk"
12)
13
14type NotResponseWriter struct {
15	body   bytes.Buffer
16	header http.Header
17}
18
19func (w NotResponseWriter) Header() http.Header {
20	return w.header
21}
22
23func (w NotResponseWriter) Write(b []byte) (int, error) {
24	log.Println("actual code", string(b))
25	return w.body.Write(b)
26}
27
28func (w NotResponseWriter) WriteHeader(statusCode int) {}
29
30func snowflake() uint64 {
31	ts := time.Now()
32	return uint64(uint64(ts.UnixNano()/int64(time.Millisecond))<<16 | uint64(time.Now().Nanosecond()&0xffff))
33}
34
35func badjunk(rw http.ResponseWriter, j junk.Junk, path string) {
36	elog.Printf("%s: bad junk: %s", path, j.ToString())
37	http.Error(rw, fmt.Sprintf("%s: bad junk", path), http.StatusBadRequest)
38}
39
40func goodjunk(rw http.ResponseWriter, j junk.Junk) {
41	rw.WriteHeader(http.StatusOK)
42	rw.Header().Set("Content-Type", "application/json; charset=utf-8")
43	rw.Write(j.ToBytes())
44}
45
46func tokengen() string {
47	b := make([]byte, 32)
48	rand.Read(b)
49	return fmt.Sprintf("%x", b)
50}