mirror of
https://github.com/believethehype/nostdress.git
synced 2026-06-15 13:38:06 -07:00
update import
updated ioutils replaced encoding/json with jsoniter refactored some code
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
@@ -46,7 +46,7 @@ func ClaimAddress(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: middleware for responses that adds this header
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
jsoniter.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -69,7 +69,7 @@ func GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
jsoniter.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -103,7 +103,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
jsoniter.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -122,7 +122,7 @@ func DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
jsoniter.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// authentication middleware
|
||||
@@ -174,15 +174,15 @@ func authenticate(next http.Handler) http.Handler {
|
||||
|
||||
// helpers
|
||||
func sendError(w http.ResponseWriter, code int, msg string, args ...interface{}) {
|
||||
b, _ := json.Marshal(Response{false, fmt.Sprintf(msg, args...), nil})
|
||||
b, _ := jsoniter.Marshal(Response{false, fmt.Sprintf(msg, args...), nil})
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
func parseParams(r *http.Request) *Params {
|
||||
reqBody, _ := ioutil.ReadAll(r.Body)
|
||||
reqBody, _ := io.ReadAll(r.Body)
|
||||
var params Params
|
||||
json.Unmarshal(reqBody, ¶ms)
|
||||
jsoniter.Unmarshal(reqBody, ¶ms)
|
||||
return ¶ms
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/cockroachdb/pebble"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type Params struct {
|
||||
@@ -77,7 +77,7 @@ func SaveName(
|
||||
}
|
||||
|
||||
// save it
|
||||
data, _ := json.Marshal(params)
|
||||
data, _ := jsoniter.Marshal(params)
|
||||
if err := db.Set(key, data, pebble.Sync); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func GetName(name, domain string) (*Params, error) {
|
||||
defer closer.Close()
|
||||
|
||||
var params Params
|
||||
if err := json.Unmarshal(val, ¶ms); err != nil {
|
||||
if err := jsoniter.Unmarshal(val, ¶ms); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ func GetAllUsers(domain string) ([]Params, error) {
|
||||
defer closer.Close()
|
||||
|
||||
var params Params
|
||||
if err := json.Unmarshal(val, ¶ms); err != nil {
|
||||
if err := jsoniter.Unmarshal(val, ¶ms); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
params.Domain = domain
|
||||
@@ -178,7 +178,7 @@ func tryMigrate(old, new string) {
|
||||
for iter.First(); iter.Valid(); iter.Next() {
|
||||
log.Debug().Str("key", string(iter.Key())).Msg("Migrating key")
|
||||
var params Params
|
||||
if err := json.Unmarshal(iter.Value(), ¶ms); err != nil {
|
||||
if err := jsoniter.Unmarshal(iter.Value(), ¶ms); err != nil {
|
||||
log.Debug().Err(err).Msg("Unmarshal error")
|
||||
continue
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func tryMigrate(old, new string) {
|
||||
params.Domain = old // old database name was domain
|
||||
|
||||
// save it
|
||||
data, err := json.Marshal(params)
|
||||
data, err := jsoniter.Marshal(params)
|
||||
if err != nil {
|
||||
log.Debug().Err(err).Msg("Marshal error")
|
||||
continue
|
||||
|
||||
@@ -17,6 +17,11 @@ require (
|
||||
github.com/tidwall/gjson v1.14.4
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/DataDog/zstd v1.5.2 // indirect
|
||||
github.com/SaveTheRbtz/generic-sync-map-go v0.0.0-20230201052002-6c5833b989be // indirect
|
||||
@@ -55,6 +60,7 @@ require (
|
||||
github.com/imroc/req v0.3.2 // indirect
|
||||
github.com/jb55/lnsocket/go v0.0.0-20220812055138-93307d1bfe4c
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/kkdai/bstream v1.0.0 // indirect
|
||||
github.com/klauspost/compress v1.16.4 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
|
||||
@@ -464,6 +464,7 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
|
||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
@@ -649,6 +650,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
|
||||
+24
-14
@@ -10,7 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -51,7 +51,9 @@ type CommandoParams struct {
|
||||
}
|
||||
|
||||
func (l CommandoParams) getCert() string { return "" }
|
||||
func (l CommandoParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
|
||||
func (l CommandoParams) isTor() bool {
|
||||
return strings.Contains(l.Host, ".onion")
|
||||
}
|
||||
|
||||
type SparkoParams struct {
|
||||
Cert string
|
||||
@@ -60,7 +62,9 @@ type SparkoParams struct {
|
||||
}
|
||||
|
||||
func (l SparkoParams) getCert() string { return l.Cert }
|
||||
func (l SparkoParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
|
||||
func (l SparkoParams) isTor() bool {
|
||||
return strings.Contains(l.Host, ".onion")
|
||||
}
|
||||
|
||||
type LNDParams struct {
|
||||
Cert string
|
||||
@@ -69,7 +73,9 @@ type LNDParams struct {
|
||||
}
|
||||
|
||||
func (l LNDParams) getCert() string { return l.Cert }
|
||||
func (l LNDParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
|
||||
func (l LNDParams) isTor() bool {
|
||||
return strings.Contains(l.Host, ".onion")
|
||||
}
|
||||
|
||||
type LNBitsParams struct {
|
||||
Cert string
|
||||
@@ -78,7 +84,9 @@ type LNBitsParams struct {
|
||||
}
|
||||
|
||||
func (l LNBitsParams) getCert() string { return l.Cert }
|
||||
func (l LNBitsParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
|
||||
func (l LNBitsParams) isTor() bool {
|
||||
return strings.Contains(l.Host, ".onion")
|
||||
}
|
||||
|
||||
type LNPayParams struct {
|
||||
PublicAccessKey string
|
||||
@@ -95,7 +103,9 @@ type EclairParams struct {
|
||||
}
|
||||
|
||||
func (l EclairParams) getCert() string { return l.Cert }
|
||||
func (l EclairParams) isTor() bool { return strings.Index(l.Host, ".onion") != -1 }
|
||||
func (l EclairParams) isTor() bool {
|
||||
return strings.Contains(l.Host, ".onion")
|
||||
}
|
||||
|
||||
type StrikeParams struct {
|
||||
Key string
|
||||
@@ -203,7 +213,7 @@ func MakeInvoice(params LNParams) (bolt11 string, err error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 300 {
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
text := string(body)
|
||||
if len(text) > 300 {
|
||||
text = text[:300]
|
||||
@@ -211,7 +221,7 @@ func MakeInvoice(params LNParams) (bolt11 string, err error) {
|
||||
return "", fmt.Errorf("call to lnd failed (%d): %s", resp.StatusCode, text)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -251,7 +261,7 @@ func MakeInvoice(params LNParams) (bolt11 string, err error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 300 {
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
text := string(body)
|
||||
if len(text) > 300 {
|
||||
text = text[:300]
|
||||
@@ -260,7 +270,7 @@ func MakeInvoice(params LNParams) (bolt11 string, err error) {
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -336,7 +346,7 @@ func MakeInvoice(params LNParams) (bolt11 string, err error) {
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -360,7 +370,7 @@ func MakeInvoice(params LNParams) (bolt11 string, err error) {
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err = ioutil.ReadAll(res.Body)
|
||||
body, err = io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -406,12 +416,12 @@ func MakeInvoice(params LNParams) (bolt11 string, err error) {
|
||||
} else if resErr.Type == gjson.String {
|
||||
return "", errors.New(resErr.String())
|
||||
}
|
||||
return "", fmt.Errorf("Unknown commando error: '%v'", resErr)
|
||||
return "", fmt.Errorf("unknown commando error: '%v'", resErr)
|
||||
}
|
||||
|
||||
invoice := gjson.Get(body, "result.bolt11")
|
||||
if invoice.Type != gjson.String {
|
||||
return "", fmt.Errorf("No bolt11 result found in invoice response, got %v", body)
|
||||
return "", fmt.Errorf("no bolt11 result found in invoice response, got %v", body)
|
||||
}
|
||||
|
||||
return invoice.String(), nil
|
||||
|
||||
Reference in New Issue
Block a user