Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. π« Status page with uptime monitoring & API monitoring as code π« openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
1.8 kB Β· 86 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687package database
import ( "database/sql" "fmt" "net/url" "os" "time"
"github.com/jmoiron/sqlx" _ "github.com/joho/godotenv/autoload" _ "github.com/tursodatabase/go-libsql")
const ( // Bound the pool so bursts of concurrent ingests can't open an unbounded // number of remote libSQL connections (see #2861). maxOpenConns = 16
maxLocalIdleConns = 16 localConnMaxIdleTime = time.Minute)
var ( dbUrl = os.Getenv("DB_URL") authToken = os.Getenv("DB_AUTH_TOKEN") dbInstance *sqlx.DB)
// New returns a database connection, reusing an existing connection if available.func New() *sqlx.DB { // Reuse Connection if dbInstance != nil { return dbInstance }
db, err := Open(dbUrl, authToken) if err != nil { fmt.Fprintf(os.Stderr, "failed to open db %s: %s", dbUrl, err) os.Exit(1) } dbInstance = db
return db}
// Open opens a libSQL database with a bounded connection pool.// dsn can be a remote URL (libsql://, http://, https://) or a local file: URL.func Open(dsn string, token string) (*sqlx.DB, error) { u, err := url.Parse(dsn) if err != nil { return nil, err } if token != "" { q := u.Query() q.Set("authToken", token) u.RawQuery = q.Encode() dsn = u.String() }
c, err := sql.Open("libsql", dsn) if err != nil { return nil, err } idleConns := maxLocalIdleConns switch u.Scheme { case "http", "https", "libsql": // Remote Hrana streams can expire before the pool's idle timeout. idleConns = 0 } c.SetMaxOpenConns(maxOpenConns) c.SetMaxIdleConns(idleConns) c.SetConnMaxIdleTime(localConnMaxIdleTime)
return sqlx.NewDb(c, "sqlite3"), nil}
// Close closes the database connection.func Close() error { if dbInstance != nil { err := dbInstance.Close() dbInstance = nil return err } return nil}