-
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.
Merge pull request #1 from Andrewx24/newfeatures
new branch
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"github.com/gin-gonic/gin" | ||
"github.com/joho/godotenv" | ||
"your-username/business-dashboard/internal/config" | ||
"your-username/business-dashboard/internal/database" | ||
"your-username/business-dashboard/internal/handlers" | ||
) | ||
|
||
func main() { | ||
if err := godotenv.Load(); err != nil { | ||
log.Println("No .env file found") | ||
} | ||
|
||
config := config.LoadConfig() | ||
db := database.InitDB(config) | ||
defer db.Close() | ||
|
||
r := gin.Default() | ||
|
||
// CORS configuration | ||
r.Use(cors.New(cors.Config{ | ||
AllowOrigins: []string{os.Getenv("FRONTEND_URL")}, | ||
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, | ||
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"}, | ||
AllowCredentials: true, | ||
})) | ||
|
||
// Routes | ||
api := r.Group("/api") | ||
handlers.SetupRoutes(api, db) | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
|
||
log.Printf("Server starting on port %s", port) | ||
if err := r.Run(":" + port); err != nil { | ||
log.Fatal("Server failed to start:", err) | ||
} | ||
} |
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,29 @@ | ||
package config | ||
|
||
type Config struct { | ||
DBHost string | ||
DBPort string | ||
DBUser string | ||
DBPassword string | ||
DBName string | ||
JWTSecret string | ||
} | ||
|
||
func LoadConfig() *Config { | ||
return &Config{ | ||
DBHost: getEnv("DB_HOST", "localhost"), | ||
DBPort: getEnv("DB_PORT", "5432"), | ||
DBUser: getEnv("DB_USER", "postgres"), | ||
DBPassword: getEnv("DB_PASSWORD", ""), | ||
DBName: getEnv("DB_NAME", "business_dashboard"), | ||
JWTSecret: getEnv("JWT_SECRET", "your-secret-key"), | ||
} | ||
} | ||
|
||
func getEnv(key, defaultValue string) string { | ||
value := os.Getenv(key) | ||
if value == "" { | ||
return defaultValue | ||
} | ||
return value | ||
} |
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,27 @@ | ||
package database | ||
|
||
import ( | ||
"fmt" | ||
"gorm.io/gorm" | ||
"gorm.io/driver/postgres" | ||
"your-username/business-dashboard/internal/config" | ||
"your-username/business-dashboard/internal/models" | ||
) | ||
|
||
func InitDB(config *config.Config) *gorm.DB { | ||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable", | ||
config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.DBPort) | ||
|
||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
panic("Failed to connect to database: " + err.Error()) | ||
} | ||
|
||
// Auto-migrate models | ||
err = db.AutoMigrate(&models.User{}) | ||
if err != nil { | ||
panic("Failed to migrate database: " + err.Error()) | ||
} | ||
|
||
return db | ||
} |
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,27 @@ | ||
package models | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type User struct { | ||
gorm.Model | ||
Email string `gorm:"uniqueIndex;not null" json:"email"` | ||
Password string `json:"-"` | ||
Name string `json:"name"` | ||
Role string `json:"role"` | ||
} | ||
|
||
func (u *User) BeforeCreate(tx *gorm.DB) error { | ||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return err | ||
} | ||
u.Password = string(hashedPassword) | ||
return nil | ||
} | ||
|
||
func (u *User) ComparePassword(password string) error { | ||
return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) | ||
} |
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,27 @@ | ||
package models | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type User struct { | ||
gorm.Model | ||
Email string `gorm:"uniqueIndex;not null" json:"email"` | ||
Password string `json:"-"` | ||
Name string `json:"name"` | ||
Role string `json:"role"` | ||
} | ||
|
||
func (u *User) BeforeCreate(tx *gorm.DB) error { | ||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return err | ||
} | ||
u.Password = string(hashedPassword) | ||
return nil | ||
} | ||
|
||
func (u *User) ComparePassword(password string) error { | ||
return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) | ||
} |