load .env, init db & lnd, render context

* more scaffolding code from delphi.market
* we now read environment from .env
* we now try to init db and lnd but in a non-blocking way
* we now set "render context" to have access to environment during template render
* frontend can now read commit from HTML attribute
This commit is contained in:
ekzyis 2024-10-26 03:56:41 +02:00
parent ccdc51eba5
commit 75955f5f84
17 changed files with 1405 additions and 40 deletions

0
.env.sample Normal file
View File

30
db/db.go Normal file
View File

@ -0,0 +1,30 @@
package db
import (
"database/sql"
_ "github.com/lib/pq"
)
type DB struct {
*sql.DB
}
func New(dbUrl string) (*DB, error) {
// open connection
db_, err := sql.Open("postgres", dbUrl)
if err != nil {
return nil, err
}
// test connection
_, err = db_.Exec("SELECT 1")
if err != nil {
return nil, err
}
// TODO: run migrations
db := &DB{DB: db_}
return db, nil
}

0
db/schema.sql Normal file
View File

40
env/env.go vendored Normal file
View File

@ -0,0 +1,40 @@
package env
import (
"log"
"os/exec"
"strings"
"github.com/joho/godotenv"
"github.com/namsral/flag"
)
var (
Port int
Env string
CommitShortSha string
)
func Load() error {
if err := godotenv.Load(); err != nil {
return err
}
flag.IntVar(&Port, "PORT", 4321, "Server port")
flag.StringVar(&Env, "ENV", "development", "Specify environment")
return nil
}
func Parse() {
flag.Parse()
CommitShortSha = execCmd("git", "rev-parse", "--short", "HEAD")
}
func execCmd(name string, args ...string) string {
cmd := exec.Command(name, args...)
stdout, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return strings.TrimSpace(string(stdout))
}

152
go.mod
View File

@ -5,22 +5,164 @@ go 1.22.0
toolchain go1.22.8
require (
github.com/a-h/templ v0.2.778
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.12.0
github.com/lib/pq v1.10.9
github.com/lightninglabs/lndclient v0.16.0-11
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42
github.com/namsral/flag v1.7.4-pre
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
)
require (
github.com/a-h/templ v0.2.778 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aead/siphash v1.0.1 // 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/btcec/v2 v2.3.2 // 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/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
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.3 // indirect
github.com/btcsuite/btcwallet/walletdb v1.4.0 // indirect
github.com/btcsuite/btcwallet/wtxmgr v1.5.0 // indirect
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.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/lru v1.0.0 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/fergusstrange/embedded-postgres v1.10.0 // 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
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/jrick/logrotate v1.0.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
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/labstack/gommon v0.4.2 // indirect
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf // indirect
github.com/lightninglabs/neutrino v0.15.0 // indirect
github.com/lightninglabs/neutrino/cache v1.1.1 // indirect
github.com/lightningnetwork/lightning-onion v1.2.1-0.20221202012345-ca23184850a1 // indirect
github.com/lightningnetwork/lnd v0.16.0-beta // indirect
github.com/lightningnetwork/lnd/clock v1.1.0 // indirect
github.com/lightningnetwork/lnd/healthcheck v1.2.2 // indirect
github.com/lightningnetwork/lnd/kvdb v1.4.1 // indirect
github.com/lightningnetwork/lnd/queue v1.1.0 // indirect
github.com/lightningnetwork/lnd/ticker v1.1.0 // indirect
github.com/lightningnetwork/lnd/tlv v1.1.0 // indirect
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.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
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/nwaples/rardecode v1.1.2 // indirect
github.com/pierrec/lz4/v4 v4.1.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/rogpeppe/fastuuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // 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
go.etcd.io/etcd/api/v3 v3.5.7 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.7 // indirect
go.etcd.io/etcd/client/v2 v2.305.7 // indirect
go.etcd.io/etcd/client/v3 v3.5.7 // indirect
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/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0 // indirect
go.opentelemetry.io/otel v1.0.1 // 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/sdk v1.0.1 // indirect
go.opentelemetry.io/otel/trace v1.0.1 // indirect
go.opentelemetry.io/proto/otlp v0.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.26.0 // indirect
google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/errgo.v1 v1.0.1 // indirect
gopkg.in/macaroon-bakery.v2 v2.0.1 // indirect
gopkg.in/macaroon.v2 v2.1.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.22.2 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.4.0 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/sqlite v1.20.3 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

1022
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
#!/usr/bin/env bash
# TODO: enumerate directories with shell
WATCH="db/ env/ fonts/ lib/ lightning/ pages/ public/ server/"
BINARY=hermes
PID=
@ -22,6 +24,7 @@ function cleanup() {
trap cleanup EXIT
restart
while inotifywait -r -e modify fonts/ lib/ pages/ public/ server/; do
while inotifywait -r -e modify $WATCH; do
echo
restart
done

22
lightning/lnd.go Normal file
View File

@ -0,0 +1,22 @@
package lightning
import (
"github.com/lightninglabs/lndclient"
)
type LNDClient struct {
*lndclient.GrpcLndServices
}
type LNDConfig = lndclient.LndServicesConfig
func New(config *LNDConfig) (*LNDClient, error) {
rcpLndServices, err := lndclient.NewLndServices(config)
if err != nil {
return nil, err
}
lnd := &LNDClient{GrpcLndServices: rcpLndServices}
return lnd, nil
}

63
main.go
View File

@ -1,11 +1,17 @@
package main
import (
"encoding/hex"
"flag"
"fmt"
"log"
"net/http"
"github.com/ekzyis/hermes/db"
"github.com/ekzyis/hermes/env"
"github.com/ekzyis/hermes/lightning"
"github.com/ekzyis/hermes/server"
"github.com/lightninglabs/lndclient"
)
func banner() {
@ -20,12 +26,65 @@ func banner() {
}
func main() {
var (
dbUrl string
lndAddress string
lndCert string
lndMacaroon string
lndNetwork string
)
banner()
s := server.New()
// env
if err := env.Load(); err != nil {
log.Fatal(err)
}
flag.StringVar(&dbUrl, "DATABASE_URL", "", "Public URL of website")
flag.StringVar(&lndAddress, "LND_ADDRESS", "localhost:10001", "LND gRPC server address")
flag.StringVar(&lndCert, "LND_CERT", "", "LND TLS certificate in hex")
flag.StringVar(&lndMacaroon, "LND_MACAROON", "", "LND macaroon in hex")
flag.StringVar(&lndNetwork, "LND_NETWORK", "regtest", "LND network")
env.Parse()
log.Printf("Commit: %s", env.CommitShortSha)
log.Printf("Environment: %s", env.Env)
// database
db_, err := db.New(dbUrl)
if err != nil {
log.Printf("❌ failed to connect to db: %v\n", err)
}
// lightning
tlsData, err := hex.DecodeString(lndCert)
if err != nil {
log.Printf("❌ failed to decode LND TLS certificate: %v\n", err)
}
lnd_, err := lightning.New(&lightning.LNDConfig{
LndAddress: lndAddress,
CustomMacaroonHex: lndMacaroon,
TLSData: string(tlsData),
Network: lndclient.Network(lndNetwork),
Insecure: false,
})
if err != nil {
log.Printf("❌ failed to connect to LND: %v\n", err)
lnd_ = nil
} else {
log.Printf("✅ connected to %s LND v%s\n", lndAddress, lnd_.Version.Version)
}
// server
ctx := server.Context{
Environment: env.Env,
CommitShortSha: env.CommitShortSha,
Db: db_,
Lnd: lnd_,
}
s := server.New(ctx)
// TODO: read port from env
if err := s.Start("127.0.0.1:8888"); err != http.ErrServerClosed {
if err = s.Start(fmt.Sprintf("127.0.0.1:%d", env.Port)); err != http.ErrServerClosed {
log.Fatal(err)
}
}

View File

@ -26,9 +26,6 @@ templ Head() {
if ctx.Value("env") != "production" {
<script defer src="/js/hotreload.js"></script>
}
<script type="text/javascript">
// helper functions
var $ = selector => document.querySelector(selector)
</script>
<script src="/js/lib.js"></script>
</head>
}

View File

@ -5,7 +5,10 @@ import "github.com/ekzyis/hermes/pages/components"
templ Index() {
<html>
@components.Head()
<body class="container">
<body
class="container"
data-commit={ templ.JSONString(ctx.Value("commit")) }
>
<div id="content" class="grid mt-3">
<div class="grid gap-10 self-center justify-self-center">
<h1 class="text-center text-5xl font-bold">0 sats</h1>

View File

@ -3,8 +3,7 @@
*/
// TODO: get commit from environment
const commit = "000000"
const commit = $$("commit")
console.log(`running ${commit} in development mode`)

4
public/js/lib.js Normal file
View File

@ -0,0 +1,4 @@
var $ = selector => document.querySelector(selector)
// access server-side data using HTML data-* attributes
var $$ = (attr) => JSON.parse($("body").dataset[attr])

16
server/router/index.go Normal file
View File

@ -0,0 +1,16 @@
package router
import (
"github.com/ekzyis/hermes/pages"
"github.com/labstack/echo/v4"
)
func Init(e *echo.Echo, hc Context) {
e.GET("/", HandleIndex(hc))
}
func HandleIndex(hc Context) echo.HandlerFunc {
return func(ec echo.Context) error {
return pages.Index().Render(hc.WithContext(ec), ec.Response().Writer)
}
}

View File

@ -0,0 +1,30 @@
package middleware
import (
"net/http"
"github.com/ekzyis/hermes/server/router"
"github.com/labstack/echo/v4"
)
func LndGuard(hc router.Context) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ec echo.Context) error {
if hc.Lnd != nil {
return next(ec)
}
return echo.NewHTTPError(http.StatusNotImplemented)
}
}
}
func DbGuard(hc router.Context) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ec echo.Context) error {
if hc.Db != nil {
return next(ec)
}
return echo.NewHTTPError(http.StatusNotImplemented)
}
}
}

26
server/router/router.go Normal file
View File

@ -0,0 +1,26 @@
package router
import (
"context"
"github.com/ekzyis/hermes/db"
"github.com/ekzyis/hermes/lightning"
"github.com/labstack/echo/v4"
)
type Context struct {
context.Context
Environment string
CommitShortSha string
Db *db.DB
Lnd *lightning.LNDClient
}
func (hc *Context) WithContext(ec echo.Context) context.Context {
ctx := ec.Request().Context()
ctx = context.WithValue(ctx, "env", hc.Environment)
ctx = context.WithValue(ctx, "session", ec.Get("session"))
ctx = context.WithValue(ctx, "commit", hc.CommitShortSha)
ctx = context.WithValue(ctx, "path", ec.Request().URL.Path)
return ctx
}

View File

@ -1,10 +1,7 @@
package server
import (
"context"
"os"
"github.com/ekzyis/hermes/pages"
"github.com/ekzyis/hermes/server/router"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
@ -13,7 +10,9 @@ type Server struct {
*echo.Echo
}
func New() *Server {
type Context = router.Context
func New(hc Context) *Server {
e := echo.New()
e.Static("/", "public")
@ -32,20 +31,9 @@ func New() *Server {
// TODO: attach error handler
// e.HTTPErrorHandler = ...
e.GET("/", func(c echo.Context) error {
return pages.Index().Render(getContext(c), c.Response().Writer)
})
router.Init(e, hc)
s := &Server{e}
return s
}
func getContext(c echo.Context) context.Context {
ctx := c.Request().Context()
ctx = context.WithValue(ctx, "env", os.Getenv("ENVIRONMENT"))
ctx = context.WithValue(ctx, "session", c.Get("session"))
ctx = context.WithValue(ctx, "commit", os.Getenv("GIT_COMMIT"))
ctx = context.WithValue(ctx, "path", c.Request().URL.Path)
return ctx
}