-
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.
- Loading branch information
Björn Urban
committed
Oct 1, 2023
1 parent
ccecf1a
commit e1d7506
Showing
5 changed files
with
195 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/B-Urb/KubeVoyage/internal/handlers" | ||
"github.com/B-Urb/KubeVoyage/internal/models" | ||
"github.com/rs/cors" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"log" | ||
"net/http" | ||
|
@@ -18,35 +14,19 @@ import ( | |
var db *gorm.DB | ||
|
||
func main() { | ||
// Read environment variables | ||
dbType := os.Getenv("DB_TYPE") | ||
dbHost := os.Getenv("DB_HOST") | ||
dbPort := os.Getenv("DB_PORT") | ||
dbUser := os.Getenv("DB_USER") | ||
dbPassword := os.Getenv("DB_PASSWORD") | ||
dbName := os.Getenv("DB_NAME") | ||
app, err := NewApp() | ||
if err != nil { | ||
log.Fatalf("Failed to initialize app: %v", err) | ||
} | ||
|
||
var dsn string | ||
var err error | ||
var db *gorm.DB | ||
app.Migrate() | ||
|
||
switch dbType { | ||
case "mysql": | ||
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPassword, dbHost, dbPort, dbName) | ||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
case "postgres": | ||
dsn = fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", dbHost, dbPort, dbUser, dbName, dbPassword) | ||
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
case "sqlite": | ||
dsn = dbName // For SQLite, dbName would be the path to the .db file | ||
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{}) | ||
default: | ||
log.Fatalf("Unsupported DB_TYPE: %s", dbType) | ||
} | ||
handler := setupServer(app) | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to connect to database: %v", err) | ||
} | ||
log.Println("Starting server on :8080") | ||
log.Fatal(http.ListenAndServe(":8080", handler)) | ||
} | ||
func setupServer(app *App) http.Handler { | ||
mux := http.NewServeMux() | ||
|
||
// Migrate the schema | ||
|
@@ -93,44 +73,11 @@ func main() { | |
mux.HandleFunc("/api/request", func(w http.ResponseWriter, r *http.Request) { | ||
handlers.HandleRequestSite(w, r, db) | ||
}) | ||
// Start the server on port 8081 | ||
log.Println("Starting server on :8080") | ||
|
||
log.Fatal(http.ListenAndServe(":8080", handler)) | ||
|
||
// ... setup your routes and start your server | ||
handler := cors.Default().Handler(mux) | ||
return handler | ||
} | ||
|
||
func isAPIRoute(path string) bool { | ||
return len(path) >= 4 && path[0:4] == "/api" | ||
} | ||
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) | ||
} | ||
} |
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/B-Urb/KubeVoyage/internal/models" | ||
"gorm.io/gorm" | ||
"log" | ||
"os" | ||
) | ||
|
||
type App struct { | ||
DB *gorm.DB | ||
JWTKey []byte | ||
BaseURL string | ||
} | ||
|
||
func NewApp() (*App, error) { | ||
db, err := 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, | ||
}, 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) | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package database | ||
|
||
import ( | ||
"fmt" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"os" | ||
) | ||
|
||
func InitializeDatabase() (*gorm.DB, error) { | ||
// Read environment variables | ||
dbType, err := getEnvOrError("DB_TYPE") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dbHost, err := getEnvOrError("DB_HOST") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dbPort, err := getEnvOrError("DB_PORT") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dbUser, err := getEnvOrError("DB_USER") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dbPassword, err := getEnvOrError("DB_PASSWORD") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dbName, err := getEnvOrError("DB_NAME") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var dsn string | ||
var db *gorm.DB | ||
|
||
switch dbType { | ||
case "mysql": | ||
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPassword, dbHost, dbPort, dbName) | ||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
case "postgres": | ||
dsn = fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", dbHost, dbPort, dbUser, dbName, dbPassword) | ||
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
case "sqlite": | ||
dsn = dbName // For SQLite, dbName would be the path to the .db file | ||
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{}) | ||
default: | ||
return nil, fmt.Errorf("Unsupported DB_TYPE: %s", dbType) | ||
} | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Failed to connect to database: %v", err) | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
func getEnvOrError(key string) (string, error) { | ||
value := os.Getenv(key) | ||
if value == "" { | ||
return "", fmt.Errorf("Environment variable %s not set", key) | ||
} | ||
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 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,37 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/B-Urb/KubeVoyage/internal/models" | ||
) | ||
|
||
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) | ||
} | ||
} |