Embedded Replicas
This commit is contained in:
parent
359520fe83
commit
6a55017624
5 changed files with 36 additions and 9 deletions
14
Dockerfile
14
Dockerfile
|
@ -1,4 +1,5 @@
|
|||
FROM golang:1.23.2-alpine AS build
|
||||
# Build stage
|
||||
FROM golang:1.23.2 AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
|
@ -8,11 +9,16 @@ RUN go mod download
|
|||
|
||||
COPY . .
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o /go/bin/app
|
||||
# Note: CGO_ENABLED=1 is default, so we don't need to explicitly set it
|
||||
RUN go build -ldflags="-s -w" -o /go/bin/app
|
||||
|
||||
FROM gcr.io/distroless/static-debian11
|
||||
# Final stage
|
||||
FROM ubuntu:22.04
|
||||
|
||||
COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
# Install SSL certificates and required runtime libraries
|
||||
RUN apt-get update && apt-get install -y \
|
||||
ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=build /go/bin/app /app
|
||||
|
||||
|
|
4
fly.toml
4
fly.toml
|
@ -15,3 +15,7 @@ min_machines_running = 1
|
|||
|
||||
[[vm]]
|
||||
size = 'shared-cpu-1x'
|
||||
|
||||
[mounts]
|
||||
source = "himbot_data"
|
||||
destination = "/data"
|
||||
|
|
2
go.mod
2
go.mod
|
@ -7,6 +7,7 @@ require github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d
|
|||
require (
|
||||
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
|
||||
github.com/coder/websocket v1.8.12 // indirect
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 // indirect
|
||||
golang.org/x/crypto v0.28.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
|
||||
golang.org/x/sys v0.26.0 // indirect
|
||||
|
@ -16,4 +17,5 @@ require (
|
|||
github.com/bwmarrin/discordgo v0.28.1
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20241011135853-3effbb6dea5c
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -9,6 +9,10 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
|
|||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 h1:JLvn7D+wXjH9g4Jsjo+VqmzTUpl/LX7vfr6VOfSWTdM=
|
||||
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06/go.mod h1:FUkZ5OHjlGPjnM2UyGJz9TypXQFgYqw6AFNO1UiROTM=
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20241011135853-3effbb6dea5c h1:a8TrFzP+zK+uYcMWuLQoNOR78SG/yISSnHwMIcyWa2Q=
|
||||
github.com/tursodatabase/go-libsql v0.0.0-20241011135853-3effbb6dea5c/go.mod h1:TjsB2miB8RW2Sse8sdxzVTdeGlx74GloD5zJYUC38d8=
|
||||
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d h1:dOMI4+zEbDI37KGb0TI44GUAwxHF9cMsIoDTJ7UmgfU=
|
||||
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d/go.mod h1:l8xTsYB90uaVdMHXMCxKKLSgw5wLYBwBKKefNIUnm9s=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
|
|
21
lib/db.go
21
lib/db.go
|
@ -10,7 +10,7 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
_ "github.com/tursodatabase/libsql-client-go/libsql"
|
||||
"github.com/tursodatabase/go-libsql"
|
||||
)
|
||||
|
||||
var DBClient *sql.DB
|
||||
|
@ -23,14 +23,25 @@ func InitDB() error {
|
|||
return fmt.Errorf("database configuration missing")
|
||||
}
|
||||
|
||||
finalDBUrl := fmt.Sprintf("%s?authToken=%s", dbUrl, dbToken)
|
||||
// Determine DB path based on /data directory existence
|
||||
dbPath := "himbot.db" // default to local
|
||||
if _, err := os.Stat("/data"); !os.IsNotExist(err) {
|
||||
dbPath = "/data/himbot.db"
|
||||
}
|
||||
|
||||
client, clientError := sql.Open("libsql", finalDBUrl)
|
||||
replica, replicaError := libsql.NewEmbeddedReplicaConnector(
|
||||
dbPath,
|
||||
dbUrl,
|
||||
libsql.WithAuthToken(dbToken),
|
||||
)
|
||||
|
||||
if clientError != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open db %s: %s", dbUrl, clientError)
|
||||
if replicaError != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open db %s: %s", dbUrl, replicaError)
|
||||
os.Exit(1)
|
||||
}
|
||||
// finalDBUrl := fmt.Sprintf("%s?authToken=%s", dbUrl, dbToken)
|
||||
|
||||
client := sql.OpenDB(replica)
|
||||
|
||||
DBClient = client
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue