Skip to content

Commit

Permalink
infra: drop requirement of CGO
Browse files Browse the repository at this point in the history
Signed-off-by: Arnav Gupta <championswimmer@gmail.com>
  • Loading branch information
championswimmer committed Feb 9, 2024
1 parent 8671f78 commit d1a875f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.20-alpine AS builder

RUN apk add --no-cache make build-base
RUN apk add --no-cache make

# Move to working directory (/build).
WORKDIR /build
Expand All @@ -13,7 +13,7 @@ RUN go mod download
COPY . .

# Set necessary environment variables needed for our image and build the API server.
ENV CGO_ENABLED=1 GOOS=linux GOARCH=amd64
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
# RUN go build -ldflags="-s -w" -o onepixel ./src/main.go
RUN make build DOCS=false

Expand Down
32 changes: 20 additions & 12 deletions src/db/init.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package db

import (
"github.com/c0deltin/duckdb-driver/duckdb"
"gorm.io/driver/clickhouse"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"onepixel_backend/src/config"
"onepixel_backend/src/db/models"
"onepixel_backend/src/utils/applogger"
Expand Down Expand Up @@ -34,17 +30,29 @@ func getGormConfig() (dbConfig *gorm.Config) {
return
}

type DatabaseProvider func(dbUrl string, config *gorm.Config) *gorm.DB

var dbProviders map[string]DatabaseProvider = map[string]DatabaseProvider{}

func InjectDBProvider(name string, provider DatabaseProvider) {
dbProviders[name] = provider
}

func init() {
InjectDBProvider("postgres", ProvidePostgresDB)
InjectDBProvider("clickhouse", ProvideClickhouseDB)
}

func GetAppDB() (*gorm.DB, error) {

createAppDbOnce.Do(func() {
applogger.Warn("App: Creating db")
switch config.DBDialect {
case "sqlite":
applogger.Warn("App: Using sqlite db")
appDb = lo.Must(gorm.Open(sqlite.Open(config.DBUrl), getGormConfig()))
appDb = dbProviders["sqlite"](config.DBUrl, getGormConfig())
break
case "postgres":
applogger.Warn("App: Using postgres db")
appDb = lo.Must(gorm.Open(postgres.Open(config.DBUrl), getGormConfig()))
appDb = dbProviders["postgres"](config.DBUrl, getGormConfig())
break
default:
panic("Database config incorrect")
Expand All @@ -60,14 +68,14 @@ func GetAppDB() (*gorm.DB, error) {

func GetEventsDB() (*gorm.DB, error) {
createEventsDbOnce.Do(func() {
applogger.Warn("Events: Creating db")

switch config.EventDBDialect {
case "clickhouse":
applogger.Warn("Events: Using clickhouse db")
eventsDb = lo.Must(gorm.Open(clickhouse.Open(config.EventDBUrl), getGormConfig()))
eventsDb = dbProviders["clickhouse"](config.EventDBUrl, getGormConfig())
break
case "duckdb":
applogger.Warn("Events: Using duck db")
eventsDb = lo.Must(gorm.Open(duckdb.Open(config.EventDBUrl), getGormConfig()))
eventsDb = dbProviders["duckdb"](config.EventDBUrl, getGormConfig())
break
default:
panic("EventDB config incorrect")
Expand Down
19 changes: 19 additions & 0 deletions src/db/providers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package db

import (
"github.com/samber/lo"
"gorm.io/driver/clickhouse"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"onepixel_backend/src/utils/applogger"
)

func ProvidePostgresDB(dbUrl string, config *gorm.Config) *gorm.DB {
applogger.Warn("App: Using postgres db")
return lo.Must(gorm.Open(postgres.Open(dbUrl), config))
}

func ProvideClickhouseDB(dbUrl string, config *gorm.Config) *gorm.DB {
applogger.Warn("App: Using clickhouse db")
return lo.Must(gorm.Open(clickhouse.Open(dbUrl), config))
}
19 changes: 19 additions & 0 deletions tests/test_providers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tests

import (
"github.com/c0deltin/duckdb-driver/duckdb"
"github.com/samber/lo"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"onepixel_backend/src/utils/applogger"
)

func ProvideSqliteDB(dbUrl string, config *gorm.Config) *gorm.DB {
applogger.Warn("Test: Using sqlite db")
return lo.Must(gorm.Open(sqlite.Open(dbUrl), config))
}

func ProvideDuckDB(dbUrl string, config *gorm.Config) *gorm.DB {
applogger.Warn("Test: Using duckdb db")
return lo.Must(gorm.Open(duckdb.Open(dbUrl), config))
}
13 changes: 11 additions & 2 deletions tests/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ package tests
import (
"bytes"
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"io"
"net/http/httptest"
"onepixel_backend/src/config"
"onepixel_backend/src/db"
"onepixel_backend/src/dtos"
"onepixel_backend/src/server"
"testing"
)

var App = server.CreateAdminApp()
var MainApp = server.CreateMainApp()
var App *fiber.App
var MainApp *fiber.App

func init() {
db.InjectDBProvider("sqlite", ProvideSqliteDB)
db.InjectDBProvider("duckdb", ProvideDuckDB)
App = server.CreateAdminApp()
MainApp = server.CreateMainApp()
}

func TestUtil_CreateUser(t *testing.T, email string, password string) dtos.UserResponse {

Expand Down

0 comments on commit d1a875f

Please sign in to comment.