I am batman
This commit is contained in:
115
internal/server/routes.go
Normal file
115
internal/server/routes.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"OpsMastery.v5/internal/handlers"
|
||||
"OpsMastery.v5/internal/middleware"
|
||||
"OpsMastery.v5/internal/webrtc_service"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (s *FiberServer) RegisterFiberRoutes(db *gorm.DB) {
|
||||
api := s.Group("/api/v1")
|
||||
|
||||
// Public routes (no authentication required)
|
||||
api.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.SendString("Server is running!")
|
||||
})
|
||||
// Authentication routes
|
||||
api.Post("/signup", handlers.SignUp)
|
||||
api.Post("/signin", handlers.SignIn)
|
||||
api.Get("/verify/:token", handlers.VerifyEmail)
|
||||
api.Post("/forgot-password", handlers.RequestPasswordReset)
|
||||
api.Post("/reset-password", handlers.ResetPassword)
|
||||
|
||||
// Public route to get a user's profile photo by ID
|
||||
api.Get("/users/:id/profile_photo", handlers.GetUserProfilePhoto)
|
||||
|
||||
// OAuth routes (move under /api/v1)
|
||||
api.Get("/auth/:provider", handlers.OAuthLogin)
|
||||
api.Get("/auth/:provider/callback", handlers.OAuthCallback)
|
||||
|
||||
// Protected routes (require authentication)
|
||||
protected := api.Group("")
|
||||
protected.Use(middleware.JWTMiddleware)
|
||||
|
||||
// Allow all authenticated users to list users
|
||||
protected.Get("/users", handlers.ListUsers)
|
||||
protected.Get("/users/search", handlers.SearchUsers)
|
||||
|
||||
// Admin routes
|
||||
protected.Delete("/users/:id", middleware.OnlyAdmin(db, handlers.DeleteUserByID))
|
||||
protected.Put("/users/:id/role", middleware.OnlyAdmin(db, handlers.SetUserRole))
|
||||
|
||||
// Moderator routes
|
||||
protected.Get("/users/:id", middleware.OnlyModerator(db, handlers.GetUserByID))
|
||||
protected.Put("/users/:id", middleware.OnlyModerator(db, handlers.UpdateUserByID))
|
||||
|
||||
// User routes
|
||||
protected.Get("/users/me", middleware.OnlyUser(db, handlers.GetCurrentUser))
|
||||
protected.Put("/users/me", middleware.OnlyUser(db, handlers.UpdateCurrentUser))
|
||||
|
||||
// Other protected routes
|
||||
protected.Post("/signout", handlers.SignOut)
|
||||
protected.Post("/auth/refresh", handlers.RefreshToken)
|
||||
|
||||
// Protected routes for tickets
|
||||
protected.Post("/ticket", handlers.CreateTicket)
|
||||
protected.Get("/tickets", handlers.ListTickets)
|
||||
protected.Get("/ticket/:id", handlers.GetTicketByID)
|
||||
protected.Put("/ticket/:id", handlers.UpdateTicketByID)
|
||||
protected.Delete("/ticket/:id", handlers.DeleteTicketByID)
|
||||
|
||||
// Protected routes for task sections
|
||||
protected.Post("/task-sections", handlers.CreateTaskSection)
|
||||
protected.Get("/task-sections", handlers.ListTaskSections)
|
||||
protected.Put("/task-section/:id", handlers.UpdateTaskSectionByID)
|
||||
protected.Delete("/task-section/:id", handlers.DeleteTaskSectionByID)
|
||||
|
||||
// Protected routes for tasks
|
||||
protected.Post("/tasks", handlers.CreateTask)
|
||||
protected.Get("/tasks", handlers.ListTasks)
|
||||
protected.Get("/task/:id", handlers.GetTaskByID)
|
||||
protected.Put("/task/:id", handlers.UpdateTaskByID)
|
||||
protected.Delete("/task/:id", handlers.DeleteTaskByID)
|
||||
|
||||
// Protected routes for chat
|
||||
protected.Get("/chats", handlers.GetChatHistory)
|
||||
protected.Post("/chats", handlers.CreateChat)
|
||||
protected.Post("/chats/:chatId/users", handlers.AddUsersToChat)
|
||||
protected.Get("/chats/user/:userId", handlers.GetChatsForUser)
|
||||
protected.Get("/chats/:chatId/messages", handlers.GetChatMessages)
|
||||
protected.Delete("/chats/:chatId", handlers.DeleteChat)
|
||||
|
||||
// WebRTC routes
|
||||
protected.Post("/webrtc/start", func(c *fiber.Ctx) error {
|
||||
go webrtc_service.StartSignalingServer()
|
||||
return c.JSON(fiber.Map{"status": "signaling server started"})
|
||||
})
|
||||
|
||||
// Chat WebSocket route
|
||||
// protected.Get("/chat/ws", websocket.New(func(c *websocket.Conn) {
|
||||
// token := c.Query("token")
|
||||
// if token == "" {
|
||||
// fmt.Println("WebSocket closed: missing token")
|
||||
// c.Close()
|
||||
// return
|
||||
// }
|
||||
// claims, err := utils.ValidateJWT(token, false)
|
||||
// if err != nil {
|
||||
// fmt.Println("WebSocket closed: invalid token:", err)
|
||||
// c.Close()
|
||||
// return
|
||||
// }
|
||||
// // Set user info as locals for use in HandleChat
|
||||
|
||||
// handlers.HandleChat(c, claims)
|
||||
// }))
|
||||
|
||||
api.Options("/chat/ws", func(c *fiber.Ctx) error {
|
||||
c.Set("Access-Control-Allow-Origin", "http://localhost:3000")
|
||||
c.Set("Access-Control-Allow-Credentials", "true")
|
||||
c.Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
})
|
||||
}
|
||||
39
internal/server/routes_test.go
Normal file
39
internal/server/routes_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package server
|
||||
|
||||
// import (
|
||||
// "github.com/gofiber/fiber/v2"
|
||||
// "io"
|
||||
// "net/http"
|
||||
// "testing"
|
||||
// )
|
||||
|
||||
// func TestHandler(t *testing.T) {
|
||||
// // Create a Fiber app for testing
|
||||
// app := fiber.New()
|
||||
// // Inject the Fiber app into the server
|
||||
// s := &FiberServer{App: app}
|
||||
// // Define a route in the Fiber app
|
||||
// app.Get("/", s.HelloWorldHandler)
|
||||
// // Create a test HTTP request
|
||||
// req, err := http.NewRequest("GET", "/", nil)
|
||||
// if err != nil {
|
||||
// t.Fatalf("error creating request. Err: %v", err)
|
||||
// }
|
||||
// // Perform the request
|
||||
// resp, err := app.Test(req)
|
||||
// if err != nil {
|
||||
// t.Fatalf("error making request to server. Err: %v", err)
|
||||
// }
|
||||
// // Your test assertions...
|
||||
// if resp.StatusCode != http.StatusOK {
|
||||
// t.Errorf("expected status OK; got %v", resp.Status)
|
||||
// }
|
||||
// expected := "{\"message\":\"Hello World\"}"
|
||||
// body, err := io.ReadAll(resp.Body)
|
||||
// if err != nil {
|
||||
// t.Fatalf("error reading response body. Err: %v", err)
|
||||
// }
|
||||
// if expected != string(body) {
|
||||
// t.Errorf("expected response body to be %v; got %v", expected, string(body))
|
||||
// }
|
||||
// }
|
||||
30
internal/server/server.go
Normal file
30
internal/server/server.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
)
|
||||
|
||||
type FiberServer struct {
|
||||
*fiber.App
|
||||
}
|
||||
|
||||
func New() *FiberServer {
|
||||
server := &FiberServer{
|
||||
App: fiber.New(fiber.Config{
|
||||
ServerHeader: "OpsMastery.v5",
|
||||
AppName: "OpsMastery.v5",
|
||||
}),
|
||||
}
|
||||
|
||||
// Correct CORS origin to allow your frontend domain
|
||||
server.App.Use(cors.New(cors.Config{
|
||||
AllowOrigins: "http://localhost:3000",
|
||||
AllowMethods: "GET,POST,PUT,DELETE,OPTIONS,PATCH",
|
||||
AllowHeaders: "Accept,Authorization,Content-Type",
|
||||
AllowCredentials: true, // allow credentials for cookies/auth
|
||||
MaxAge: 300,
|
||||
}))
|
||||
|
||||
return server
|
||||
}
|
||||
Reference in New Issue
Block a user