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