-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend): more refactoring in backend packages
- Loading branch information
Björn Urban
committed
Oct 1, 2023
1 parent
d7a0639
commit ddbac29
Showing
8 changed files
with
106 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,30 @@ | ||
package main | ||
package application | ||
|
||
import ( | ||
"fmt" | ||
"github.com/B-Urb/KubeVoyage/internal/database" | ||
"github.com/B-Urb/KubeVoyage/internal/models" | ||
"gorm.io/gorm" | ||
"log" | ||
"os" | ||
) | ||
|
||
type App struct { | ||
DB *gorm.DB | ||
JWTKey []byte | ||
BaseURL string | ||
DB *gorm.DB | ||
} | ||
|
||
func NewApp() (*App, error) { | ||
db, err := initializeDatabase() | ||
db, err := database.InitializeDatabase() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to initialize database: %v", err) | ||
} | ||
|
||
jwtKey, err := getEnvOrError("JWT_SECRET_KEY") | ||
if err != nil { | ||
log.Fatalf("Error reading JWT_SECRET_KEY: %v", err) | ||
} | ||
|
||
baseURL, err := getEnvOrError("BASE_URL") | ||
if err != nil { | ||
log.Fatalf("Error reading BASE_URL: %v", err) | ||
} | ||
|
||
return &App{ | ||
DB: db, | ||
JWTKey: []byte(jwtKey), | ||
BaseURL: baseURL, | ||
DB: db, | ||
}, nil | ||
} | ||
|
||
func (app *App) Migrate() { | ||
app.DB.AutoMigrate(&models.User{}, &models.Site{}, &models.UserSite{}) | ||
} | ||
|
||
func getEnvOrError(key string) (string, error) { | ||
value := os.Getenv(key) | ||
if value == "" { | ||
return "", fmt.Errorf("environment variable %s not set", key) | ||
err := app.DB.AutoMigrate(models.User{}, models.Site{}, models.UserSite{}) | ||
if err != nil { | ||
return | ||
} | ||
return value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package test | ||
|
||
//func generateTestData() { | ||
// // Insert test data for Users | ||
// users := []models.User{ | ||
// {Email: "[email protected]", Password: "password1", Role: "admin"}, | ||
// {Email: "[email protected]", Password: "password2", Role: "user"}, | ||
// {Email: "[email protected]", Password: "password3", Role: "user"}, | ||
// } | ||
// for _, user := range users { | ||
// db.Create(&user) | ||
// } | ||
// | ||
// // Insert test data for Sites | ||
// sites := []models.Site{ | ||
// {URL: "https://site1.com"}, | ||
// {URL: "https://site2.com"}, | ||
// {URL: "https://site3.com"}, | ||
// } | ||
// for _, site := range sites { | ||
// db.Create(&site) | ||
// } | ||
// | ||
// // Insert test data for UserSite | ||
// userSites := []models.UserSite{ | ||
// {UserID: 1, SiteID: 1, State: "authorized"}, | ||
// {UserID: 2, SiteID: 2, State: "requested"}, | ||
// {UserID: 3, SiteID: 3, State: "authorized"}, | ||
// } | ||
// for _, userSite := range userSites { | ||
// db.Create(&userSite) | ||
// } | ||
//} |
Oops, something went wrong.