Sets up internal packages for database and api
This commit is contained in:
parent
b059fa25cb
commit
d82a1c9215
4 changed files with 15 additions and 15 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
|
|
@ -8,7 +9,7 @@ import (
|
|||
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
||||
)
|
||||
|
||||
func Setup(app *fiber.App) error {
|
||||
func Setup(app *fiber.App, db *sql.DB) error {
|
||||
apiKey := os.Getenv("API_KEY")
|
||||
|
||||
if apiKey == "" {
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
)
|
||||
|
||||
var DB *sqlx.DB
|
||||
|
||||
//go:embed migrations/*
|
||||
var dbMigrations embed.FS
|
||||
|
||||
func Connect(filename string) error {
|
||||
DB, err := sqlx.Open("sqlite3", filename)
|
||||
func Connect(filename string) (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite3", filename)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
migrations := migrate.EmbedFileSystemMigrationSource{
|
||||
|
|
@ -26,13 +24,13 @@ func Connect(filename string) error {
|
|||
Root: "migrations",
|
||||
}
|
||||
|
||||
n, err := migrate.Exec(DB.DB, "sqlite3", migrations, migrate.Up)
|
||||
n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("Applied %d migrations - Database is ready!\n", n)
|
||||
|
||||
return nil
|
||||
return db, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
-- +migrate Up
|
||||
CREATE table users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name text NOT NULL
|
||||
first_name text NOT NULL,
|
||||
email text NOT NULL
|
||||
);
|
||||
|
||||
-- +migrate Down
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue