refactor: fix init and use DI
* fix init misuse which makes running tests impossible since tests are run inside a temporary test directory which breaks env loading * use dependency injection pattern for server creation and global server context in route handlers
This commit is contained in:
parent
50244ee9ca
commit
c66e96bf3f
40
db/db.go
40
db/db.go
|
@ -2,44 +2,28 @@ package db
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
var (
|
||||
db *DB
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
*sql.DB
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Fatalf("error loading env vars: %s", err)
|
||||
}
|
||||
var dbUrl string
|
||||
flag.StringVar(&dbUrl, "DATABASE_URL", "", "Database URL")
|
||||
flag.Parse()
|
||||
if dbUrl == "" {
|
||||
log.Fatal("DATABASE_URL not set")
|
||||
}
|
||||
db = initDB(dbUrl)
|
||||
}
|
||||
|
||||
func initDB(url string) *DB {
|
||||
db, err := sql.Open("postgres", url)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
func New(dbUrl string) (*DB, error) {
|
||||
var (
|
||||
db_ *sql.DB
|
||||
db *DB
|
||||
err error
|
||||
)
|
||||
if db_, err = sql.Open("postgres", dbUrl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// test connection
|
||||
_, err = db.Exec("SELECT 1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
if _, err = db_.Exec("SELECT 1"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO: run migrations
|
||||
return &DB{DB: db}
|
||||
db = &DB{DB: db_}
|
||||
return db, nil
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package db
|
|||
|
||||
import "time"
|
||||
|
||||
func CreateInvoice(invoice *Invoice) error {
|
||||
func (db *DB) CreateInvoice(invoice *Invoice) error {
|
||||
if err := db.QueryRow(""+
|
||||
"INSERT INTO invoices(pubkey, msats, preimage, hash, bolt11, created_at, expires_at) "+
|
||||
"VALUES($1, $2, $3, $4, $5, $6, $7) "+
|
||||
|
@ -18,7 +18,7 @@ type FetchInvoiceWhere struct {
|
|||
Hash string
|
||||
}
|
||||
|
||||
func FetchInvoice(where *FetchInvoiceWhere, invoice *Invoice) error {
|
||||
func (db *DB) FetchInvoice(where *FetchInvoiceWhere, invoice *Invoice) error {
|
||||
var (
|
||||
query = "SELECT id, pubkey, msats, preimage, hash, bolt11, created_at, expires_at, confirmed_at, held_since FROM invoices "
|
||||
args []any
|
||||
|
@ -38,7 +38,7 @@ func FetchInvoice(where *FetchInvoiceWhere, invoice *Invoice) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func ConfirmInvoice(hash string, confirmedAt time.Time, msatsReceived int) error {
|
||||
func (db *DB) ConfirmInvoice(hash string, confirmedAt time.Time, msatsReceived int) error {
|
||||
if _, err := db.Exec("UPDATE invoices SET confirmed_at = $2, msats_received = $3 WHERE hash = $1", hash, confirmedAt, msatsReceived); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
package db
|
||||
|
||||
func CreateLNAuth(lnAuth *LNAuth) error {
|
||||
func (db *DB) CreateLNAuth(lnAuth *LNAuth) error {
|
||||
err := db.QueryRow(
|
||||
"INSERT INTO lnauth(k1, lnurl) VALUES($1, $2) RETURNING session_id",
|
||||
lnAuth.K1, lnAuth.LNURL).Scan(&lnAuth.SessionId)
|
||||
return err
|
||||
}
|
||||
|
||||
func FetchSessionId(k1 string, sessionId *string) error {
|
||||
func (db *DB) FetchSessionId(k1 string, sessionId *string) error {
|
||||
err := db.QueryRow("SELECT session_id FROM lnauth WHERE k1 = $1", k1).Scan(sessionId)
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteLNAuth(lnAuth *LNAuth) error {
|
||||
func (db *DB) DeleteLNAuth(lnAuth *LNAuth) error {
|
||||
_, err := db.Exec("DELETE FROM lnauth WHERE k1 = $1", lnAuth.K1)
|
||||
return err
|
||||
}
|
||||
|
|
22
db/market.go
22
db/market.go
|
@ -2,14 +2,20 @@ package db
|
|||
|
||||
import "database/sql"
|
||||
|
||||
func FetchMarket(marketId int, market *Market) error {
|
||||
type FetchOrdersWhere struct {
|
||||
MarketId int
|
||||
Pubkey string
|
||||
Confirmed bool
|
||||
}
|
||||
|
||||
func (db *DB) FetchMarket(marketId int, market *Market) error {
|
||||
if err := db.QueryRow("SELECT id, description FROM markets WHERE id = $1", marketId).Scan(&market.Id, &market.Description); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FetchActiveMarkets(markets *[]Market) error {
|
||||
func (db *DB) FetchActiveMarkets(markets *[]Market) error {
|
||||
var (
|
||||
rows *sql.Rows
|
||||
market Market
|
||||
|
@ -26,7 +32,7 @@ func FetchActiveMarkets(markets *[]Market) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func FetchShares(marketId int, shares *[]Share) error {
|
||||
func (db *DB) FetchShares(marketId int, shares *[]Share) error {
|
||||
rows, err := db.Query("SELECT id, market_id, description FROM shares WHERE market_id = $1 ORDER BY description DESC", marketId)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -40,13 +46,7 @@ func FetchShares(marketId int, shares *[]Share) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
type FetchOrdersWhere struct {
|
||||
MarketId int
|
||||
Pubkey string
|
||||
Confirmed bool
|
||||
}
|
||||
|
||||
func FetchOrders(where *FetchOrdersWhere, orders *[]Order) error {
|
||||
func (db *DB) FetchOrders(where *FetchOrdersWhere, orders *[]Order) error {
|
||||
query := "" +
|
||||
"SELECT o.id, share_id, o.pubkey, o.side, o.quantity, o.price, o.invoice_id, o.created_at, s.description, s.market_id, i.confirmed_at " +
|
||||
"FROM orders o " +
|
||||
|
@ -79,7 +79,7 @@ func FetchOrders(where *FetchOrdersWhere, orders *[]Order) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func CreateOrder(order *Order) error {
|
||||
func (db *DB) CreateOrder(order *Order) error {
|
||||
if _, err := db.Exec(""+
|
||||
"INSERT INTO orders(share_id, pubkey, side, quantity, price, invoice_id) "+
|
||||
"VALUES ($1, $2, $3, $4, $5, $6)",
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package db
|
||||
|
||||
func CreateSession(s *Session) error {
|
||||
func (db *DB) CreateSession(s *Session) error {
|
||||
_, err := db.Exec("INSERT INTO sessions(pubkey, session_id) VALUES($1, $2)", s.Pubkey, s.SessionId)
|
||||
return err
|
||||
}
|
||||
|
||||
func FetchSession(s *Session) error {
|
||||
func (db *DB) FetchSession(s *Session) error {
|
||||
err := db.QueryRow("SELECT pubkey FROM sessions WHERE session_id = $1", s.SessionId).Scan(&s.Pubkey)
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteSession(s *Session) error {
|
||||
func (db *DB) DeleteSession(s *Session) error {
|
||||
_, err := db.Exec("DELETE FROM sessions where session_id = $1", s.SessionId)
|
||||
return err
|
||||
}
|
||||
|
|
111
db/types.go
111
db/types.go
|
@ -6,61 +6,56 @@ import (
|
|||
"gopkg.in/guregu/null.v4"
|
||||
)
|
||||
|
||||
type Serial = int
|
||||
type UUID = string
|
||||
|
||||
type LNAuth struct {
|
||||
K1 string
|
||||
LNURL string
|
||||
CreatdAt time.Time
|
||||
SessionId string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Pubkey string
|
||||
LastSeen time.Time
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
Pubkey string
|
||||
SessionId string
|
||||
}
|
||||
|
||||
type Market struct {
|
||||
Id Serial
|
||||
Description string
|
||||
Active bool
|
||||
}
|
||||
|
||||
type Share struct {
|
||||
Id UUID
|
||||
MarketId int
|
||||
Description string
|
||||
}
|
||||
|
||||
type Invoice struct {
|
||||
Id UUID
|
||||
Pubkey string
|
||||
Msats int64
|
||||
MsatsReceived int64
|
||||
Preimage string
|
||||
Hash string
|
||||
PaymentRequest string
|
||||
CreatedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
ConfirmedAt null.Time
|
||||
HeldSince null.Time
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
Id UUID
|
||||
CreatedAt time.Time
|
||||
ShareId string `form:"share_id"`
|
||||
Share
|
||||
Pubkey string
|
||||
Side string `form:"side"`
|
||||
Quantity int64 `form:"quantity"`
|
||||
Price int64 `form:"price"`
|
||||
InvoiceId UUID
|
||||
Invoice
|
||||
}
|
||||
type (
|
||||
Serial = int
|
||||
UUID = string
|
||||
LNAuth struct {
|
||||
K1 string
|
||||
LNURL string
|
||||
CreatdAt time.Time
|
||||
SessionId string
|
||||
}
|
||||
User struct {
|
||||
Pubkey string
|
||||
LastSeen time.Time
|
||||
}
|
||||
Session struct {
|
||||
Pubkey string
|
||||
SessionId string
|
||||
}
|
||||
Market struct {
|
||||
Id Serial
|
||||
Description string
|
||||
Active bool
|
||||
}
|
||||
Share struct {
|
||||
Id UUID
|
||||
MarketId int
|
||||
Description string
|
||||
}
|
||||
Invoice struct {
|
||||
Id UUID
|
||||
Pubkey string
|
||||
Msats int64
|
||||
MsatsReceived int64
|
||||
Preimage string
|
||||
Hash string
|
||||
PaymentRequest string
|
||||
CreatedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
ConfirmedAt null.Time
|
||||
HeldSince null.Time
|
||||
}
|
||||
Order struct {
|
||||
Id UUID
|
||||
CreatedAt time.Time
|
||||
ShareId string `form:"share_id"`
|
||||
Share
|
||||
Pubkey string
|
||||
Side string `form:"side"`
|
||||
Quantity int64 `form:"quantity"`
|
||||
Price int64 `form:"price"`
|
||||
InvoiceId UUID
|
||||
Invoice
|
||||
}
|
||||
)
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package db
|
||||
|
||||
func CreateUser(u *User) error {
|
||||
func (db *DB) CreateUser(u *User) error {
|
||||
_, err := db.Exec(
|
||||
"INSERT INTO users(pubkey) VALUES ($1) ON CONFLICT(pubkey) DO UPDATE SET last_seen = CURRENT_TIMESTAMP",
|
||||
u.Pubkey)
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateUser(u *User) error {
|
||||
func (db *DB) UpdateUser(u *User) error {
|
||||
_, err := db.Exec("UPDATE users SET last_seen = $1 WHERE pubkey = $2", u.LastSeen, u.Pubkey)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -19,13 +19,17 @@ var (
|
|||
Version string
|
||||
)
|
||||
|
||||
func init() {
|
||||
func Load(filenames ...string) error {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Fatalf("error loading env vars: %s", err)
|
||||
return err
|
||||
}
|
||||
flag.StringVar(&PublicURL, "PUBLIC_URL", "delphi.market", "Public URL of website")
|
||||
flag.IntVar(&Port, "PORT", 4321, "Server port")
|
||||
flag.StringVar(&Env, "ENV", "development", "Specify for which environment files should be built")
|
||||
flag.StringVar(&Env, "ENV", "development", "Specify environment")
|
||||
return nil
|
||||
}
|
||||
|
||||
func Parse() {
|
||||
flag.Parse()
|
||||
CommitLongSha = execCmd("git", "rev-parse", "HEAD")
|
||||
CommitShortSha = execCmd("git", "rev-parse", "--short", "HEAD")
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
func CreateInvoice(pubkey string, msats int64) (*db.Invoice, error) {
|
||||
func (lnd *LNDClient) CreateInvoice(d *db.DB, pubkey string, msats int64) (*db.Invoice, error) {
|
||||
var (
|
||||
expiry time.Duration = time.Hour
|
||||
preimage lntypes.Preimage
|
||||
|
@ -45,13 +45,13 @@ func CreateInvoice(pubkey string, msats int64) (*db.Invoice, error) {
|
|||
CreatedAt: lnInvoice.CreationDate,
|
||||
ExpiresAt: lnInvoice.CreationDate.Add(expiry),
|
||||
}
|
||||
if err := db.CreateInvoice(dbInvoice); err != nil {
|
||||
if err := d.CreateInvoice(dbInvoice); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dbInvoice, nil
|
||||
}
|
||||
|
||||
func CheckInvoice(hash lntypes.Hash) {
|
||||
func (lnd *LNDClient) CheckInvoice(d *db.DB, hash lntypes.Hash) {
|
||||
var (
|
||||
pollInterval = 5 * time.Second
|
||||
invoice db.Invoice
|
||||
|
@ -60,12 +60,7 @@ func CheckInvoice(hash lntypes.Hash) {
|
|||
err error
|
||||
)
|
||||
|
||||
if !Enabled {
|
||||
log.Printf("LND disabled, skipping checking invoice: hash=%s", hash)
|
||||
return
|
||||
}
|
||||
|
||||
if err = db.FetchInvoice(&db.FetchInvoiceWhere{Hash: hash.String()}, &invoice); err != nil {
|
||||
if err = d.FetchInvoice(&db.FetchInvoiceWhere{Hash: hash.String()}, &invoice); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
@ -100,7 +95,7 @@ func CheckInvoice(hash lntypes.Hash) {
|
|||
handleLoopError(err)
|
||||
continue
|
||||
}
|
||||
if err = db.ConfirmInvoice(hash.String(), time.Now(), int(lnInvoice.AmountPaid)); err != nil {
|
||||
if err = d.ConfirmInvoice(hash.String(), time.Now(), int(lnInvoice.AmountPaid)); err != nil {
|
||||
handleLoopError(err)
|
||||
continue
|
||||
}
|
||||
|
|
44
lnd/lnd.go
44
lnd/lnd.go
|
@ -3,47 +3,25 @@ package lnd
|
|||
import (
|
||||
"log"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/lightninglabs/lndclient"
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
var (
|
||||
lnd *LNDClient
|
||||
Enabled bool
|
||||
)
|
||||
|
||||
type LNDClient struct {
|
||||
lndclient.GrpcLndServices
|
||||
*lndclient.GrpcLndServices
|
||||
}
|
||||
|
||||
func init() {
|
||||
type LNDConfig = lndclient.LndServicesConfig
|
||||
|
||||
func New(config *LNDConfig) (*LNDClient, error) {
|
||||
var (
|
||||
lndCert string
|
||||
lndMacaroonDir string
|
||||
lndHost string
|
||||
rpcLndServices *lndclient.GrpcLndServices
|
||||
rcpLndServices *lndclient.GrpcLndServices
|
||||
lnd *LNDClient
|
||||
err error
|
||||
)
|
||||
if err = godotenv.Load(); err != nil {
|
||||
log.Fatalf("error loading env vars: %s", err)
|
||||
if rcpLndServices, err = lndclient.NewLndServices(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flag.StringVar(&lndCert, "LND_CERT", "", "Path to LND TLS certificate")
|
||||
flag.StringVar(&lndMacaroonDir, "LND_MACAROON_DIR", "", "LND macaroon directory")
|
||||
flag.StringVar(&lndHost, "LND_HOST", "localhost:10001", "LND gRPC server address")
|
||||
flag.Parse()
|
||||
if rpcLndServices, err = lndclient.NewLndServices(&lndclient.LndServicesConfig{
|
||||
LndAddress: lndHost,
|
||||
MacaroonDir: lndMacaroonDir,
|
||||
TLSPath: lndCert,
|
||||
// TODO: make network configurable
|
||||
Network: lndclient.NetworkRegtest,
|
||||
}); err != nil {
|
||||
log.Println(err)
|
||||
Enabled = false
|
||||
return
|
||||
}
|
||||
lnd = &LNDClient{GrpcLndServices: *rpcLndServices}
|
||||
log.Printf("Connected to %s running LND v%s", lndHost, lnd.Version.Version)
|
||||
Enabled = true
|
||||
lnd = &LNDClient{GrpcLndServices: rcpLndServices}
|
||||
log.Printf("Connected to %s running LND v%s", config.LndAddress, lnd.Version.Version)
|
||||
return lnd, nil
|
||||
}
|
||||
|
|
61
main.go
61
main.go
|
@ -5,8 +5,13 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/env"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lnd"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router"
|
||||
"github.com/lightninglabs/lndclient"
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -14,11 +19,65 @@ var (
|
|||
)
|
||||
|
||||
func init() {
|
||||
var (
|
||||
dbUrl string
|
||||
lndAddress string
|
||||
lndCert string
|
||||
lndMacaroonDir string
|
||||
db_ *db.DB
|
||||
lnd_ *lnd.LNDClient
|
||||
ctx router.ServerContext
|
||||
err error
|
||||
)
|
||||
if err = env.Load(); err != nil {
|
||||
log.Fatalf("error loading env vars: %v", err)
|
||||
}
|
||||
flag.StringVar(&dbUrl, "DATABASE_URL", "delphi.market", "Public URL of website")
|
||||
flag.StringVar(&lndAddress, "LND_ADDRESS", "localhost:10001", "LND gRPC server address")
|
||||
flag.StringVar(&lndCert, "LND_CERT", "", "Path to LND TLS certificate")
|
||||
flag.StringVar(&lndMacaroonDir, "LND_MACAROON_DIR", "", "LND macaroon directory")
|
||||
env.Parse()
|
||||
figlet()
|
||||
log.Printf("Commit: %s", env.CommitShortSha)
|
||||
log.Printf("Public URL: %s", env.PublicURL)
|
||||
log.Printf("Environment: %s", env.Env)
|
||||
|
||||
s = server.NewServer()
|
||||
if db_, err = db.New(dbUrl); err != nil {
|
||||
log.Fatalf("error connecting to database: %v", err)
|
||||
}
|
||||
|
||||
if lnd_, err = lnd.New(&lnd.LNDConfig{
|
||||
LndAddress: lndAddress,
|
||||
TLSPath: lndCert,
|
||||
MacaroonDir: lndMacaroonDir,
|
||||
Network: lndclient.NetworkRegtest,
|
||||
}); err != nil {
|
||||
log.Printf("[warn] error connecting to LND: %v\n", err)
|
||||
lnd_ = nil
|
||||
}
|
||||
|
||||
ctx = server.ServerContext{
|
||||
PublicURL: env.PublicURL,
|
||||
CommitShortSha: env.CommitShortSha,
|
||||
CommitLongSha: env.CommitLongSha,
|
||||
Version: env.Version,
|
||||
Db: db_,
|
||||
Lnd: lnd_,
|
||||
}
|
||||
s = server.New(ctx)
|
||||
}
|
||||
|
||||
func figlet() {
|
||||
log.Println(
|
||||
"\n" +
|
||||
" _ _ _ _ \n" +
|
||||
" __| | ___| |_ __ | |__ (_)\n" +
|
||||
" / _` |/ _ \\ | '_ \\| '_ \\| |\n" +
|
||||
"| (_| | __/ | |_) | | | | |\n" +
|
||||
" \\__,_|\\___|_| .__/|_| |_|_|\n" +
|
||||
" |_| .market \n" +
|
||||
"----------------------------",
|
||||
)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lnd"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type ServerContext struct {
|
||||
Environment string
|
||||
PublicURL string
|
||||
CommitShortSha string
|
||||
CommitLongSha string
|
||||
Version string
|
||||
Db *db.DB
|
||||
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)
|
||||
}
|
||||
|
||||
func merge[T comparable](target *map[T]any, src *map[T]any) {
|
||||
for k, v := range *src {
|
||||
(*target)[k] = v
|
||||
}
|
||||
}
|
|
@ -4,25 +4,25 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lib"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleIndex(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleIndex(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
markets []db.Market
|
||||
err error
|
||||
data map[string]any
|
||||
)
|
||||
if err = db.FetchActiveMarkets(&markets); err != nil {
|
||||
if err = sc.Db.FetchActiveMarkets(&markets); err != nil {
|
||||
return err
|
||||
}
|
||||
data = map[string]any{
|
||||
"session": c.Get("session"),
|
||||
"markets": markets,
|
||||
}
|
||||
lib.Merge(&data, &envVars)
|
||||
return c.Render(http.StatusOK, "index.html", data)
|
||||
|
||||
return sc.Render(c, http.StatusOK, "index.html", data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,12 +7,12 @@ import (
|
|||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lib"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lnd"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
)
|
||||
|
||||
func HandleInvoiceAPI(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleInvoiceAPI(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
invoiceId string
|
||||
|
@ -21,7 +21,7 @@ func HandleInvoiceAPI(envVars map[string]any) echo.HandlerFunc {
|
|||
err error
|
||||
)
|
||||
invoiceId = c.Param("id")
|
||||
if err = db.FetchInvoice(&db.FetchInvoiceWhere{Id: invoiceId}, &invoice); err == sql.ErrNoRows {
|
||||
if err = sc.Db.FetchInvoice(&db.FetchInvoiceWhere{Id: invoiceId}, &invoice); err == sql.ErrNoRows {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
@ -34,7 +34,7 @@ func HandleInvoiceAPI(envVars map[string]any) echo.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleInvoice(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleInvoice(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
invoiceId string
|
||||
|
@ -46,7 +46,7 @@ func HandleInvoice(envVars map[string]any) echo.HandlerFunc {
|
|||
err error
|
||||
)
|
||||
invoiceId = c.Param("id")
|
||||
if err = db.FetchInvoice(&db.FetchInvoiceWhere{Id: invoiceId}, &invoice); err == sql.ErrNoRows {
|
||||
if err = sc.Db.FetchInvoice(&db.FetchInvoiceWhere{Id: invoiceId}, &invoice); err == sql.ErrNoRows {
|
||||
return echo.NewHTTPError(http.StatusNotFound)
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
@ -57,7 +57,7 @@ func HandleInvoice(envVars map[string]any) echo.HandlerFunc {
|
|||
if hash, err = lntypes.MakeHashFromStr(invoice.Hash); err != nil {
|
||||
return err
|
||||
}
|
||||
go lnd.CheckInvoice(hash)
|
||||
go sc.Lnd.CheckInvoice(sc.Db, hash)
|
||||
if qr, err = lib.ToQR(invoice.PaymentRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -73,6 +73,6 @@ func HandleInvoice(envVars map[string]any) echo.HandlerFunc {
|
|||
"lnurl": invoice.PaymentRequest,
|
||||
"qr": qr,
|
||||
}
|
||||
return c.Render(http.StatusOK, "invoice.html", data)
|
||||
return sc.Render(c, http.StatusOK, "invoice.html", data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,10 +8,11 @@ import (
|
|||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lib"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/auth"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleLogin(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleLogin(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
lnAuth *auth.LNAuth
|
||||
|
@ -25,7 +26,7 @@ func HandleLogin(envVars map[string]any) echo.HandlerFunc {
|
|||
return err
|
||||
}
|
||||
dbLnAuth = db.LNAuth{K1: lnAuth.K1, LNURL: lnAuth.LNURL}
|
||||
if err = db.CreateLNAuth(&dbLnAuth); err != nil {
|
||||
if err = sc.Db.CreateLNAuth(&dbLnAuth); err != nil {
|
||||
return err
|
||||
}
|
||||
c.SetCookie(&http.Cookie{Name: "session", HttpOnly: true, Path: "/", Value: dbLnAuth.SessionId, Secure: true, Expires: expires})
|
||||
|
@ -37,12 +38,11 @@ func HandleLogin(envVars map[string]any) echo.HandlerFunc {
|
|||
"lnurl": lnAuth.LNURL,
|
||||
"qr": qr,
|
||||
}
|
||||
lib.Merge(&data, &envVars)
|
||||
return c.Render(http.StatusOK, "login.html", data)
|
||||
return sc.Render(c, http.StatusOK, "login.html", data)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleLoginCallback(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleLoginCallback(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
query auth.LNAuthResponse
|
||||
|
@ -52,7 +52,7 @@ func HandleLoginCallback(envVars map[string]any) echo.HandlerFunc {
|
|||
if err := c.Bind(&query); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest)
|
||||
}
|
||||
if err = db.FetchSessionId(query.K1, &sessionId); err == sql.ErrNoRows {
|
||||
if err = sc.Db.FetchSessionId(query.K1, &sessionId); err == sql.ErrNoRows {
|
||||
return echo.NewHTTPError(http.StatusNotFound, map[string]string{"reason": "session not found"})
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
@ -62,13 +62,13 @@ func HandleLoginCallback(envVars map[string]any) echo.HandlerFunc {
|
|||
} else if !ok {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, map[string]string{"reason": "bad signature"})
|
||||
}
|
||||
if err = db.CreateUser(&db.User{Pubkey: query.Key}); err != nil {
|
||||
if err = sc.Db.CreateUser(&db.User{Pubkey: query.Key}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = db.CreateSession(&db.Session{Pubkey: query.Key, SessionId: sessionId}); err != nil {
|
||||
if err = sc.Db.CreateSession(&db.Session{Pubkey: query.Key, SessionId: sessionId}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = db.DeleteLNAuth(&db.LNAuth{K1: query.K1}); err != nil {
|
||||
if err = sc.Db.DeleteLNAuth(&db.LNAuth{K1: query.K1}); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(http.StatusOK, map[string]string{"status": "OK"})
|
||||
|
|
|
@ -5,10 +5,11 @@ import (
|
|||
"time"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleLogout(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleLogout(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
cookie *http.Cookie
|
||||
|
@ -20,7 +21,7 @@ func HandleLogout(envVars map[string]any) echo.HandlerFunc {
|
|||
return c.Redirect(http.StatusSeeOther, "/")
|
||||
}
|
||||
sessionId = cookie.Value
|
||||
if err = db.DeleteSession(&db.Session{SessionId: sessionId}); err != nil {
|
||||
if err = sc.Db.DeleteSession(&db.Session{SessionId: sessionId}); err != nil {
|
||||
return err
|
||||
}
|
||||
// tell browser that cookie is expired and thus can be deleted
|
||||
|
|
|
@ -9,12 +9,12 @@ import (
|
|||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/env"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lib"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lnd"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
)
|
||||
|
||||
func HandleMarket(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleMarket(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
marketId int64
|
||||
|
@ -27,15 +27,15 @@ func HandleMarket(envVars map[string]any) echo.HandlerFunc {
|
|||
if marketId, err = strconv.ParseInt(c.Param("id"), 10, 64); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Bad Request")
|
||||
}
|
||||
if err = db.FetchMarket(int(marketId), &market); err == sql.ErrNoRows {
|
||||
if err = sc.Db.FetchMarket(int(marketId), &market); err == sql.ErrNoRows {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "Not Found")
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = db.FetchShares(market.Id, &shares); err != nil {
|
||||
if err = sc.Db.FetchShares(market.Id, &shares); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = db.FetchOrders(&db.FetchOrdersWhere{MarketId: market.Id, Confirmed: true}, &orders); err != nil {
|
||||
if err = sc.Db.FetchOrders(&db.FetchOrdersWhere{MarketId: market.Id, Confirmed: true}, &orders); err != nil {
|
||||
return err
|
||||
}
|
||||
data = map[string]any{
|
||||
|
@ -48,12 +48,11 @@ func HandleMarket(envVars map[string]any) echo.HandlerFunc {
|
|||
"NoShare": shares[1],
|
||||
"Orders": orders,
|
||||
}
|
||||
lib.Merge(&data, &envVars)
|
||||
return c.Render(http.StatusOK, "market.html", data)
|
||||
return sc.Render(c, http.StatusOK, "market.html", data)
|
||||
}
|
||||
}
|
||||
|
||||
func HandlePostOrder(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandlePostOrder(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
marketId string
|
||||
|
@ -86,7 +85,7 @@ func HandlePostOrder(envVars map[string]any) echo.HandlerFunc {
|
|||
// TODO: if SELL order, check share balance of user
|
||||
|
||||
// Create HODL invoice
|
||||
if invoice, err = lnd.CreateInvoice(o.Pubkey, msats); err != nil {
|
||||
if invoice, err = sc.Lnd.CreateInvoice(sc.Db, o.Pubkey, msats); err != nil {
|
||||
return err
|
||||
}
|
||||
// Create QR code to pay HODL invoice
|
||||
|
@ -98,11 +97,11 @@ func HandlePostOrder(envVars map[string]any) echo.HandlerFunc {
|
|||
}
|
||||
|
||||
// Start goroutine to poll status and update invoice in background
|
||||
go lnd.CheckInvoice(hash)
|
||||
go sc.Lnd.CheckInvoice(sc.Db, hash)
|
||||
|
||||
// Create (unconfirmed) order
|
||||
o.InvoiceId = invoice.Id
|
||||
if err := db.CreateOrder(&o); err != nil {
|
||||
if err := sc.Db.CreateOrder(&o); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -115,7 +114,6 @@ func HandlePostOrder(envVars map[string]any) echo.HandlerFunc {
|
|||
"invoice": *invoice,
|
||||
"redirectURL": fmt.Sprintf("https://%s/market/%s", env.PublicURL, marketId),
|
||||
}
|
||||
lib.Merge(&data, &envVars)
|
||||
return c.Render(http.StatusPaymentRequired, "invoice.html", data)
|
||||
return sc.Render(c, http.StatusPaymentRequired, "invoice.html", data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,10 +5,11 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleCheckSession(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleCheckSession(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
cookie *http.Cookie
|
||||
|
@ -19,7 +20,7 @@ func HandleCheckSession(envVars map[string]any) echo.HandlerFunc {
|
|||
return echo.NewHTTPError(http.StatusBadRequest, map[string]string{"reason": "cookie required"})
|
||||
}
|
||||
s = db.Session{SessionId: cookie.Value}
|
||||
if err = db.FetchSession(&s); err == sql.ErrNoRows {
|
||||
if err = sc.Db.FetchSession(&s); err == sql.ErrNoRows {
|
||||
return echo.NewHTTPError(http.StatusNotFound, map[string]string{"reason": "session not found"})
|
||||
} else if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"net/http"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lib"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func HandleUser(envVars map[string]any) echo.HandlerFunc {
|
||||
func HandleUser(sc context.ServerContext) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
u db.User
|
||||
|
@ -17,7 +17,7 @@ func HandleUser(envVars map[string]any) echo.HandlerFunc {
|
|||
data map[string]any
|
||||
)
|
||||
u = c.Get("session").(db.User)
|
||||
if err = db.FetchOrders(&db.FetchOrdersWhere{Pubkey: u.Pubkey}, &orders); err != nil {
|
||||
if err = sc.Db.FetchOrders(&db.FetchOrdersWhere{Pubkey: u.Pubkey}, &orders); err != nil {
|
||||
return err
|
||||
}
|
||||
data = map[string]any{
|
||||
|
@ -25,7 +25,6 @@ func HandleUser(envVars map[string]any) echo.HandlerFunc {
|
|||
"user": u,
|
||||
"Orders": orders,
|
||||
}
|
||||
lib.Merge(&data, &envVars)
|
||||
return c.Render(http.StatusOK, "user.html", data)
|
||||
return sc.Render(c, http.StatusOK, "user.html", data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,14 +3,14 @@ package middleware
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/lnd"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func LNDGuard(envVars map[string]any) echo.MiddlewareFunc {
|
||||
func LNDGuard(sc context.ServerContext) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if lnd.Enabled {
|
||||
if sc.Lnd != nil {
|
||||
return next(c)
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusMethodNotAllowed)
|
||||
|
|
|
@ -6,10 +6,11 @@ import (
|
|||
"time"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/db"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func Session(envVars map[string]any) echo.MiddlewareFunc {
|
||||
func Session(sc context.ServerContext) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
var (
|
||||
|
@ -23,10 +24,10 @@ func Session(envVars map[string]any) echo.MiddlewareFunc {
|
|||
return next(c)
|
||||
}
|
||||
s = &db.Session{SessionId: cookie.Value}
|
||||
if err = db.FetchSession(s); err == nil {
|
||||
if err = sc.Db.FetchSession(s); err == nil {
|
||||
// session found
|
||||
u = &db.User{Pubkey: s.Pubkey, LastSeen: time.Now()}
|
||||
if err = db.UpdateUser(u); err != nil {
|
||||
if err = sc.Db.UpdateUser(u); err != nil {
|
||||
return err
|
||||
}
|
||||
c.Set("session", *u)
|
||||
|
@ -38,7 +39,7 @@ func Session(envVars map[string]any) echo.MiddlewareFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func SessionGuard(envVars map[string]any) echo.MiddlewareFunc {
|
||||
func SessionGuard(sc context.ServerContext) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
session := c.Get("session")
|
||||
|
|
|
@ -3,42 +3,69 @@ package router
|
|||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"git.ekzyis.com/ekzyis/delphi.market/env"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/handler"
|
||||
"git.ekzyis.com/ekzyis/delphi.market/server/router/middleware"
|
||||
)
|
||||
|
||||
func AddRoutes(e *echo.Echo) {
|
||||
envVars := map[string]any{
|
||||
"PUBLIC_URL": env.PublicURL,
|
||||
"COMMIT_SHORT_SHA": env.CommitShortSha,
|
||||
"COMMIT_LONG_SHA": env.CommitLongSha,
|
||||
"VERSION": env.Version,
|
||||
}
|
||||
e.Use(middleware.Session(envVars))
|
||||
e.GET("/", handler.HandleIndex(envVars))
|
||||
e.GET("/login", handler.HandleLogin(envVars))
|
||||
e.GET("/api/login", handler.HandleLoginCallback(envVars))
|
||||
e.GET("/api/session", handler.HandleCheckSession(envVars))
|
||||
e.POST("/logout", handler.HandleLogout(envVars))
|
||||
e.GET("/user",
|
||||
handler.HandleUser(envVars),
|
||||
middleware.SessionGuard(envVars))
|
||||
e.GET("/market/:id",
|
||||
handler.HandleMarket(envVars),
|
||||
middleware.SessionGuard(envVars))
|
||||
e.POST("/market/:id/order",
|
||||
handler.HandlePostOrder(envVars),
|
||||
middleware.SessionGuard(envVars),
|
||||
middleware.LNDGuard(envVars))
|
||||
e.GET("/invoice/:id",
|
||||
handler.HandleInvoice(envVars),
|
||||
middleware.SessionGuard(envVars),
|
||||
middleware.LNDGuard(envVars),
|
||||
)
|
||||
e.GET("/api/invoice/:id",
|
||||
handler.HandleInvoiceAPI(envVars),
|
||||
middleware.SessionGuard(envVars),
|
||||
middleware.LNDGuard(envVars),
|
||||
)
|
||||
type ServerContext = context.ServerContext
|
||||
|
||||
type MiddlewareFunc func(sc ServerContext) echo.MiddlewareFunc
|
||||
type HandlerFunc = func(sc ServerContext) echo.HandlerFunc
|
||||
|
||||
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, "/", handler.HandleIndex)
|
||||
GET(e, sc, "/login", handler.HandleLogin)
|
||||
POST(e, sc, "/logout", handler.HandleLogout)
|
||||
GET(e, sc, "/user",
|
||||
handler.HandleUser,
|
||||
middleware.SessionGuard)
|
||||
GET(e, sc, "/market/:id",
|
||||
handler.HandleMarket,
|
||||
middleware.SessionGuard)
|
||||
POST(e, sc, "/market/:id/order",
|
||||
handler.HandlePostOrder,
|
||||
middleware.SessionGuard,
|
||||
middleware.LNDGuard)
|
||||
GET(e, sc, "/invoice/:id",
|
||||
handler.HandleInvoice,
|
||||
middleware.SessionGuard)
|
||||
}
|
||||
|
||||
func addBackendRoutes(e *echo.Echo, sc ServerContext) {
|
||||
GET(e, sc, "/api/login", handler.HandleLoginCallback)
|
||||
GET(e, sc, "/api/session", handler.HandleCheckSession)
|
||||
GET(e, sc, "/api/invoice/:id",
|
||||
handler.HandleInvoiceAPI,
|
||||
middleware.SessionGuard)
|
||||
}
|
||||
|
||||
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 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
|
||||
}
|
||||
|
|
|
@ -11,21 +11,25 @@ type Server struct {
|
|||
*echo.Echo
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
e := echo.New()
|
||||
type ServerContext = router.ServerContext
|
||||
|
||||
func New(ctx ServerContext) *Server {
|
||||
var (
|
||||
e *echo.Echo
|
||||
s *Server
|
||||
)
|
||||
e = echo.New()
|
||||
e.Static("/", "public")
|
||||
|
||||
e.Renderer = router.T
|
||||
|
||||
router.AddRoutes(e)
|
||||
|
||||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||
Format: "${time_custom} ${method} ${uri} ${status}\n",
|
||||
CustomTimeFormat: "2006-01-02 15:04:05.00000-0700",
|
||||
}))
|
||||
|
||||
e.HTTPErrorHandler = httpErrorHandler
|
||||
|
||||
return &Server{e}
|
||||
s = &Server{e}
|
||||
|
||||
router.AddRoutes(e, ctx)
|
||||
|
||||
return s
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue