Skip to content

Commit

Permalink
Merge pull request #2 from leomorpho/sqlite
Browse files Browse the repository at this point in the history
Add Sqlite support
  • Loading branch information
leomorpho authored Sep 18, 2024
2 parents d927561 + 73175ec commit 8b3d1ec
Show file tree
Hide file tree
Showing 14 changed files with 7,883 additions and 492 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ e2e_tests/test-results
data/mailpit.db-shm
data/mailpit.db-wal
data/mailpit.db
dbs

# Ignore all JavaScript and CSS files in the static folder and its subdirectories
/static/**/*.js
Expand Down Expand Up @@ -189,3 +190,4 @@ pwabuilder-android-wrapper/signing.keystore
bubblewrap


deploy-real.yml
22 changes: 17 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ FROM golang:1.22.2-bullseye AS builder
# Set the working directory inside the container
WORKDIR /app

# NOTE: Install necessary libraries for SQLite
RUN apt-get update && apt-get install -y \
gcc \
musl-dev \
sqlite3 \
libsqlite3-dev

# Copy Go modules and download dependencies
COPY go.mod go.sum ./
RUN go mod download
Expand All @@ -12,9 +19,9 @@ RUN go mod download
COPY . .

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -gcflags=all=-l -o /app/goship-web ./cmd/web/main.go
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -gcflags=all=-l -o /app/goship-worker ./cmd/worker/main.go
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -gcflags=all=-l -o /app/goship-seed ./cmd/seed/main.go
RUN CGO_ENABLED=1 GOOS=linux go build -ldflags="-s -w" -gcflags=all=-l -o /app/goship-web ./cmd/web/main.go
RUN CGO_ENABLED=1 GOOS=linux go build -ldflags="-s -w" -gcflags=all=-l -o /app/goship-worker ./cmd/worker/main.go
RUN CGO_ENABLED=1 GOOS=linux go build -ldflags="-s -w" -gcflags=all=-l -o /app/goship-seed ./cmd/seed/main.go

# Install asynq tools
RUN go install github.com/hibiken/asynq/tools/asynq@latest
Expand Down Expand Up @@ -49,8 +56,13 @@ RUN chmod +x /entrypoint.sh

COPY config/config.yaml .
COPY service-worker.js /service-worker.js
RUN mkdir pwabuilder-android-wrapper
COPY pwabuilder-android-wrapper/assetlinks.json pwabuilder-android-wrapper/assetlinks.json
COPY static /static

# Below is only used if you need to use PWABuilder to make a native Android app
# RUN mkdir pwabuilder-android-wrapper
# COPY pwabuilder-android-wrapper/assetlinks.json pwabuilder-android-wrapper/assetlinks.json

ENTRYPOINT ["/entrypoint.sh"]

# Clean up any unnecessary files
RUN apt-get purge -y gcc musl-dev libsqlite3-dev && apt-get autoremove -y && apt-get clean
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,8 @@ If you'd like a no-nonesense (or not too much?) starter kit to get your next pro
- **[S3](https://aws.amazon.com/s3/)** - Host files and images on any S3-compatible service (e.g., Backblaze).
- **Redis** - used for task queuing, caching, and SSE events.
- Currently making optional for single binary deployments
>>>>>>> main
## WIP Documentation

See [goship.run](http://goship.run). NOTE: it's currently being actively developed! Feel free to help ❤️.
90 changes: 44 additions & 46 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -11,8 +12,6 @@ import (

"github.com/mikestefanello/pagoda/pkg/routes"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/pkg/tasks"
"github.com/mikestefanello/pagoda/seeder"
)

func timeoutMiddleware(next http.Handler, writeTimeout time.Duration) http.Handler {
Expand Down Expand Up @@ -63,56 +62,55 @@ func main() {
}
}

if err := c.Web.StartServer(&srv); err != http.ErrServerClosed {
if err := c.Web.StartServer(&srv); errors.Is(err, http.ErrServerClosed) {
c.Web.Logger.Fatalf("shutting down the server: %v", err)
}
}()

// TODO: is it ensured that there is only ever a single scheduler at a time?
// Start the scheduler service to queue periodic tasks
go func() {
if err := c.Tasks.StartScheduler(); err != nil {
c.Web.Logger.Fatalf("scheduler shutdown: %v", err)
}
}()
// // Start the scheduler service to queue periodic tasks
// go func() {
// if err := c.Tasks.StartScheduler(); err != nil {
// c.Web.Logger.Fatalf("scheduler shutdown: %v", err)
// }
// }()

seeder.RunIdempotentSeeder(c.Config, c.ORM)
// seeder.RunIdempotentSeeder(c.Config, c.ORM)

// Start the scheduled tasks
if err := c.Tasks.
New(tasks.TypeDeactivateExpiredSubscriptions).
Periodic("@every 6h").
Timeout(120 * time.Second).
Retain(24 * time.Hour).
Save(); err != nil {
c.Web.Logger.Fatalf("failed to register scheduler task: %v", err)
}
if err := c.Tasks.
New(tasks.TypeDeleteStaleNotifications).
Periodic("@every 12h").
Timeout(120 * time.Second).
Retain(24 * time.Hour).
Save(); err != nil {
c.Web.Logger.Fatalf("failed to register scheduler task: %v", err)
}
// NOTE: we run the following task every 30 minutes, but it will check if the same notif type has
// not already been sent to profiles.
if err := c.Tasks.
New(tasks.TypeAllDailyConvoNotifications).
Periodic("@every 30m").
Timeout(120 * time.Second).
Retain(24 * time.Hour).
Save(); err != nil {
c.Web.Logger.Fatalf("failed to register scheduler task: %v", err)
}
if err := c.Tasks.
New(tasks.TypeEmailUpdates).
Periodic("@every 6h").
Timeout(30 * time.Minute).
Retain(48 * time.Hour).
Save(); err != nil {
c.Web.Logger.Fatalf("failed to run startup task: %v", err)
}
// // Start the scheduled tasks
// if err := c.Tasks.
// New(tasks.TypeDeactivateExpiredSubscriptions).
// Periodic("@every 6h").
// Timeout(120 * time.Second).
// Retain(24 * time.Hour).
// Save(); err != nil {
// c.Web.Logger.Fatalf("failed to register scheduler task: %v", err)
// }
// if err := c.Tasks.
// New(tasks.TypeDeleteStaleNotifications).
// Periodic("@every 12h").
// Timeout(120 * time.Second).
// Retain(24 * time.Hour).
// Save(); err != nil {
// c.Web.Logger.Fatalf("failed to register scheduler task: %v", err)
// }
// // NOTE: we run the following task every 30 minutes, but it will check if the same notif type has
// // not already been sent to profiles.
// if err := c.Tasks.
// New(tasks.TypeAllDailyConvoNotifications).
// Periodic("@every 30m").
// Timeout(120 * time.Second).
// Retain(24 * time.Hour).
// Save(); err != nil {
// c.Web.Logger.Fatalf("failed to register scheduler task: %v", err)
// }
// if err := c.Tasks.
// New(tasks.TypeEmailUpdates).
// Periodic("@every 6h").
// Timeout(30 * time.Minute).
// Retain(48 * time.Hour).
// Save(); err != nil {
// c.Web.Logger.Fatalf("failed to run startup task: %v", err)
// }

// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
quit := make(chan os.Signal, 1)
Expand Down
25 changes: 12 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (

type app string
type environment string
type dbmode string

const (
// EnvLocal represents the local environment
Expand All @@ -42,6 +43,12 @@ const (

// EnvProduction represents the production environment
EnvProduction environment = "prod"

// DBModeEmbedded represents an embedded DB being used as a storage backend
DBModeEmbedded dbmode = "embedded"

// DBModeStandalone represents a standalone DB as being used as a storage backend
DBModeStandalone dbmode = "standalone"
)

// SwitchEnvironment sets the environment variable used to dictate which environment the application is
Expand All @@ -62,7 +69,6 @@ type (
Database DatabaseConfig
Mail MailConfig
Phone PhoneConfig
ML MLConfig
Recommender RecommenderConfig
Storage StorageConfig
}
Expand Down Expand Up @@ -96,7 +102,6 @@ type (
}
EmailVerificationTokenExpiration time.Duration
OperationalConstants OperationalConstants
E3Kit E3Kit
PageSize int
VapidPublicKey string
VapidPrivateKey string
Expand Down Expand Up @@ -128,12 +133,6 @@ type (
MaxLikedQuestionHistoryFreePlan int
}

E3Kit struct {
AppId string
AppKeyId string
AppKey string
}

// CacheConfig stores the cache configuration
CacheConfig struct {
Hostname string
Expand All @@ -149,6 +148,11 @@ type (

// DatabaseConfig stores the database configuration
DatabaseConfig struct {
DbMode dbmode
EmbeddedDriver string
EmbeddedConnection string
EmbeddedTestConnection string
// TODO: eventually separate in-memory (SQLite) and standalone DB configs
Hostname string
Port uint16
User string
Expand Down Expand Up @@ -177,11 +181,6 @@ type (
ValidationCodeExpirationMinutes int
}

MLConfig struct {
WeaviateHost string
WeaviateScheme string
}

RecommenderConfig struct {
NumProfilesToMatchAtOnce int
}
Expand Down
8 changes: 6 additions & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
http:
hostname: "localhost"
port: 8000
domain: "http://localhost:8002"
domain: "http://localhost:8000"
readTimeout: "5s"
writeTimeout: "10s"
idleTimeout: "2m"
Expand All @@ -13,7 +13,7 @@ http:

app:
name: "GoShip"
supportEmail: "info@goship.app"
supportEmail: "info@goship.run"
environment: "local"
# Change this on any live environments
encryptionKey: "?E(G+KbPeShVmYq3t6w9z$C&F)J@McQf"
Expand Down Expand Up @@ -61,6 +61,10 @@ cache:
page: "0h"

database:
dbMode: "embedded"
embeddedDriver: "sqlite3"
embeddedConnection: "dbs/main.db?_journal=WAL&_timeout=5000&_fk=true"
embeddedTestConnection: ":memory:?_journal=WAL&_timeout=5000&_fk=true"
hostname: "localhost"
port: 5432
user: "admin"
Expand Down
Loading

0 comments on commit 8b3d1ec

Please sign in to comment.