Skip to content

Commit

Permalink
feat: add fly io deployment and CI (#8)
Browse files Browse the repository at this point in the history
* Add fly io deployment and CI
* fix: add tls options for grpc connections

---------

Co-authored-by: yse <[email protected]>
  • Loading branch information
roeierez and hydra-yse authored Dec 17, 2024
1 parent b5a2dee commit 1b1ab8e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 13 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/fly-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/

name: Fly Deploy
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
concurrency: deploy-group # optional: ensure only one action runs at a time
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ARG GO_VERSION=1
FROM golang:${GO_VERSION}-bookworm as builder

WORKDIR /usr/src/app
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /run-app .


FROM debian:bookworm

COPY --from=builder /run-app /usr/local/bin/
CMD ["run-app"]
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

type Config struct {
GrpcListenAddress string `env:"GRPC_LISTEN_ADDRESS"`
SQLiteDirPath string `env:"SQLITE_DIR_PATH"`
GrpcListenAddress string `env:"GRPC_LISTEN_ADDRESS,default=0.0.0.0:8080"`
SQLiteDirPath string `env:"SQLITE_DIR_PATH,default=db"`
PgDatabaseUrl string `env:"DATABASE_URL"`
}

Expand Down
36 changes: 36 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# fly.toml app configuration file generated for sync-service-wandering-morning-6267 on 2024-11-06T09:05:44+02:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'sync-service-wandering-morning-6267'
primary_region = 'lhr'

[build]
[build.args]
GO_VERSION = '1.22'

[env]
PORT = '8080'

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1

[[services]]
internal_port = 8080
protocol = "tcp"

[[services.ports]]
handlers = ["tls"]
port = 443
tls_options = { "alpn" = ["h2"] }
14 changes: 12 additions & 2 deletions store/postgres/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ package postgres
import (
"context"
"database/sql"
"embed"
"fmt"

"github.com/breez/data-sync/store"

"github.com/golang-migrate/migrate/v4"
pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/golang-migrate/migrate/v4/source/iofs"
_ "github.com/golang-migrate/migrate/v4/source/pkger"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)

//go:embed migrations/*.sql
var migrationFS embed.FS

type PgSyncStorage struct {
db *pgxpool.Pool
}
Expand All @@ -30,8 +35,13 @@ func NewPGSyncStorage(databaseURL string) (*PgSyncStorage, error) {
return nil, fmt.Errorf("failed to create migration driver %w", err)
}

m, err := migrate.NewWithDatabaseInstance(
"pkger:///store/postgres/migrations",
migrationDriver, err := iofs.New(migrationFS, "migrations")
if err != nil {
return nil, fmt.Errorf("failed to create migration driver %w", err)
}

m, err := migrate.NewWithInstance(
"iofs", migrationDriver,
"data-sync", driver)
if err != nil {
return nil, fmt.Errorf("failed to instantiate migrations %w", err)
Expand Down
15 changes: 11 additions & 4 deletions store/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package sqlite
import (
"context"
"database/sql"
"embed"
"errors"
"fmt"
"os"

"github.com/breez/data-sync/store"
"github.com/markbates/pkger"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/golang-migrate/migrate/v4/source/iofs"
_ "github.com/golang-migrate/migrate/v4/source/pkger"
_ "github.com/mattn/go-sqlite3"
)

var _ = pkger.Include("/store/migrations")
//go:embed migrations/*.sql
var migrationFS embed.FS

type SQLiteSyncStorage struct {
db *sql.DB
Expand All @@ -38,8 +40,13 @@ func NewSQLiteSyncStorage(file string) (*SQLiteSyncStorage, error) {
return nil, fmt.Errorf("failed to create migration driver %w", err)
}

m, err := migrate.NewWithDatabaseInstance(
"pkger:///store/sqlite/migrations",
migrationDriver, err := iofs.New(migrationFS, "migrations")
if err != nil {
return nil, fmt.Errorf("failed to create migration driver %w", err)
}

m, err := migrate.NewWithInstance(
"iofs", migrationDriver,
file, driver)
if err != nil {
return nil, fmt.Errorf("failed to instantiate migrations %w", err)
Expand Down
7 changes: 2 additions & 5 deletions syncer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ func NewPersistentSyncerServer(config *config.Config) (*PersistentSyncerServer,
var err error

if config.PgDatabaseUrl != "" {
log.Printf("creating postgres storage: %v\n", config.PgDatabaseUrl)
storage, err = postgres.NewPGSyncStorage(config.PgDatabaseUrl)
if err != nil {
return nil, err
}
} else {
// get sqlite file path and set default value if needed
sqliteFile := config.SQLiteDirPath
if sqliteFile == "" {
config.SQLiteDirPath = "db"
}
log.Printf("creating sqlite storage: %v\n", config.SQLiteDirPath)
if err := os.MkdirAll(config.SQLiteDirPath, 0700); err != nil {
return nil, fmt.Errorf("failed to create databases directory %w", err)
}
Expand Down

0 comments on commit 1b1ab8e

Please sign in to comment.