Skip to content

Commit

Permalink
Merge pull request #1 from Andrewx24/newfeatures
Browse files Browse the repository at this point in the history
new branch
  • Loading branch information
Andrewx24 authored Jan 10, 2025
2 parents 67e5c90 + 718715c commit f853359
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
45 changes: 45 additions & 0 deletions backend/cmd/main.go
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)
}
}
29 changes: 29 additions & 0 deletions backend/internal/config/config.go
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
}
27 changes: 27 additions & 0 deletions backend/internal/database/database.go
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
}
27 changes: 27 additions & 0 deletions backend/internal/handlers/auth.go
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))
}
27 changes: 27 additions & 0 deletions backend/internal/models/user.go
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))
}

0 comments on commit f853359

Please sign in to comment.