help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
go-rest-api
/
internal
/
routes
RSK World
go-rest-api
Go REST API - Enterprise-grade REST API with JWT Authentication + PostgreSQL + Redis Caching + Docker + Comprehensive Testing + Educational Design
routes
  • routes.go5.8 KB
routes.go
internal/routes/routes.go
Raw Download
Find: Go to:
/*
* Author: RSK World
* Email: help@rskworld.in / support@rskworld.in
* Website: https://rskworld.in/contact.php
* Year: 2026
*/

package routes

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/rskworld/go-rest-api/internal/config"
	"github.com/rskworld/go-rest-api/internal/handlers"
	"github.com/rskworld/go-rest-api/internal/middleware"

	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func SetupRouter(r *gin.Engine, cfg *config.Config) {
	// Global Middleware
	r.Use(gin.Recovery())
	r.Use(middleware.LoggerMiddleware())
	r.Use(middleware.CORSMiddleware())
	r.Use(middleware.RateLimitMiddleware())

	// Handlers
	authHandler := handlers.NewAuthHandler(cfg)
	apiHandler := handlers.NewAPIHandler()
	productHandler := handlers.NewProductHandler()
	categoryHandler := handlers.NewCategoryHandler()
	uploadHandler := handlers.NewUploadHandler(cfg)

	// Swagger configuration
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// Static file serving for uploads
	r.Static("/uploads", cfg.UploadPath)

	// API Versioning - V1 Routes
	v1 := r.Group("/api/v1")
	setupV1Routes(v1, cfg, authHandler, apiHandler, productHandler, categoryHandler, uploadHandler)

	// Legacy routes (redirect to v1 for backward compatibility)
	api := r.Group("/api")
	setupLegacyRoutes(api, cfg, authHandler, apiHandler, productHandler, categoryHandler, uploadHandler)
}

// V1 API Routes
func setupV1Routes(rg *gin.RouterGroup, cfg *config.Config, authHandler *handlers.AuthHandler, apiHandler *handlers.APIHandler, productHandler *handlers.ProductHandler, categoryHandler *handlers.CategoryHandler, uploadHandler *handlers.UploadHandler) {
	// Public Routes
	{
		rg.POST("/register", authHandler.Register)
		rg.POST("/login", authHandler.Login)
		rg.GET("/health", func(c *gin.Context) {
			c.JSON(200, gin.H{
				"status":  "ok",
				"message": "Service is healthy",
				"version": "v1",
				"author":  "RSK World",
			})
		})

		// Category Routes (Public for View - read-only)
		rg.GET("/categories", categoryHandler.GetCategories)
		rg.GET("/categories/:id", categoryHandler.GetCategory)

		// Product Routes (Public for View - read-only)
		rg.GET("/products", productHandler.GetProducts)
		rg.GET("/products/:id", productHandler.GetProduct)
	}

	// Protected Routes
	protected := rg.Group("/")
	protected.Use(middleware.AuthMiddleware(cfg))
	{
		protected.GET("/profile", apiHandler.GetUserProfile)

		// Admin-only Routes (Write operations)
		admin := protected.Group("/")
		admin.Use(middleware.RequireAdmin())
		{
			// Category management
			admin.POST("/categories", categoryHandler.CreateCategory)
			admin.PUT("/categories/:id", categoryHandler.UpdateCategory)
			admin.DELETE("/categories/:id", categoryHandler.DeleteCategory)

			// Product management
			admin.POST("/products", productHandler.CreateProduct)
			admin.PUT("/products/:id", productHandler.UpdateProduct)
			admin.DELETE("/products/:id", productHandler.DeleteProduct)

			// File upload management
			admin.POST("/products/:id/upload", uploadHandler.UploadProductImage)
			admin.DELETE("/products/:id/image", uploadHandler.DeleteProductImage)
		}
	}
}

// Legacy API Routes (for backward compatibility)
func setupLegacyRoutes(rg *gin.RouterGroup, cfg *config.Config, authHandler *handlers.AuthHandler, apiHandler *handlers.APIHandler, productHandler *handlers.ProductHandler, categoryHandler *handlers.CategoryHandler, uploadHandler *handlers.UploadHandler) {
	// Redirect legacy routes to v1
	rg.POST("/register", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/register") })
	rg.POST("/login", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/login") })
	rg.GET("/health", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/health") })

	// Category Routes
	rg.GET("/categories", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/categories") })
	rg.GET("/categories/:id", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/categories/"+c.Param("id")) })

	// Product Routes
	rg.GET("/products", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/products") })
	rg.GET("/products/:id", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/products/"+c.Param("id")) })

	// Protected routes - these need proper middleware setup
	protected := rg.Group("/")
	protected.Use(middleware.AuthMiddleware(cfg))
	{
		protected.GET("/profile", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/profile") })

		// Admin routes
		admin := protected.Group("/")
		admin.Use(middleware.RequireAdmin())
		{
			admin.POST("/categories", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/categories") })
			admin.PUT("/categories/:id", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/categories/"+c.Param("id")) })
			admin.DELETE("/categories/:id", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/categories/"+c.Param("id")) })

			admin.POST("/products", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/products") })
			admin.PUT("/products/:id", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/products/"+c.Param("id")) })
			admin.DELETE("/products/:id", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/products/"+c.Param("id")) })

			admin.POST("/products/:id/upload", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/products/"+c.Param("id")+"/upload") })
			admin.DELETE("/products/:id/image", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1/products/"+c.Param("id")+"/image") })
		}
	}
}
140 lines•5.8 KB
go

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer