feat: basic auth shit + db init
This commit is contained in:
parent
db49da5fd9
commit
d8caef7e5d
10 changed files with 261 additions and 69 deletions
41
main.go
41
main.go
|
@ -1,12 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"stereo.cat/backend/internal/api"
|
||||
"stereo.cat/backend/internal/auth"
|
||||
"stereo.cat/backend/internal/auth/client"
|
||||
"stereo.cat/backend/internal/types"
|
||||
)
|
||||
|
||||
|
@ -20,6 +26,8 @@ func getEnv(key, fallback string) string {
|
|||
func main() {
|
||||
_ = godotenv.Load()
|
||||
|
||||
databaseType := getEnv("DATABASE_TYPE", "sqlite")
|
||||
sqliteFile := getEnv("SQLITE_FILE", "stereo.db")
|
||||
imagePath := getEnv("IMAGE_PATH", os.TempDir())
|
||||
|
||||
if _, err := os.Stat(imagePath); err != nil {
|
||||
|
@ -33,8 +41,41 @@ func main() {
|
|||
c := types.StereoConfig{
|
||||
Router: gin.Default(),
|
||||
ImagePath: imagePath,
|
||||
Client: client.New(
|
||||
os.Getenv("REDIRECT_URI"),
|
||||
os.Getenv("CLIENT_ID"),
|
||||
os.Getenv("CLIENT_SECRET"),
|
||||
),
|
||||
}
|
||||
|
||||
switch databaseType {
|
||||
case "sqlite":
|
||||
db, err := gorm.Open(sqlite.Open(sqliteFile), &gorm.Config{})
|
||||
|
||||
c.Database = db
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
break
|
||||
|
||||
case "postgres":
|
||||
db, err := gorm.Open(postgres.Open(os.Getenv("POSTGRES_DSN")), &gorm.Config{})
|
||||
|
||||
c.Database = db
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
break
|
||||
default:
|
||||
panic(errors.New("Invalid database type was specified."))
|
||||
}
|
||||
|
||||
c.Database.AutoMigrate(&auth.User{})
|
||||
|
||||
api.Register(&c)
|
||||
|
||||
c.Router.Run()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue