feat: Dockerize the application

This commit is contained in:
Prad Nukala 2025-01-04 19:52:54 -05:00
parent 466b34b5bd
commit 6e3cee596f
7 changed files with 48 additions and 78 deletions

3
Dockerfile Normal file
View File

@ -0,0 +1,3 @@
FROM scratch
ENTRYPOINT ["/hway"]
COPY hway /

View File

@ -6,12 +6,12 @@ BINDIR ?= $(GOPATH)/bin
# for dockerized protobuf tools # for dockerized protobuf tools
DOCKER := $(shell which docker) DOCKER := $(shell which docker)
HTTPS_GIT := github.com/onsonr/hway.git HTTPS_GIT := github.com/onsonr/hway.git
export RELEASE_DATE="$(date +%Y).$(date +%V).$(date +%u)"
all: install test all: install test
build: go.sum build: go.sum
go build -o build/hway ./cmd go build -o build/hway .
install: go.sum install: go.sum
go install -mod=readonly ./cmd/hway go install -mod=readonly ./cmd/hway

View File

@ -1,12 +1,12 @@
// Package gateway provides the default routes for the Sonr hway. // Package gateway provides the default routes for the Sonr hway.
package gateway package app
import ( import (
"github.com/labstack/echo-contrib/echoprometheus" "github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
echomiddleware "github.com/labstack/echo/v4/middleware" echomiddleware "github.com/labstack/echo/v4/middleware"
"github.com/onsonr/hway/gateway/handlers" "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" hwayorm "github.com/onsonr/hway/internal/models"
"github.com/onsonr/hway/pkg/common" "github.com/onsonr/hway/pkg/common"
"github.com/onsonr/hway/pkg/context" "github.com/onsonr/hway/pkg/context"

View File

@ -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
}

View File

@ -1,13 +1,17 @@
package main package main
import ( import (
"database/sql"
_ "embed"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os" "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" "github.com/spf13/cobra"
) )
@ -27,6 +31,16 @@ var (
psqlDB string // PostgresSQL Database Flag 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 { func rootCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "hway", Use: "hway",
@ -40,11 +54,11 @@ func rootCmd() *cobra.Command {
if err != nil { if err != nil {
panic(err) panic(err)
} }
dbq, err := setupPostgresDB() dbq, err := setupSQLiteDB()
if err != nil { if err != nil {
panic(err) panic(err)
} }
e, err := gateway.New(env, ipc, dbq) e, err := app.New(env, ipc, dbq)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -69,24 +83,27 @@ func rootCmd() *cobra.Command {
return cmd return cmd
} }
func formatPsqlDSN() string { func loadEnvImplFromArgs(args []string) (config.Hway, error) {
if psqlHost == "" { cmd := rootCmd()
return "" if err := cmd.ParseFlags(args); err != nil {
return nil, err
} }
host := psqlHost env := &config.HwayImpl{
port := "5432" ServePort: servePort,
ChainId: chainID,
if parts := strings.Split(psqlHost, ":"); len(parts) == 2 { IpfsGatewayUrl: ipfsGatewayURL,
host = parts[0] SonrApiUrl: sonrAPIURL,
port = parts[1] SonrGrpcUrl: sonrGrpcURL,
SonrRpcUrl: sonrRPCURL,
} }
return env, nil
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=verify-full", }
host, port, psqlUser, psqlPass, psqlDB)
func setupSQLiteDB() (*hwayorm.Queries, error) {
log.Printf("Attempting to connect to PostgreSQL with DSN: host=%s port=%s user=%s dbname=%s", conn, err := sql.Open("sqlite3", ":memory:")
host, port, psqlUser, psqlDB) // Don't log the password if err != nil {
return nil, err
return dsn }
return hwayorm.New(conn), nil
} }

View File

@ -3,9 +3,9 @@ package common
import ( import (
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
nodev1beta1 "github.com/cosmos/cosmos-sdk/client/grpc/node" nodev1beta1 "github.com/cosmos/cosmos-sdk/client/grpc/node"
didv1 "github.com/onsonr/hway/api/did/v1" didv1 "github.com/onsonr/sonr/api/did/v1"
dwnv1 "github.com/onsonr/hway/api/dwn/v1" dwnv1 "github.com/onsonr/sonr/api/dwn/v1"
svcv1 "github.com/onsonr/hway/api/svc/v1" svcv1 "github.com/onsonr/sonr/api/svc/v1"
"google.golang.org/grpc" "google.golang.org/grpc"
) )

View File

@ -8,8 +8,6 @@ import (
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/onsonr/hway/internal/crypto/keys"
"github.com/onsonr/hway/internal/crypto/ucan"
) )
type IPFSTokenStore interface { type IPFSTokenStore interface {