Skip to content

Commit 18dd18f

Browse files
committed
Add several dependencies needed for API routes
1 parent 4dc6e35 commit 18dd18f

28 files changed

+2594
-0
lines changed

buildmode/buildmode.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package buildmode
2+
3+
import (
4+
"flag"
5+
"strings"
6+
)
7+
8+
// BuildMode is injected at build time.
9+
var (
10+
BuildMode string
11+
)
12+
13+
// Dev returns true when built to run in a dev deployment.
14+
func Dev() bool {
15+
return strings.HasPrefix(BuildMode, "dev")
16+
}
17+
18+
// Test returns true when running inside a unit test.
19+
func Test() bool {
20+
return flag.Lookup("test.v") != nil
21+
}

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/alecthomas/chroma v0.9.1 // indirect
4343
github.com/apparentlymart/go-textseg v1.0.0 // indirect
4444
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
45+
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
4546
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
4647
github.com/containerd/continuity v0.1.0 // indirect
4748
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
@@ -53,6 +54,9 @@ require (
5354
github.com/docker/go-connections v0.4.0 // indirect
5455
github.com/docker/go-units v0.4.0 // indirect
5556
github.com/fatih/color v1.13.0 // indirect
57+
github.com/go-playground/locales v0.14.0 // indirect
58+
github.com/go-playground/universal-translator v0.18.0 // indirect
59+
github.com/go-playground/validator/v10 v10.10.0 // indirect
5660
github.com/gogo/protobuf v1.3.2 // indirect
5761
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5862
github.com/google/go-cmp v0.5.6 // indirect
@@ -66,6 +70,7 @@ require (
6670
github.com/imdario/mergo v0.3.12 // indirect
6771
github.com/inconshreveable/mousetrap v1.0.0 // indirect
6872
github.com/justinas/nosurf v1.1.1 // indirect
73+
github.com/leodido/go-urn v1.2.1 // indirect
6974
github.com/mattn/go-colorable v0.1.12 // indirect
7075
github.com/mattn/go-isatty v0.0.14 // indirect
7176
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
@@ -107,4 +112,5 @@ require (
107112
google.golang.org/grpc v1.43.0 // indirect
108113
gopkg.in/yaml.v2 v2.4.0 // indirect
109114
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
115+
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 // indirect
110116
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
151151
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
152152
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
153153
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
154+
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
155+
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
154156
github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
155157
github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3ATZkfNZeM=
156158
github.com/aws/aws-sdk-go v1.17.7 h1:/4+rDPe0W95KBmNGYCG+NUvdL8ssPYBMxL+aSCg6nIA=
@@ -468,6 +470,13 @@ github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL9
468470
github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
469471
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
470472
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
473+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
474+
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
475+
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
476+
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
477+
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
478+
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
479+
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
471480
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
472481
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
473482
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@@ -804,6 +813,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
804813
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
805814
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
806815
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
816+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
817+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
807818
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
808819
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
809820
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
@@ -818,6 +829,8 @@ github.com/kylecarbs/terraform-config-inspect v0.0.0-20211215004401-bbc517866b88
818829
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
819830
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
820831
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
832+
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
833+
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
821834
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
822835
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
823836
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@@ -1020,6 +1033,7 @@ github.com/pion/webrtc/v3 v3.1.13 h1:2XxgGstOqt03ba8QD5+m9S8DCA3Ez53mULT4If8onOg
10201033
github.com/pion/webrtc/v3 v3.1.13/go.mod h1:RACpyE1EDYlzonfbdPvXkIGDaqD8+NsHqZJN0yEbRbA=
10211034
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
10221035
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
1036+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
10231037
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10241038
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10251039
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -1069,6 +1083,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
10691083
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
10701084
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
10711085
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
1086+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
1087+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
10721088
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
10731089
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
10741090
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
@@ -1265,6 +1281,7 @@ golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPh
12651281
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
12661282
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
12671283
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
1284+
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
12681285
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
12691286
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
12701287
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
@@ -1889,6 +1906,8 @@ k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
18891906
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
18901907
k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
18911908
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
1909+
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 h1:ZKMMxTvduyf5WUtREOqg5LiXaN1KO/+0oOQPRFrClpo=
1910+
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
18921911
modernc.org/b v1.0.0/go.mod h1:uZWcZfRj1BpYzfN9JTerzlNUnnPsV9O2ZA8JsRcubNg=
18931912
modernc.org/cc/v3 v3.32.4/go.mod h1:0R6jl1aZlIl2avnYfbfHBS1QB6/f+16mihBObaBC878=
18941913
modernc.org/ccgo/v3 v3.9.2/go.mod h1:gnJpy6NIVqkETT+L5zPsQFj7L2kkhfPMzOghRNv/CFo=

longid/id.go

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
package longid
2+
3+
import (
4+
"bytes"
5+
"database/sql"
6+
"database/sql/driver"
7+
"encoding/binary"
8+
"encoding/hex"
9+
"fmt"
10+
"hash/fnv"
11+
"math/rand"
12+
"os"
13+
"sync/atomic"
14+
"time"
15+
16+
"golang.org/x/xerrors"
17+
)
18+
19+
// bit counts.
20+
const (
21+
TimeBits = 32
22+
IncrementorBits = 10
23+
24+
RandomBits = 70
25+
HostIDBits = 8
26+
27+
// amount of random bits in each uint64
28+
RandomBits1 = 64 - (TimeBits + IncrementorBits)
29+
RandomBits2 = RandomBits - RandomBits1
30+
31+
HostMask = 0x00000000000000FF
32+
)
33+
34+
var (
35+
inc uint32
36+
hostID int64
37+
)
38+
39+
func init() {
40+
rand.Seed(time.Now().UnixNano())
41+
42+
hostname, err := os.Hostname()
43+
if err != nil {
44+
panic(err)
45+
}
46+
hash := fnv.New64a()
47+
_, _ = hash.Write([]byte(hostname))
48+
hostID = (int64(hash.Sum64())) & HostMask
49+
inc = rand.Uint32()
50+
}
51+
52+
// HostID returns the host ID for the current machine.
53+
func HostID() int64 {
54+
return hostID
55+
}
56+
57+
// ID describes a 128 bit ID
58+
type ID [16]byte
59+
60+
// parse errors
61+
var (
62+
ErrWrongSize = xerrors.New("id in string form should be exactly 33 bytes")
63+
)
64+
65+
// FromSlice converts a slice into an ID.
66+
func FromSlice(b []byte) ID {
67+
var l ID
68+
copy(l[:], b)
69+
return l
70+
}
71+
72+
func part1() int64 {
73+
seconds := time.Now().Unix()
74+
75+
// place time portion properly
76+
time := seconds << (64 - TimeBits)
77+
78+
i := atomic.AddUint32(&inc, 1)
79+
80+
// reset incrementor if it's too big
81+
atomic.CompareAndSwapUint32(&inc, ((1 << IncrementorBits) - 1), 0)
82+
83+
i <<= (RandomBits1)
84+
85+
var randBuf [4]byte
86+
_, _ = rand.Read(randBuf[:])
87+
88+
rand := (binary.BigEndian.Uint32(randBuf[:]) >> (32 - RandomBits1))
89+
90+
return time + int64(i) + int64(rand)
91+
}
92+
93+
func part2() int64 {
94+
var randBuf [8]byte
95+
_, _ = rand.Read(randBuf[:])
96+
rand := binary.BigEndian.Uint64(randBuf[:]) << HostIDBits
97+
// fmt.Printf("%x\n", rand)
98+
99+
return int64(rand) + hostID
100+
}
101+
102+
// New generates a long ID.
103+
func New() ID {
104+
var id ID
105+
binary.BigEndian.PutUint64(id[:8], uint64(part1()))
106+
binary.BigEndian.PutUint64(id[8:], uint64(part2()))
107+
return id
108+
}
109+
110+
// Bytes returns a byte slice from l.
111+
func (l ID) Bytes() []byte {
112+
return l[:]
113+
}
114+
115+
// CreatedAt returns the time the ID was created at.
116+
func (l ID) CreatedAt() time.Time {
117+
epoch := (time.Now().Unix() >> (TimeBits)) << (TimeBits)
118+
119+
ts := binary.BigEndian.Uint64(l[:8]) >> (64 - TimeBits)
120+
121+
// fmt.Printf("%064b\n", epoch)
122+
// fmt.Printf("%064b\n", ts)
123+
124+
return time.Unix(epoch+int64(ts), 0)
125+
}
126+
127+
// String returns the text representation of l
128+
func (l ID) String() string {
129+
return fmt.Sprintf("%08x-%024x", l[:4], l[4:])
130+
}
131+
132+
// MarshalText marshals l
133+
func (l ID) MarshalText() ([]byte, error) {
134+
return []byte(l.String()), nil
135+
}
136+
137+
// UnmarshalText parses b
138+
func (l *ID) UnmarshalText(b []byte) error {
139+
ll, err := Parse(string(b))
140+
if err != nil {
141+
return err
142+
}
143+
copy(l[:], ll[:])
144+
return nil
145+
}
146+
147+
// MarshalJSON marshals l
148+
func (l ID) MarshalJSON() ([]byte, error) {
149+
return []byte("\"" + l.String() + "\""), nil
150+
}
151+
152+
// UnmarshalJSON parses b
153+
func (l *ID) UnmarshalJSON(b []byte) error {
154+
return l.UnmarshalText(bytes.Trim(b, "\""))
155+
}
156+
157+
var _ = driver.Valuer(New())
158+
var _ = sql.Scanner(&ID{})
159+
160+
func (l ID) Value() (driver.Value, error) {
161+
return l.Bytes(), nil
162+
}
163+
164+
func (l *ID) Scan(v interface{}) error {
165+
b, ok := v.([]byte)
166+
if !ok {
167+
return xerrors.New("can only scan binary types")
168+
}
169+
if len(b) != 16 {
170+
return xerrors.New("must be 16 bytes")
171+
}
172+
copy(l[:], b)
173+
return nil
174+
}
175+
176+
// Parse parses the String() representation of a Long
177+
func Parse(l string) (ID, error) {
178+
var (
179+
id ID
180+
err error
181+
)
182+
if len(l) != 33 {
183+
return id, ErrWrongSize
184+
}
185+
186+
p1, err := hex.DecodeString(l[:8])
187+
if err != nil {
188+
return id, xerrors.Errorf("failed to decode short portion: %w", err)
189+
}
190+
191+
p2, err := hex.DecodeString(l[9:])
192+
if err != nil {
193+
return id, xerrors.Errorf("failed to decode rand portion: %w", err)
194+
}
195+
196+
copy(id[:4], p1)
197+
copy(id[4:], p2)
198+
199+
return id, nil
200+
}
201+
202+
// TimeReset the current bounds of
203+
// validity for timestamps extracted from longs
204+
func TimeReset() (last time.Time, next time.Time) {
205+
const lastStr = "00000000-00680e087d8fff20a11d24e6"
206+
const nextStr = "ffffffff-00680e087d8fff20a11d24e6"
207+
l, _ := Parse(lastStr)
208+
last = l.CreatedAt()
209+
210+
l, _ = Parse(nextStr)
211+
next = l.CreatedAt()
212+
213+
return
214+
}

0 commit comments

Comments
 (0)