aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2019-02-02 06:53:12 -0800
committerChristine Dodrill <me@christine.website>2019-02-02 06:53:12 -0800
commit7bcfbc6a4da40f498af83e40c3c57ef02ec212cf (patch)
treeee224b7b08722e06d78f49bc557249891264e38f
parentda3595468056f1dd04d79c908f8a65dc0c5a397f (diff)
downloadx-7bcfbc6a4da40f498af83e40c3c57ef02ec212cf.tar.xz
x-7bcfbc6a4da40f498af83e40c3c57ef02ec212cf.zip
web: use error type, simplify some code
-rw-r--r--Dockerfile2
-rw-r--r--discord/ilo-kesi/main.go3
-rw-r--r--discord/ilo-kesi/parse.go10
-rw-r--r--glue/CHEATSHEET.md2
-rw-r--r--glue/glue.go3
-rw-r--r--go.mod4
-rw-r--r--go.sum8
-rw-r--r--idp/translations/en_US.json15
-rw-r--r--idp/translations/jbo_latn.json15
-rw-r--r--web/discordwebhook/webhook.go2
-rw-r--r--web/error.go2
-rw-r--r--web/switchcounter/switchc.go16
-rw-r--r--web/tokipana/tokipana.go14
-rw-r--r--web/tokiponatokens/toki_pona.go14
14 files changed, 80 insertions, 30 deletions
diff --git a/Dockerfile b/Dockerfile
index 3002350..7dca26a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,4 @@
-FROM xena/go:1.11.4 AS build
+FROM xena/go:1.11.5 AS build
WORKDIR /x
COPY . .
ENV GOPROXY=https://cache.greedo.xeserv.us
diff --git a/discord/ilo-kesi/main.go b/discord/ilo-kesi/main.go
index 49d4f2c..ce15aa3 100644
--- a/discord/ilo-kesi/main.go
+++ b/discord/ilo-kesi/main.go
@@ -5,7 +5,6 @@ import (
"fmt"
"log"
"math/rand"
- "net/http"
"os"
"os/signal"
"regexp"
@@ -51,7 +50,7 @@ func main() {
}
cfg.JanLawa = append(cfg.JanLawa, "console")
- sw := switchcounter.NewHTTPClient(http.DefaultClient, cfg.SwitchCounterWebhook)
+ sw := switchcounter.NewHTTPClient(cfg.SwitchCounterWebhook)
line := liner.NewLiner()
defer line.Close()
diff --git a/discord/ilo-kesi/parse.go b/discord/ilo-kesi/parse.go
index f945e39..c54f9c4 100644
--- a/discord/ilo-kesi/parse.go
+++ b/discord/ilo-kesi/parse.go
@@ -53,8 +53,9 @@ func (i ilo) parse(authorID, inp string) (*reply, error) {
if err != nil {
return nil, err
}
- if resp.StatusCode/100 != 2 {
- return nil, errors.New(resp.Status)
+ err = switchcounter.Validate(resp)
+ if err != nil {
+ return nil, err
}
var st switchcounter.Status
err = json.NewDecoder(resp.Body).Decode(&st)
@@ -78,8 +79,9 @@ func (i ilo) parse(authorID, inp string) (*reply, error) {
if err != nil {
return nil, err
}
- if resp.StatusCode/100 != 2 {
- return nil, errors.New(resp.Status)
+ err = switchcounter.Validate(resp)
+ if err != nil {
+ return nil, err
}
fmt.Fprintf(buf, "tenpo ni la jan %s li lawa insa.\n", req.Subject)
diff --git a/glue/CHEATSHEET.md b/glue/CHEATSHEET.md
index 0536567..d03e91c 100644
--- a/glue/CHEATSHEET.md
+++ b/glue/CHEATSHEET.md
@@ -456,8 +456,6 @@ local ssh = require "ssh"
SSH client library
-https://github.com/kohkimakimoto/gluassh/blob/master/gluassh_test.go
-
## `template`
```lua
diff --git a/glue/glue.go b/glue/glue.go
index 0e56406..1b7f34f 100644
--- a/glue/glue.go
+++ b/glue/glue.go
@@ -56,8 +56,7 @@ Available options are:
-p file write cpu profiles to the file
-v show version information
-license show license information
- -manpage generate manpage
-`)
+ -manpage generate manpage`)
}
internal.HandleStartup()
if len(opt_p) != 0 {
diff --git a/go.mod b/go.mod
index 13b0756..eb2813b 100644
--- a/go.mod
+++ b/go.mod
@@ -71,7 +71,7 @@ require (
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc
golang.org/x/exp/errors v0.0.0-20190125153040-c74c464bbbf2
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b // indirect
- golang.org/x/net v0.0.0-20190110200230-915654e7eabc // indirect
+ golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 // indirect
golang.org/x/oauth2 v0.0.0-20190111185915-36a7019397c4
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
@@ -82,5 +82,5 @@ require (
layeh.com/gopher-luar v1.0.4 // indirect
within.website/confyg v0.1.0
within.website/johaus v1.0.0
- within.website/ln v0.4.0
+ within.website/ln v0.5.0
)
diff --git a/go.sum b/go.sum
index 5b2b7b8..c61d03b 100644
--- a/go.sum
+++ b/go.sum
@@ -173,8 +173,8 @@ golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3 h1:dgd4x4kJt7G4k4m93AYLzM8Ni6h2qLTfh9n9vXJT3/0=
golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
-golang.org/x/net v0.0.0-20190110200230-915654e7eabc h1:Yx9JGxI1SBhVLFjpAkWMaO1TF+xyqtHLjZpvQboJGiM=
-golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 h1:ulvT7fqt0yHWzpJwI57MezWnYDVpCAYBVuYst/L+fAY=
+golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190111185915-36a7019397c4 h1:Xi5aaGtyrfSB/gXS4Kal2NNpB7uzffL3yzWi2kByI18=
golang.org/x/oauth2 v0.0.0-20190111185915-36a7019397c4/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -207,5 +207,5 @@ within.website/confyg v0.1.0 h1:2/IKJRAwrhqxTi1WZoKOQ0jl/gJ3wGfi05Z3abEC4tw=
within.website/confyg v0.1.0/go.mod h1:qKxglCn43FmpkxdqnoLAPXs21JY1vgQfeeaZ/ovKtEE=
within.website/johaus v1.0.0 h1:snIOuMhjotkML54BT6dxGqrHeB2iZAuCPcTnqsgbE/E=
within.website/johaus v1.0.0/go.mod h1:0RBs2TucQ3CZQiVoqNj9Wch7mtZY05uvNO14v0lKcM4=
-within.website/ln v0.4.0 h1:Ka7GDv9VO1fphOVBtpi2iKVYmUtO/4RYZtnzo3VPyIw=
-within.website/ln v0.4.0/go.mod h1:ifURKqsCJekcsdUE+hyCdcuhQqQ+9v9DfA++ZqYxZFE=
+within.website/ln v0.5.0 h1:PEJV47WuPl1HYfWVz0Bdr4FGqa4VfBUxmxP9NScl09U=
+within.website/ln v0.5.0/go.mod h1:ifURKqsCJekcsdUE+hyCdcuhQqQ+9v9DfA++ZqYxZFE=
diff --git a/idp/translations/en_US.json b/idp/translations/en_US.json
new file mode 100644
index 0000000..ae7d293
--- /dev/null
+++ b/idp/translations/en_US.json
@@ -0,0 +1,15 @@
+{
+ "errors": {
+ "not_allowed": "Not Allowed",
+ "forbidden": "Forbidden",
+ "url_error": "URL Error"
+ },
+
+ "prose": {
+ "index": "This is a private identity provider supporting IndieAuth for Christine Dodrill. Unauthorized access is forbidden.",
+ "auth": "Log in to {0} as {1}"
+ },
+
+ "auth_title": "Auth",
+ "submit_button": "Submit"
+}
diff --git a/idp/translations/jbo_latn.json b/idp/translations/jbo_latn.json
new file mode 100644
index 0000000..3356371
--- /dev/null
+++ b/idp/translations/jbo_latn.json
@@ -0,0 +1,15 @@
+{
+ "errors": {
+ "not_allowed": "na e'akne",
+ "forbidden": "narplicru",
+ "url_error": "na jimpe lo urli"
+ },
+
+ "prose": {
+ "index": "ni'o ti samse'u fi lo nu e'ande po la kristin.dadril .i lo narplicru pilno cu na e'akne",
+ "auth": ".i tolna'e fa zoi .uy. {1} .uy. be zoi .uy. {0} .uy."
+ },
+
+ "auth_title": "dubju'o",
+ "submit_button": "dudydatni"
+}
diff --git a/web/discordwebhook/webhook.go b/web/discordwebhook/webhook.go
index 6cbe6d1..91df381 100644
--- a/web/discordwebhook/webhook.go
+++ b/web/discordwebhook/webhook.go
@@ -59,7 +59,7 @@ func Send(whurl string, w Webhook) *http.Request {
// Validate validates the response from Discord.
func Validate(resp *http.Response) error {
- if resp.StatusCode/100 != 2 {
+ if resp.StatusCode != http.StatusOK {
return web.NewError(http.StatusOK, resp)
}
diff --git a/web/error.go b/web/error.go
index 051d61d..d5ff31a 100644
--- a/web/error.go
+++ b/web/error.go
@@ -13,6 +13,8 @@ import (
// NewError creates an Error based on an expected HTTP status code vs data populated
// from an HTTP response.
+//
+// This consumes the body of the HTTP response.
func NewError(wantStatusCode int, resp *http.Response) error {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
diff --git a/web/switchcounter/switchc.go b/web/switchcounter/switchc.go
index b74eb42..f40929f 100644
--- a/web/switchcounter/switchc.go
+++ b/web/switchcounter/switchc.go
@@ -6,6 +6,8 @@ import (
"encoding/json"
"net/http"
"time"
+
+ "github.com/Xe/x/web"
)
type arg struct {
@@ -19,6 +21,16 @@ type Status struct {
StartedAt time.Time `json:"started_at"`
}
+// Validate ensures a HTTP response contains the expected fields.
+func Validate(resp *http.Response) error {
+ if resp.StatusCode == http.StatusOK {
+ return nil
+ }
+
+ return web.NewError(http.StatusOK, resp)
+}
+
+// API is a builder for HTTP requests to interface with Switch Counter.
type API struct {
url string // webhook url
}
@@ -44,6 +56,7 @@ func (a API) makeRequestWith(body interface{}) (*http.Request, error) {
return req, nil
}
+// Status returns a request for which systemmate is currently in front.
func (a API) Status() *http.Request {
result, err := a.makeRequestWith(arg{Command: "switch"})
if err != nil {
@@ -52,6 +65,7 @@ func (a API) Status() *http.Request {
return result
}
+// Switch changes the recorded front to the given systemmate.
func (a API) Switch(front string) *http.Request {
result, err := a.makeRequestWith(arg{Command: "switch", MemberName: front})
if err != nil {
@@ -61,7 +75,7 @@ func (a API) Switch(front string) *http.Request {
}
// NewHTTPClient creates a new instance of API over HTTP.
-func NewHTTPClient(a *http.Client, webhookURL string) API {
+func NewHTTPClient(webhookURL string) API {
return API{
url: webhookURL,
}
diff --git a/web/tokipana/tokipana.go b/web/tokipana/tokipana.go
index 2daa54b..b5871ee 100644
--- a/web/tokipana/tokipana.go
+++ b/web/tokipana/tokipana.go
@@ -2,30 +2,22 @@
package tokipana
import (
- "errors"
"net/http"
"net/url"
+
+ "github.com/Xe/x/web"
)
// The API URL.
const APIURL = `http://inamidst.com/services/tokipana`
-// Errors
-var (
- ErrInvalidRequest = errors.New("tokipana: invalid request")
-)
-
// Validate checks if a response from the API is valid or not.
func Validate(resp *http.Response) error {
if resp.StatusCode == http.StatusOK {
return nil
}
- if resp.StatusCode%100 != 2 {
- return ErrInvalidRequest
- }
-
- return nil
+ return web.NewError(http.StatusOK, resp)
}
// Translate returns a request to translate the given toki pona text into english.
diff --git a/web/tokiponatokens/toki_pona.go b/web/tokiponatokens/toki_pona.go
index f1079cb..c3e3fe3 100644
--- a/web/tokiponatokens/toki_pona.go
+++ b/web/tokiponatokens/toki_pona.go
@@ -5,6 +5,8 @@ import (
"encoding/json"
"net/http"
"strings"
+
+ "github.com/Xe/x/web"
)
// Part is an individual part of a sentence.
@@ -90,6 +92,14 @@ const (
// Sentence is a series of sentence parts. This correlates to one Toki Pona sentence.
type Sentence []Part
+func validate(resp *http.Response) error {
+ if resp.StatusCode != http.StatusOK {
+ return web.NewError(http.StatusOK, resp)
+ }
+
+ return nil
+}
+
// Tokenize returns a series of toki pona tokens.
func Tokenize(aurl, text string) ([]Sentence, error) {
buf := bytes.NewBuffer([]byte(text))
@@ -105,6 +115,10 @@ func Tokenize(aurl, text string) ([]Sentence, error) {
return nil, err
}
defer resp.Body.Close()
+ err = validate(resp)
+ if err != nil {
+ return nil, err
+ }
var result []Sentence
err = json.NewDecoder(resp.Body).Decode(&result)