Index page with templ
This commit is contained in:
parent
975072145e
commit
3d3ae16617
|
@ -7,3 +7,6 @@ delphi.market
|
|||
# required for hot reload
|
||||
public/hotreload
|
||||
*.log
|
||||
|
||||
# templ
|
||||
*_templ.go
|
2
Makefile
2
Makefile
|
@ -5,9 +5,11 @@ SOURCE := $(shell find db env lib lnd pages public server -type f) main.go
|
|||
build: delphi.market
|
||||
|
||||
delphi.market: $(SOURCE)
|
||||
templ generate -path server/router/pages
|
||||
go build -o delphi.market .
|
||||
|
||||
run:
|
||||
templ generate -path server/router/pages
|
||||
go run .
|
||||
|
||||
test:
|
||||
|
|
51
go.mod
51
go.mod
|
@ -1,12 +1,17 @@
|
|||
module git.ekzyis.com/ekzyis/delphi.market
|
||||
|
||||
go 1.20
|
||||
go 1.21
|
||||
|
||||
toolchain go1.22.4
|
||||
|
||||
replace git.schwanenlied.me/yawning/bsaes.git v0.0.0-20180720073208-c0276d75487e => gitlab.com/yawning/bsaes.git v0.0.0-20180720073208-c0276d75487e
|
||||
|
||||
require (
|
||||
github.com/a-h/templ v0.2.747
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.2
|
||||
github.com/btcsuite/btcd/btcutil v1.1.3
|
||||
github.com/btcsuite/btcutil v1.0.2
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/labstack/echo/v4 v4.11.1
|
||||
github.com/lib/pq v1.10.9
|
||||
|
@ -14,22 +19,19 @@ require (
|
|||
github.com/lightningnetwork/lnd v0.16.0-beta
|
||||
github.com/namsral/flag v1.7.4-pre
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
github.com/stretchr/testify v1.8.1
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
|
||||
github.com/stretchr/testify v1.8.4
|
||||
gopkg.in/guregu/null.v4 v4.0.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
|
||||
github.com/aead/siphash v1.0.1 // indirect
|
||||
github.com/andybalholm/brotli v1.0.3 // indirect
|
||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/btcsuite/btcd v0.23.5-0.20230125025938-be056b0a0b2f // indirect
|
||||
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
|
||||
github.com/btcsuite/btcd/btcutil/psbt v1.1.5 // indirect
|
||||
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
|
||||
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
|
||||
github.com/btcsuite/btcutil/psbt v1.0.2 // indirect
|
||||
github.com/btcsuite/btcwallet v0.16.7 // indirect
|
||||
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.2 // indirect
|
||||
github.com/btcsuite/btcwallet/wallet/txrules v1.2.0 // indirect
|
||||
|
@ -39,24 +41,16 @@ require (
|
|||
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
|
||||
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
|
||||
github.com/btcsuite/winsvc v1.0.0 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.1 // indirect
|
||||
github.com/coreos/bbolt v1.3.3 // indirect
|
||||
github.com/coreos/etcd v3.3.22+incompatible // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
|
||||
github.com/decred/dcrd/lru v1.0.0 // indirect
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/dsnet/compress v0.0.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.0 // indirect
|
||||
github.com/dvyukov/go-fuzz v0.0.0-20210602112143-b1f3d6f4ef4e // indirect
|
||||
github.com/fergusstrange/embedded-postgres v1.10.0 // indirect
|
||||
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
|
||||
github.com/go-errors/errors v1.0.1 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||
|
@ -87,7 +81,6 @@ require (
|
|||
github.com/kkdai/bstream v1.0.0 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/klauspost/pgzip v1.2.5 // indirect
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
|
||||
github.com/labstack/gommon v0.4.0 // indirect
|
||||
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf // indirect
|
||||
github.com/lightninglabs/neutrino v0.15.0 // indirect
|
||||
|
@ -102,7 +95,7 @@ require (
|
|||
github.com/lightningnetwork/lnd/tor v1.1.0 // indirect
|
||||
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||
github.com/mholt/archiver/v3 v3.5.0 // indirect
|
||||
github.com/miekg/dns v1.1.43 // indirect
|
||||
|
@ -126,6 +119,7 @@ require (
|
|||
github.com/ulikunitz/xz v0.5.10 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
|
||||
go.etcd.io/bbolt v1.3.6 // indirect
|
||||
|
@ -136,27 +130,22 @@ require (
|
|||
go.etcd.io/etcd/pkg/v3 v3.5.7 // indirect
|
||||
go.etcd.io/etcd/raft/v3 v3.5.7 // indirect
|
||||
go.etcd.io/etcd/server/v3 v3.5.7 // indirect
|
||||
go.opentelemetry.io/contrib v0.20.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0 // indirect
|
||||
go.opentelemetry.io/otel v1.0.1 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp v0.20.0 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.1 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.1 // indirect
|
||||
go.opentelemetry.io/otel/metric v0.20.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk v1.0.1 // indirect
|
||||
go.opentelemetry.io/otel/sdk/export/metric v0.20.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk/metric v0.20.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.0.1 // indirect
|
||||
go.opentelemetry.io/proto/otlp v0.9.0 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
go.uber.org/zap v1.17.0 // indirect
|
||||
golang.org/x/crypto v0.13.0 // indirect
|
||||
golang.org/x/mod v0.12.0 // indirect
|
||||
golang.org/x/net v0.15.0 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
golang.org/x/term v0.12.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.uber.org/zap v1.27.0 // indirect
|
||||
golang.org/x/crypto v0.22.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/mod v0.17.0 // indirect
|
||||
golang.org/x/net v0.24.0 // indirect
|
||||
golang.org/x/sys v0.21.0 // indirect
|
||||
golang.org/x/term v0.19.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/time v0.3.0 // indirect
|
||||
golang.org/x/tools v0.13.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced // indirect
|
||||
|
|
5
main.go
5
main.go
|
@ -27,7 +27,7 @@ func init() {
|
|||
lndNetwork string
|
||||
db_ *db.DB
|
||||
lnd_ *lnd.LNDClient
|
||||
ctx router.ServerContext
|
||||
ctx router.Context
|
||||
err error
|
||||
)
|
||||
|
||||
|
@ -64,7 +64,8 @@ func init() {
|
|||
lnd_.CheckInvoices(db_)
|
||||
}
|
||||
|
||||
ctx = server.ServerContext{
|
||||
ctx = server.Context{
|
||||
Environment: env.Env,
|
||||
PublicURL: env.PublicURL,
|
||||
CommitShortSha: env.CommitShortSha,
|
||||
CommitLongSha: env.CommitLongSha,
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lnd"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type ServerContext struct {
|
||||
type Context struct {
|
||||
context.Context
|
||||
Environment string
|
||||
PublicURL string
|
||||
CommitShortSha string
|
||||
|
@ -16,19 +19,18 @@ type ServerContext struct {
|
|||
Lnd *lnd.LNDClient
|
||||
}
|
||||
|
||||
func (sc *ServerContext) Render(c echo.Context, code int, name string, data map[string]any) error {
|
||||
envVars := map[string]any{
|
||||
"PUBLIC_URL": sc.PublicURL,
|
||||
"COMMIT_SHORT_SHA": sc.CommitShortSha,
|
||||
"COMMIT_LONG_SHA": sc.CommitLongSha,
|
||||
"VERSION": sc.Version,
|
||||
}
|
||||
merge(&data, &envVars)
|
||||
return c.Render(code, name, data)
|
||||
}
|
||||
type RenderContextKey string
|
||||
|
||||
func merge[T comparable](target *map[T]any, src *map[T]any) {
|
||||
for k, v := range *src {
|
||||
(*target)[k] = v
|
||||
}
|
||||
var (
|
||||
EnvContextKey RenderContextKey = "env"
|
||||
SessionContextKey RenderContextKey = "session"
|
||||
CommitContextKey RenderContextKey = "commit"
|
||||
)
|
||||
|
||||
func RenderContext(sc Context, c echo.Context) context.Context {
|
||||
ctx := c.Request().Context()
|
||||
ctx = context.WithValue(ctx, EnvContextKey, sc.Environment)
|
||||
ctx = context.WithValue(ctx, SessionContextKey, c.Get("session"))
|
||||
ctx = context.WithValue(ctx, CommitContextKey, sc.CommitShortSha)
|
||||
return ctx
|
||||
}
|
||||
|
|
|
@ -5,29 +5,17 @@ import (
|
|||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleIndex(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleIndex(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
markets []db.Market
|
||||
err error
|
||||
data map[string]any
|
||||
)
|
||||
if err = sc.Db.FetchActiveMarkets(&markets); err != nil {
|
||||
return err
|
||||
}
|
||||
data = map[string]any{
|
||||
"session": c.Get("session"),
|
||||
"markets": markets,
|
||||
}
|
||||
|
||||
return sc.Render(c, http.StatusOK, "index.html", data)
|
||||
return pages.Index().Render(context.RenderContext(sc, c), c.Response().Writer)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleMarkets(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleMarkets(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
markets []db.Market
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
)
|
||||
|
||||
func HandleInvoiceStatus(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleInvoiceStatus(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
invoiceId string
|
||||
|
@ -51,7 +51,7 @@ func HandleInvoiceStatus(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleInvoice(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleInvoice(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
invoiceId string
|
||||
|
@ -90,11 +90,11 @@ func HandleInvoice(sc context.ServerContext) echo.HandlerFunc {
|
|||
"lnurl": invoice.PaymentRequest,
|
||||
"qr": qr,
|
||||
}
|
||||
return sc.Render(c, http.StatusOK, "invoice.html", data)
|
||||
return c.Render(http.StatusOK, "invoice.html", data)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleInvoices(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleInvoices(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
u db.User
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleLogin(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleLogin(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
lnAuth *auth.LNAuth
|
||||
|
@ -41,7 +41,7 @@ func HandleLogin(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleLoginCallback(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleLoginCallback(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
query auth.LNAuthResponse
|
||||
|
|
|
@ -27,7 +27,7 @@ func TestLogin(t *testing.T) {
|
|||
assert = assert.New(t)
|
||||
e *echo.Echo
|
||||
c echo.Context
|
||||
sc context.ServerContext
|
||||
sc context.Context
|
||||
req *http.Request
|
||||
rec *httptest.ResponseRecorder
|
||||
cookies []*http.Cookie
|
||||
|
@ -35,7 +35,7 @@ func TestLogin(t *testing.T) {
|
|||
dbSessionId string
|
||||
err error
|
||||
)
|
||||
sc = context.ServerContext{Db: db}
|
||||
sc = context.Context{Db: db}
|
||||
e, req, rec = test.HTTPMocks("GET", "/login", nil)
|
||||
c = e.NewContext(req, rec)
|
||||
|
||||
|
@ -63,7 +63,7 @@ func TestLoginCallback(t *testing.T) {
|
|||
assert = assert.New(t)
|
||||
e *echo.Echo
|
||||
c echo.Context
|
||||
sc context.ServerContext
|
||||
sc context.Context
|
||||
req *http.Request
|
||||
rec *httptest.ResponseRecorder
|
||||
sk *secp256k1.PrivateKey
|
||||
|
@ -96,7 +96,7 @@ func TestLoginCallback(t *testing.T) {
|
|||
}
|
||||
key = hex.EncodeToString(pk.SerializeCompressed())
|
||||
|
||||
sc = context.ServerContext{Db: db}
|
||||
sc = context.Context{Db: db}
|
||||
e, req, rec = test.HTTPMocks("GET", fmt.Sprintf("/api/login?k1=%s&key=%s&sig=%s", lnAuth.K1, key, sig), nil)
|
||||
c = e.NewContext(req, rec)
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleLogout(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleLogout(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
cookie *http.Cookie
|
||||
|
|
|
@ -26,7 +26,7 @@ func TestLogout(t *testing.T) {
|
|||
assert = assert.New(t)
|
||||
e *echo.Echo
|
||||
c echo.Context
|
||||
sc context.ServerContext
|
||||
sc context.Context
|
||||
req *http.Request
|
||||
rec *httptest.ResponseRecorder
|
||||
pk *secp256k1.PublicKey
|
||||
|
@ -34,7 +34,7 @@ func TestLogout(t *testing.T) {
|
|||
key string
|
||||
err error
|
||||
)
|
||||
sc = context.ServerContext{Db: db}
|
||||
sc = context.Context{Db: db}
|
||||
e, req, rec = test.HTTPMocks("POST", "/logout", nil)
|
||||
|
||||
_, pk, err = test.GenerateKeyPair()
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
)
|
||||
|
||||
func HandleMarket(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleMarket(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
marketId int64
|
||||
|
@ -68,7 +68,7 @@ func HandleMarket(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleCreateMarket(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleCreateMarket(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
tx *sql.Tx
|
||||
|
@ -131,7 +131,7 @@ func HandleCreateMarket(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleMarketOrders(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleMarketOrders(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
marketId int64
|
||||
|
@ -148,7 +148,7 @@ func HandleMarketOrders(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleOrder(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleOrder(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
tx *sql.Tx
|
||||
|
@ -250,7 +250,7 @@ func HandleOrder(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleDeleteOrder(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleDeleteOrder(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
orderId string
|
||||
|
@ -327,7 +327,7 @@ func HandleDeleteOrder(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleOrders(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleOrders(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
u db.User
|
||||
|
@ -342,7 +342,7 @@ func HandleOrders(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleMarketStats(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleMarketStats(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
marketId int64
|
||||
|
@ -359,7 +359,7 @@ func HandleMarketStats(sc context.ServerContext) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleMarketSettlement(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleMarketSettlement(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
marketId int64
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleCheckSession(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleCheckSession(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
cookie *http.Cookie
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleUser(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleUser(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
u db.User
|
||||
|
@ -25,6 +25,6 @@ func HandleUser(sc context.ServerContext) echo.HandlerFunc {
|
|||
"user": u,
|
||||
"Orders": orders,
|
||||
}
|
||||
return sc.Render(c, http.StatusOK, "user.html", data)
|
||||
return c.Render(http.StatusOK, "user.html", data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"github.com/lightningnetwork/lnd/zpay32"
|
||||
)
|
||||
|
||||
func HandleWithdrawal(sc context.ServerContext) echo.HandlerFunc {
|
||||
func HandleWithdrawal(sc context.Context) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
w db.Withdrawal
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func LNDGuard(sc context.ServerContext) echo.MiddlewareFunc {
|
||||
func LNDGuard(sc context.Context) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if sc.Lnd != nil {
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func Session(sc context.ServerContext) echo.MiddlewareFunc {
|
||||
func Session(sc context.Context) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
|
@ -39,7 +39,7 @@ func Session(sc context.ServerContext) echo.MiddlewareFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func SessionGuard(sc context.ServerContext) echo.MiddlewareFunc {
|
||||
func SessionGuard(sc context.Context) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
session := c.Get("session")
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
package pages
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
c "git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
)
|
||||
|
||||
templ Index() {
|
||||
<html>
|
||||
<head>
|
||||
<title>delphi.market</title>
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico"/>
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"/>
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"/>
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"/>
|
||||
<link rel="manifest" href="/site.webmanifest"/>
|
||||
<link rel="stylesheet" href="/index.css"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<meta name="theme-color" content="#091833"/>
|
||||
<script src="/htmx.js" integrity="sha384-Xh+GLLi0SMFPwtHQjT72aPG19QvKB8grnyRbYBNIdHWc2NkCrz65jlU7YrzO6qRp" crossorigin="anonymous"></script>
|
||||
if ctx.Value(c.EnvContextKey) == "development" {
|
||||
<script defer src="/hotreload.js"></script>
|
||||
}
|
||||
</head>
|
||||
<body>
|
||||
<header class="flex flex-row text-center justify-center pt-1">
|
||||
<nav>
|
||||
<a href="/">home</a>
|
||||
if ctx.Value(c.SessionContextKey) != nil {
|
||||
<a href="/user">user</a>
|
||||
} else {
|
||||
<a href="/login">login</a>
|
||||
}
|
||||
</nav>
|
||||
</header>
|
||||
<div class="container flex flex-column text-center">
|
||||
<code>
|
||||
<strong>
|
||||
<!-- need to escape backslashes or backtick here ... -->
|
||||
<pre>
|
||||
{ fmt.Sprintf(""+
|
||||
" _ _ _ _ \n"+
|
||||
" __| | ___| |_ __ | |__ (_)\n"+
|
||||
" / _` |/ _ \\ | '_ \\| '_ \\| |\n"+
|
||||
"| (_| | __/ | |_) | | | | |\n"+
|
||||
" \\__,_|\\___|_| .__/|_| |_|_|\n"+
|
||||
" |_| \n") }
|
||||
</pre>
|
||||
</strong>
|
||||
</code>
|
||||
<div class="font-mono mb-1">A prediction market using the lightning network [WIP]</div>
|
||||
</div>
|
||||
<footer class="flex justify-center">
|
||||
<div>
|
||||
<hr/>
|
||||
<small>
|
||||
<code>
|
||||
running
|
||||
<a
|
||||
href={ templ.SafeURL("https://github.com/ekzyis/delphi.market/commit/" + ctx.Value(c.CommitContextKey).(string)) }
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>{ ctx.Value(c.CommitContextKey).(string) }</a>
|
||||
</code>
|
||||
</small>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
}
|
|
@ -8,96 +8,10 @@ import (
|
|||
"git.ekzyis.com/ekzyis/delphi.market/server/router/middleware"
|
||||
)
|
||||
|
||||
type ServerContext = context.ServerContext
|
||||
type Context = context.Context
|
||||
|
||||
type MiddlewareFunc func(sc ServerContext) echo.MiddlewareFunc
|
||||
type HandlerFunc = func(sc ServerContext) echo.HandlerFunc
|
||||
func Init(e *echo.Echo, sc Context) {
|
||||
e.Use(middleware.Session(sc))
|
||||
|
||||
func AddRoutes(e *echo.Echo, sc ServerContext) {
|
||||
mountMiddleware(e, sc)
|
||||
addFrontendRoutes(e, sc)
|
||||
addBackendRoutes(e, sc)
|
||||
}
|
||||
|
||||
func mountMiddleware(e *echo.Echo, sc ServerContext) {
|
||||
Use(e, sc, middleware.Session)
|
||||
}
|
||||
|
||||
func addFrontendRoutes(e *echo.Echo, sc ServerContext) {
|
||||
GET(e, sc, "/user",
|
||||
handler.HandleUser,
|
||||
middleware.SessionGuard)
|
||||
GET(e, sc, "/market/:id",
|
||||
handler.HandleMarket,
|
||||
middleware.SessionGuard)
|
||||
POST(e, sc, "/market/:id/order",
|
||||
handler.HandleOrder,
|
||||
middleware.SessionGuard,
|
||||
middleware.LNDGuard)
|
||||
GET(e, sc, "/invoice/:id",
|
||||
handler.HandleInvoice,
|
||||
middleware.SessionGuard)
|
||||
}
|
||||
|
||||
func addBackendRoutes(e *echo.Echo, sc ServerContext) {
|
||||
GET(e, sc, "/api/markets", handler.HandleMarkets)
|
||||
POST(e, sc, "/api/market",
|
||||
handler.HandleCreateMarket,
|
||||
middleware.SessionGuard,
|
||||
middleware.LNDGuard)
|
||||
GET(e, sc, "/api/market/:id", handler.HandleMarket)
|
||||
GET(e, sc, "/api/market/:id/orders", handler.HandleMarketOrders)
|
||||
GET(e, sc, "/api/market/:id/stats", handler.HandleMarketStats)
|
||||
POST(e, sc, "/api/market/:id/settle",
|
||||
handler.HandleMarketSettlement,
|
||||
middleware.SessionGuard,
|
||||
middleware.LNDGuard)
|
||||
POST(e, sc, "/api/order",
|
||||
handler.HandleOrder,
|
||||
middleware.SessionGuard,
|
||||
middleware.LNDGuard)
|
||||
DELETE(e, sc, "/api/order/:id",
|
||||
handler.HandleDeleteOrder,
|
||||
middleware.SessionGuard)
|
||||
GET(e, sc, "/api/orders",
|
||||
handler.HandleOrders,
|
||||
middleware.SessionGuard)
|
||||
GET(e, sc, "/api/login", handler.HandleLogin)
|
||||
GET(e, sc, "/api/login/callback", handler.HandleLoginCallback)
|
||||
POST(e, sc, "/api/logout", handler.HandleLogout)
|
||||
GET(e, sc, "/api/session", handler.HandleCheckSession)
|
||||
GET(e, sc, "/api/invoice/:id",
|
||||
handler.HandleInvoiceStatus,
|
||||
middleware.SessionGuard)
|
||||
GET(e, sc, "/api/invoices",
|
||||
handler.HandleInvoices,
|
||||
middleware.SessionGuard)
|
||||
POST(e, sc, "/api/withdrawal",
|
||||
handler.HandleWithdrawal,
|
||||
middleware.SessionGuard,
|
||||
middleware.LNDGuard)
|
||||
}
|
||||
|
||||
func GET(e *echo.Echo, sc ServerContext, path string, scF HandlerFunc, scM ...MiddlewareFunc) *echo.Route {
|
||||
return e.GET(path, scF(sc), toMiddlewareFunc(sc, scM...)...)
|
||||
}
|
||||
|
||||
func POST(e *echo.Echo, sc ServerContext, path string, scF HandlerFunc, scM ...MiddlewareFunc) *echo.Route {
|
||||
return e.POST(path, scF(sc), toMiddlewareFunc(sc, scM...)...)
|
||||
}
|
||||
|
||||
func DELETE(e *echo.Echo, sc ServerContext, path string, scF HandlerFunc, scM ...MiddlewareFunc) *echo.Route {
|
||||
return e.DELETE(path, scF(sc), toMiddlewareFunc(sc, scM...)...)
|
||||
}
|
||||
|
||||
func Use(e *echo.Echo, sc ServerContext, scM ...MiddlewareFunc) {
|
||||
e.Use(toMiddlewareFunc(sc, scM...)...)
|
||||
}
|
||||
|
||||
func toMiddlewareFunc(sc ServerContext, scM ...MiddlewareFunc) []echo.MiddlewareFunc {
|
||||
var m []echo.MiddlewareFunc
|
||||
for _, m_ := range scM {
|
||||
m = append(m, m_(sc))
|
||||
}
|
||||
return m
|
||||
e.GET("/", handler.HandleIndex(sc))
|
||||
}
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
type Number interface {
|
||||
constraints.Integer | constraints.Float
|
||||
}
|
||||
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
|
||||
func ParseTemplates(pattern string) *Template {
|
||||
return &Template{
|
||||
templates: template.Must(template.New("").Funcs(template.FuncMap{
|
||||
"add": add[int64],
|
||||
"sub": sub[int64],
|
||||
"div": div[int64],
|
||||
"substr": substr,
|
||||
}).ParseGlob("pages/**.html")),
|
||||
}
|
||||
}
|
||||
|
||||
func add[T Number](arg1 T, arg2 T) T {
|
||||
return arg1 + arg2
|
||||
}
|
||||
|
||||
func sub[T Number](arg1 T, arg2 T) T {
|
||||
return arg1 - arg2
|
||||
}
|
||||
|
||||
func div[T Number](arg1 T, arg2 T) T {
|
||||
return arg1 / arg2
|
||||
}
|
||||
|
||||
func substr(s string, start, length int) string {
|
||||
if start < 0 || start >= len(s) {
|
||||
return ""
|
||||
}
|
||||
end := start + length
|
||||
if end > len(s) {
|
||||
end = len(s)
|
||||
}
|
||||
return s[start:end]
|
||||
}
|
|
@ -11,16 +11,17 @@ type Server struct {
|
|||
*echo.Echo
|
||||
}
|
||||
|
||||
type ServerContext = router.ServerContext
|
||||
type Context = router.Context
|
||||
|
||||
func New(ctx ServerContext) *Server {
|
||||
func New(ctx Context) *Server {
|
||||
var (
|
||||
e *echo.Echo
|
||||
s *Server
|
||||
)
|
||||
e = echo.New()
|
||||
|
||||
e.Static("/", "public")
|
||||
e.Renderer = router.ParseTemplates("pages/**.html")
|
||||
|
||||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||
Format: "${time_custom} ${method} ${uri} ${status}\n",
|
||||
CustomTimeFormat: "2006-01-02 15:04:05.00000-0700",
|
||||
|
@ -30,11 +31,12 @@ func New(ctx ServerContext) *Server {
|
|||
AllowCredentials: true,
|
||||
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
|
||||
}))
|
||||
|
||||
e.HTTPErrorHandler = httpErrorHandler
|
||||
|
||||
s = &Server{e}
|
||||
|
||||
router.AddRoutes(e, ctx)
|
||||
router.Init(e, ctx)
|
||||
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
|
@ -16,8 +15,9 @@ func HTTPMocks(method string, target string, body io.Reader) (*echo.Echo, *http.
|
|||
rec *httptest.ResponseRecorder
|
||||
)
|
||||
e = echo.New()
|
||||
e.Renderer = router.ParseTemplates("pages/**.html")
|
||||
|
||||
req = httptest.NewRequest(method, target, body)
|
||||
rec = httptest.NewRecorder()
|
||||
|
||||
return e, req, rec
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue