A RESTful API built with Go for managing products, categories, sales, and user authentication. This project provides a complete product management solution with JWT-based authentication, PostgreSQL database integration, and Swagger documentation.
- User Authentication: JWT-based authentication with registration, login, logout, and token refresh
- Product Management: Full CRUD operations for products with category association
- Category Management: Complete category management system
- Sales Tracking: Sales recording with product associations
- Database Integration: PostgreSQL with GORM ORM for automatic migrations
- API Documentation: Interactive Swagger UI documentation
- Middleware Support: Authentication middleware and CORS support
- Secure: Password hashing and JWT token validation
The productmanagerapi is structured to provide a modular and scalable approach to product management through a RESTful API. The project is organized into several key directories:
cmd/: Contains the main application entry point and Swagger documentationconfig/: Database configuration and application settingscontrollers/: HTTP request handlers for all API endpointsmodels/: GORM data models (User, Product, Category, Sale, SaleProduct)responseFormatter/: Standardized API response formattingroutes/: API endpoint routing configurationservices/: Business logic layer for data operationstypes/: Custom type definitionsutils/: Utility functions including authentication middleware
- Language: Go 1.23.5
- Database: PostgreSQL with GORM ORM
- Authentication: JWT (JSON Web Tokens)
- Documentation: Swagger/OpenAPI
- HTTP Routing: Native Go HTTP multiplexer
The application uses the following data models:
- ID, Username, Password, Email, Role
- Supports user authentication and role-based access
- ID, Name, Description
- Used to organize products into categories
- ID, Name, Description, Price, Stock, CategoryID
- Core product information with stock tracking
- ID, Products (SaleProduct array), Total
- Represents a complete sale transaction
- ID, SaleID, ProductID, Quantity, Total
- Junction table for many-to-many relationship between sales and products
- Go 1.16 or higher (Go 1.23.5 recommended)
- PostgreSQL database
- Git
-
Clone the repository:
git clone https://github.com/Bakemono-san/productmanagerapi.git cd productmanagerapi -
Set up PostgreSQL database:
Create a PostgreSQL database named
product_managementand update the connection string inconfig/dbconfig.goif needed:const dsn = "host=localhost user=bakemono password=bakemono dbname=product_management port=5432 sslmode=disable"
-
Download dependencies:
go mod download
-
Run the application:
go run cmd/main.go
The server will start on
http://localhost:2002
The application automatically creates the required database tables on startup using GORM's AutoMigrate feature for the following models:
- Users
- Categories
- Products
- Sales
- SaleProducts
This command starts the API server.
The API provides endpoints for managing products, categories, sales, and user authentication. Most endpoints require JWT authentication (except auth endpoints and Swagger docs).
- POST
/auth/register- Register a new user - POST
/auth/login- User login - POST
/refresh-token- Refresh JWT token - POST
/logout- User logout
- GET
/products- Retrieve all products - GET
/product?id={id}- Retrieve a specific product by ID - POST
/create-product- Create a new product - PUT
/update-product- Update an existing product - DELETE
/delete-product- Delete a product
- GET
/categories- Retrieve all categories - GET
/category?id={id}- Retrieve a specific category by ID - POST
/create-category- Create a new category - PUT
/update-category- Update an existing category - DELETE
/delete-category- Delete a category
- GET
/sales- Retrieve all sales - POST
/create-sale- Create a new sale - DELETE
/delete-sale- Delete a sale
- GET
/swagger/*- Swagger UI documentation
All endpoints (except auth and swagger) require JWT authentication via the Authorization header:
Authorization: Bearer <your-jwt-token>The application uses PostgreSQL as the database. Configuration is managed in config/dbconfig.go:
const dsn = "host=localhost user=bakemono password=bakemono dbname=product_management port=5432 sslmode=disable"Update this connection string according to your PostgreSQL setup:
- host: PostgreSQL server host (default: localhost)
- user: Database username
- password: Database password
- dbname: Database name (default: product_management)
- port: PostgreSQL port (default: 5432)
JWT secret key is configured in config/dbconfig.go:
var SECRET_KEY = "BAKEMONO_SECRET"Important: Change this to a secure secret key in production.
The API server runs on port 2002 by default. This can be modified in cmd/main.go.
First, register a user and login to get a JWT token:
# Register a new user
curl -X POST http://localhost:2002/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","email":"test@example.com","password":"testpass","role":"admin"}'
# Login to get JWT token
curl -X POST http://localhost:2002/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"testpass"}'Then use the token to access protected endpoints:
# Get all products (replace YOUR_TOKEN with actual token)
curl -X GET http://localhost:2002/products \
-H "Authorization: Bearer YOUR_TOKEN"
# Create a category
curl -X POST http://localhost:2002/create-category \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"Electronics","description":"Electronic products"}'
# Create a product
curl -X POST http://localhost:2002/create-product \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"Laptop","description":"Gaming laptop","price":999.99,"stock":10,"categoryId":1}'Access the interactive API documentation at:
http://localhost:2002/swagger/index.htmlImport the API endpoints into Postman and set up Bearer token authentication in the Authorization tab.
productmanagerapi/
βββ cmd/
β βββ main.go # Application entry point
β βββ docs/ # Swagger documentation files
βββ config/
β βββ dbconfig.go # Database and app configuration
βββ controllers/
β βββ authController.go # Authentication endpoints
β βββ categoryController.go # Category CRUD operations
β βββ productController.go # Product CRUD operations
β βββ saleController.go # Sales management
βββ models/
β βββ model.go # GORM data models
βββ routes/
β βββ routes.go # API route definitions
βββ services/
β βββ authService.go # Authentication business logic
β βββ categoryService.go # Category business logic
β βββ productService.go # Product business logic
β βββ salesService.go # Sales business logic
βββ types/
β βββ types.go # Custom type definitions
βββ utils/
β βββ utils.go # Utility functions and middleware
βββ responseFormatter/
βββ responseFormatter.go # API response formatting
gorm.io/gorm- ORM for database operationsgorm.io/driver/postgres- PostgreSQL driver for GORMgithub.com/golang-jwt/jwt/v5- JWT implementationgithub.com/swaggo/swag- Swagger documentation generator
We welcome contributions to the Product Manager API! Please see our CONTRIBUTING.md file for detailed guidelines on how to contribute to this project.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes following our coding standards
- Test your changes thoroughly
- Commit using conventional commits:
git commit -m "feat: add amazing feature" - Push to your fork:
git push origin feature/amazing-feature - Open a Pull Request
- π Bug fixes
- β¨ New features
- π Documentation improvements
- π§ͺ Tests
- π¨ Code quality improvements
For detailed information about our development process, coding standards, and pull request guidelines, please read our Contributing Guide.
Thank you for contributing to Product Manager API! π
This project is licensed under the MIT License.
For questions or support, please open an issue in the repository or contact the maintainer directly.