From 6e3cee596f5535ad4d116b971c9f23d7795b864f Mon Sep 17 00:00:00 2001 From: Darp Alakun Date: Sat, 4 Jan 2025 19:52:54 -0500 Subject: [PATCH] feat: Dockerize the application --- Dockerfile | 3 ++ Makefile | 4 +-- app/app.go | 4 +-- cmd/main.go | 48 ------------------------------- cmd/cmds.go => main.go | 59 +++++++++++++++++++++++++-------------- pkg/common/chain_query.go | 6 ++-- pkg/common/ucan_store.go | 2 -- 7 files changed, 48 insertions(+), 78 deletions(-) create mode 100644 Dockerfile delete mode 100644 cmd/main.go rename cmd/cmds.go => main.go (69%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d23a012 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM scratch +ENTRYPOINT ["/hway"] +COPY hway / diff --git a/Makefile b/Makefile index 7358b0b..dea8f50 100644 --- a/Makefile +++ b/Makefile @@ -6,12 +6,12 @@ BINDIR ?= $(GOPATH)/bin # for dockerized protobuf tools DOCKER := $(shell which docker) HTTPS_GIT := github.com/onsonr/hway.git - +export RELEASE_DATE="$(date +%Y).$(date +%V).$(date +%u)" all: install test build: go.sum - go build -o build/hway ./cmd + go build -o build/hway . install: go.sum go install -mod=readonly ./cmd/hway diff --git a/app/app.go b/app/app.go index a015865..9e4c46d 100644 --- a/app/app.go +++ b/app/app.go @@ -1,12 +1,12 @@ // Package gateway provides the default routes for the Sonr hway. -package gateway +package app import ( "github.com/labstack/echo-contrib/echoprometheus" "github.com/labstack/echo/v4" echomiddleware "github.com/labstack/echo/v4/middleware" "github.com/onsonr/hway/gateway/handlers" - config "github.com/onsonr/hway/internal/config/hway" + config "github.com/onsonr/hway/internal/config" hwayorm "github.com/onsonr/hway/internal/models" "github.com/onsonr/hway/pkg/common" "github.com/onsonr/hway/pkg/context" diff --git a/cmd/main.go b/cmd/main.go deleted file mode 100644 index cdaca24..0000000 --- a/cmd/main.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "context" - _ "embed" - "fmt" - "os" - - "github.com/jackc/pgx/v5" - hwayorm "github.com/onsonr/hway/internal/models" -) - -// main is the entry point for the application -func main() { - cmd := rootCmd() - if err := cmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) - } - os.Exit(0) -} - -func loadEnvImplFromArgs(args []string) (config.Hway, error) { - cmd := rootCmd() - if err := cmd.ParseFlags(args); err != nil { - return nil, err - } - - env := &config.HwayImpl{ - ServePort: servePort, - ChainId: chainID, - IpfsGatewayUrl: ipfsGatewayURL, - SonrApiUrl: sonrAPIURL, - SonrGrpcUrl: sonrGrpcURL, - SonrRpcUrl: sonrRPCURL, - PsqlDSN: formatPsqlDSN(), - } - return env, nil -} - -func setupPostgresDB() (*hwayorm.Queries, error) { - pgdsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", psqlHost, psqlUser, psqlPass, psqlDB) - conn, err := pgx.Connect(context.Background(), pgdsn) - if err != nil { - return nil, err - } - return hwayorm.New(conn), nil -} diff --git a/cmd/cmds.go b/main.go similarity index 69% rename from cmd/cmds.go rename to main.go index 168aed3..3204be2 100644 --- a/cmd/cmds.go +++ b/main.go @@ -1,13 +1,17 @@ package main import ( + "database/sql" + _ "embed" "fmt" "log" "net/http" "os" - "strings" - "github.com/onsonr/hway/gateway" + "github.com/onsonr/hway/app" + "github.com/onsonr/hway/internal/config" + hwayorm "github.com/onsonr/hway/internal/models" + "github.com/onsonr/hway/pkg/common" "github.com/spf13/cobra" ) @@ -27,6 +31,16 @@ var ( psqlDB string // PostgresSQL Database Flag ) +// main is the entry point for the application +func main() { + cmd := rootCmd() + if err := cmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } + os.Exit(0) +} + func rootCmd() *cobra.Command { cmd := &cobra.Command{ Use: "hway", @@ -40,11 +54,11 @@ func rootCmd() *cobra.Command { if err != nil { panic(err) } - dbq, err := setupPostgresDB() + dbq, err := setupSQLiteDB() if err != nil { panic(err) } - e, err := gateway.New(env, ipc, dbq) + e, err := app.New(env, ipc, dbq) if err != nil { panic(err) } @@ -69,24 +83,27 @@ func rootCmd() *cobra.Command { return cmd } -func formatPsqlDSN() string { - if psqlHost == "" { - return "" +func loadEnvImplFromArgs(args []string) (config.Hway, error) { + cmd := rootCmd() + if err := cmd.ParseFlags(args); err != nil { + return nil, err } - host := psqlHost - port := "5432" - - if parts := strings.Split(psqlHost, ":"); len(parts) == 2 { - host = parts[0] - port = parts[1] + env := &config.HwayImpl{ + ServePort: servePort, + ChainId: chainID, + IpfsGatewayUrl: ipfsGatewayURL, + SonrApiUrl: sonrAPIURL, + SonrGrpcUrl: sonrGrpcURL, + SonrRpcUrl: sonrRPCURL, } - - dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=verify-full", - host, port, psqlUser, psqlPass, psqlDB) - - log.Printf("Attempting to connect to PostgreSQL with DSN: host=%s port=%s user=%s dbname=%s", - host, port, psqlUser, psqlDB) // Don't log the password - - return dsn + return env, nil +} + +func setupSQLiteDB() (*hwayorm.Queries, error) { + conn, err := sql.Open("sqlite3", ":memory:") + if err != nil { + return nil, err + } + return hwayorm.New(conn), nil } diff --git a/pkg/common/chain_query.go b/pkg/common/chain_query.go index 288f218..ae4409c 100644 --- a/pkg/common/chain_query.go +++ b/pkg/common/chain_query.go @@ -3,9 +3,9 @@ package common import ( bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" nodev1beta1 "github.com/cosmos/cosmos-sdk/client/grpc/node" - didv1 "github.com/onsonr/hway/api/did/v1" - dwnv1 "github.com/onsonr/hway/api/dwn/v1" - svcv1 "github.com/onsonr/hway/api/svc/v1" + didv1 "github.com/onsonr/sonr/api/did/v1" + dwnv1 "github.com/onsonr/sonr/api/dwn/v1" + svcv1 "github.com/onsonr/sonr/api/svc/v1" "google.golang.org/grpc" ) diff --git a/pkg/common/ucan_store.go b/pkg/common/ucan_store.go index 9acd282..ff166d3 100644 --- a/pkg/common/ucan_store.go +++ b/pkg/common/ucan_store.go @@ -8,8 +8,6 @@ import ( "github.com/golang-jwt/jwt" "github.com/ipfs/go-cid" - "github.com/onsonr/hway/internal/crypto/keys" - "github.com/onsonr/hway/internal/crypto/ucan" ) type IPFSTokenStore interface {