Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9826e9b
Initial commit
Zahnentferner Dec 3, 2024
463d7a2
Update README.md
keshav-nischal Dec 7, 2024
fb25239
basic and incomplete signup
keshav-nischal Dec 7, 2024
9894752
Merge pull request #1 from keshav-nischal/main
keshav-nischal Dec 7, 2024
835e63e
Authentication, Forgot Password, Integration with FE
sakshi-in-loops Dec 21, 2024
e53a827
Framework for POC & Auth update
sakshi-in-loops Jan 1, 2025
c5732c6
Bug Fixes & Transcript Addition
sakshi-in-loops Jan 21, 2025
0edf5fe
Frontend Refactoring
sakshi-in-loops Feb 2, 2025
c6e8b82
Merge pull request #31 from sakshi-in-loops/FERefactoring
keshav-nischal Feb 3, 2025
db76e22
refactored backend
Rishab87 Feb 9, 2025
1b3697b
git ignore in main folder
optmyzrKeshav Feb 26, 2025
db1408e
Merge pull request #1 from AOSSIE-Org/main
keshav-nischal Feb 26, 2025
fd95fd5
Merge pull request #42 from keshav-nischal/main
keshav-nischal Feb 26, 2025
22ff4cc
Merge pull request #35 from Rishab87/backend-refactor
keshav-nischal Feb 26, 2025
4676f2b
docs: new setup process
Naman-B-Parlecha Mar 2, 2025
416d1d6
Merge pull request #45 from Naman-B-Parlecha/doc/naman
keshav-nischal Mar 2, 2025
fb2c4ef
password hide-visible feature added
221fa04732 Mar 3, 2025
de5cf0d
Merge pull request #48 from 221fa04732/Feature-Request-Show-Hide-Pass…
keshav-nischal Mar 3, 2025
5a973b9
feat: enhance post-sign-in flow with profile and leaderboard UIs for …
rixitgithub Mar 27, 2025
5938dcb
chore: update config.prod.yml with descriptive placeholders
rixitgithub Mar 27, 2025
c0e167b
Merge remote-tracking branch 'origin/main'
rixitgithub Mar 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions backend/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,89 @@ import (
"strconv"

"arguehub/config"
"arguehub/db"
"arguehub/middlewares"
"arguehub/routes"
"arguehub/utils"
"arguehub/websocket"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

func main() {
// Load the configuration from the specified YAML file
cfg, err := config.LoadConfig("./config/config.prod.yml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}

router := setupRouter(cfg)
// Establish a connection to MongoDB using the URI from the configuration
if err := db.ConnectMongoDB(cfg.Database.URI); err != nil {
log.Fatalf("Failed to connect to MongoDB: %v", err)
}
log.Println("Connected to MongoDB")

// Populate initial debate-related data (custom data seeding utility function)
utils.SeedDebateData()
utils.PopulateTestUsers()

// Set up the Gin router and configure CORS, middleware, and routes
router := setupRouter(cfg)
port := strconv.Itoa(cfg.Server.Port)
log.Printf("Server starting on port %s", port)

// Start the server on the configured port
if err := router.Run(":" + port); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}

func setupRouter(cfg *config.Config) *gin.Engine {
// gin.SetMode(gin.ReleaseMode) // Uncomment this line for production

router := gin.Default()

// Set trusted proxies to prevent reverse proxy issues in certain deployment scenarios
router.SetTrustedProxies([]string{"127.0.0.1", "localhost"})

// Apply CORS policy to allow requests from the frontend (localhost:5173)
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:5173"},
AllowOrigins: []string{"http://localhost:5173"}, // Allow frontend on localhost
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))

// Handle preflight OPTIONS requests
router.OPTIONS("/*path", func(c *gin.Context) {
c.Status(204)
})

router.POST("/signup", routes.SignUpRouteHandler)
router.POST("/verifyEmail", routes.VerifyEmailRouteHandler)
router.POST("/login", routes.LoginRouteHandler)
router.POST("/forgotPassword", routes.ForgotPasswordRouteHandler)
router.POST("/confirmForgotPassword", routes.VerifyForgotPasswordRouteHandler)
router.POST("/verifyToken", routes.VerifyTokenRouteHandler)

// Public routes for authentication and user actions
router.POST("/signup", routes.SignUpRouteHandler) // Handle user signup
router.POST("/verifyEmail", routes.VerifyEmailRouteHandler) // Verify user email
router.POST("/login", routes.LoginRouteHandler) // Handle user login
router.POST("/forgotPassword", routes.ForgotPasswordRouteHandler) // Handle forgotten password requests
router.POST("/confirmForgotPassword", routes.VerifyForgotPasswordRouteHandler) // Verify password reset token
router.POST("/verifyToken", routes.VerifyTokenRouteHandler) // Verify token (JWT or other)

// WebSocket route for real-time communication
router.GET("/ws", websocket.WebsocketHandler)

// Protected routes requiring authentication (JWT validation)
auth := router.Group("/")
auth.Use(middlewares.AuthMiddleware("./config/config.prod.yml")) // Apply custom authentication middleware
{
// Profile management routes
auth.GET("/user/fetchprofile", routes.GetProfileRouteHandler) // Fetch user profile data
auth.PUT("/user/updateprofile", routes.UpdateProfileRouteHandler) // Update user profile

// Get leaderboard with user rankings based on debates
auth.GET("/leaderboard", routes.GetLeaderboardRouteHandler)

// Update ELO score after a debate (e.g., for leaderboard updates)
auth.POST("/debate/result", routes.UpdateEloAfterDebateRouteHandler)
}

return router
}
}
56 changes: 31 additions & 25 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,43 @@ package config

import (
"fmt"
"io/ioutil"
"io/ioutil"

"gopkg.in/yaml.v3"
"gopkg.in/yaml.v3"
)

type Config struct {
Server struct {
Port int `yaml:"port"`
} `yaml:"server"`

Cognito struct {
AppClientId string `yaml:"appClientId"`
AppClientSecret string `yaml:"appClientSecret"`
UserPoolId string `yaml:"userPoolId"`
Region string `yaml:"region"`
} `yaml:"cognito"`

Openai struct {
GptApiKey string `yaml:"gptApiKey"`
} `yaml:"openai`
Server struct {
Port int `yaml:"port"`
} `yaml:"server"`

Cognito struct {
AppClientId string `yaml:"appClientId"`
AppClientSecret string `yaml:"appClientSecret"`
UserPoolId string `yaml:"userPoolId"`
Region string `yaml:"region"`
} `yaml:"cognito"`

Openai struct {
GptApiKey string `yaml:"gptApiKey"`
} `yaml:"openai"`

Database struct {
URI string `yaml:"uri"`
} `yaml:"database"`
}

// LoadConfig reads the configuration file
func LoadConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}

var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
}

return &cfg, nil
return &cfg, nil
}
14 changes: 8 additions & 6 deletions backend/config/config.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ server:
port: 1313

cognito:
appClientId:
appClientSecret:
userPoolId:
region:
appClientId: <your-cognito-app-client-id>
appClientSecret: <your-cognito-app-client-secret>
userPoolId: <your-cognito-user-pool-id>
region: eu-north-1

openai:
gptApiKey:

gptApiKey: <your-openai-gpt-api-key>

database:
uri: "<your-mongodb-connection-string>"
Loading