Switched from turso to local sqlite

This commit is contained in:
2025-02-13 22:23:16 +11:00
parent 62111df6c5
commit cab6be063f
6 changed files with 9 additions and 35 deletions

View File

@@ -20,8 +20,7 @@ type Config struct {
ReadHeaderTimeout time.Duration // Timeout for reading request headers in seconds ReadHeaderTimeout time.Duration // Timeout for reading request headers in seconds
WriteTimeout time.Duration // Timeout for writing requests in seconds WriteTimeout time.Duration // Timeout for writing requests in seconds
IdleTimeout time.Duration // Timeout for idle connections in seconds IdleTimeout time.Duration // Timeout for idle connections in seconds
TursoDBName string // DB Name for Turso DB/Branch DBName string // Filename of the db (doesnt include file extension)
TursoToken string // Bearer token for Turso DB/Branch
SecretKey string // Secret key for signing tokens SecretKey string // Secret key for signing tokens
AccessTokenExpiry int64 // Access token expiry in minutes AccessTokenExpiry int64 // Access token expiry in minutes
RefreshTokenExpiry int64 // Refresh token expiry in minutes RefreshTokenExpiry int64 // Refresh token expiry in minutes
@@ -35,7 +34,7 @@ type Config struct {
func GetConfig(args map[string]string) (*Config, error) { func GetConfig(args map[string]string) (*Config, error) {
err := godotenv.Load(".env") err := godotenv.Load(".env")
if err != nil { if err != nil {
fmt.Println(".env file not found.") fmt.Println(err)
} }
var ( var (
host string host string
@@ -83,13 +82,12 @@ func GetConfig(args map[string]string) (*Config, error) {
config := &Config{ config := &Config{
Host: host, Host: host,
Port: port, Port: port,
TrustedHost: os.Getenv("TRUSTED_HOST"), TrustedHost: GetEnvDefault("TRUSTED_HOST", "127.0.0.1"),
SSL: GetEnvBool("SSL_MODE", false), SSL: GetEnvBool("SSL_MODE", false),
ReadHeaderTimeout: GetEnvDur("READ_HEADER_TIMEOUT", 2), ReadHeaderTimeout: GetEnvDur("READ_HEADER_TIMEOUT", 2),
WriteTimeout: GetEnvDur("WRITE_TIMEOUT", 10), WriteTimeout: GetEnvDur("WRITE_TIMEOUT", 10),
IdleTimeout: GetEnvDur("IDLE_TIMEOUT", 120), IdleTimeout: GetEnvDur("IDLE_TIMEOUT", 120),
TursoDBName: os.Getenv("TURSO_DB_NAME"), DBName: GetEnvDefault("DB_NAME", "projectreshoot"),
TursoToken: os.Getenv("TURSO_AUTH_TOKEN"),
SecretKey: os.Getenv("SECRET_KEY"), SecretKey: os.Getenv("SECRET_KEY"),
AccessTokenExpiry: GetEnvInt64("ACCESS_TOKEN_EXPIRY", 5), AccessTokenExpiry: GetEnvInt64("ACCESS_TOKEN_EXPIRY", 5),
RefreshTokenExpiry: GetEnvInt64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day RefreshTokenExpiry: GetEnvInt64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day
@@ -99,15 +97,6 @@ func GetConfig(args map[string]string) (*Config, error) {
LogDir: GetEnvDefault("LOG_DIR", ""), LogDir: GetEnvDefault("LOG_DIR", ""),
} }
if config.TrustedHost == "" {
return nil, errors.New("Envar not set: TRUSTED_HOST")
}
if config.TursoDBName == "" {
return nil, errors.New("Envar not set: TURSO_DB_NAME")
}
if config.TursoToken == "" {
return nil, errors.New("Envar not set: TURSO_AUTH_TOKEN")
}
if config.SecretKey == "" { if config.SecretKey == "" {
return nil, errors.New("Envar not set: SECRET_KEY") return nil, errors.New("Envar not set: SECRET_KEY")
} }

View File

@@ -4,15 +4,15 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/pkg/errors" "github.com/pkg/errors"
_ "github.com/tursodatabase/libsql-client-go/libsql"
) )
// Returns a database connection handle for the Turso DB // Returns a database connection handle for the Turso DB
func ConnectToDatabase(primaryUrl *string, authToken *string) (*sql.DB, error) { func ConnectToDatabase(dbName string) (*sql.DB, error) {
url := fmt.Sprintf("libsql://%s.turso.io?authToken=%s", *primaryUrl, *authToken) file := fmt.Sprintf("file:%s.db", dbName)
db, err := sql.Open("sqlite3", file)
db, err := sql.Open("libsql", url)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "sql.Open") return nil, errors.Wrap(err, "sql.Open")
} }

4
go.mod
View File

@@ -11,18 +11,14 @@ require (
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0 github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.10.0
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d
golang.org/x/crypto v0.33.0 golang.org/x/crypto v0.33.0
) )
require ( require (
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/coder/websocket v1.8.12 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/sys v0.30.0 // indirect golang.org/x/sys v0.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

8
go.sum
View File

@@ -1,9 +1,5 @@
github.com/a-h/templ v0.3.833 h1:L/KOk/0VvVTBegtE0fp2RJQiBm7/52Zxv5fqlEHiQUU= github.com/a-h/templ v0.3.833 h1:L/KOk/0VvVTBegtE0fp2RJQiBm7/52Zxv5fqlEHiQUU=
github.com/a-h/templ v0.3.833/go.mod h1:cAu4AiZhtJfBjMY0HASlyzvkrtjnHWPeEsyGK2YYmfk= github.com/a-h/templ v0.3.833/go.mod h1:cAu4AiZhtJfBjMY0HASlyzvkrtjnHWPeEsyGK2YYmfk=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -34,12 +30,8 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus= golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@@ -56,7 +56,7 @@ func run(ctx context.Context, w io.Writer, args map[string]string) error {
return errors.Wrap(err, "logging.GetLogger") return errors.Wrap(err, "logging.GetLogger")
} }
conn, err := db.ConnectToDatabase(&config.TursoDBName, &config.TursoToken) conn, err := db.ConnectToDatabase(config.DBName)
if err != nil { if err != nil {
return errors.Wrap(err, "db.ConnectToDatabase") return errors.Wrap(err, "db.ConnectToDatabase")
} }

View File

@@ -8,9 +8,6 @@ import (
) )
func TestConfig() (*config.Config, error) { func TestConfig() (*config.Config, error) {
os.Setenv("TRUSTED_HOST", "127.0.0.1")
os.Setenv("TURSO_DB_NAME", ".")
os.Setenv("TURSO_AUTH_TOKEN", ".")
os.Setenv("SECRET_KEY", ".") os.Setenv("SECRET_KEY", ".")
cfg, err := config.GetConfig(map[string]string{}) cfg, err := config.GetConfig(map[string]string{})
if err != nil { if err != nil {