An awesome Golang API template to jumpstart your projects!
Explore the docs »
Report Bug
·
Request Feature
Xanny Go Template is a robust and scalable boilerplate for building RESTful APIs using Go (Golang). Designed with best practices and modern development in mind, this template serves as a starting point for backend projects, enabling developers to quickly set up and customize their API solutions.
- Modular Architecture: Organized with a clean directory structure, separating concerns for better maintainability and scalability.
- Layered Approach: Implements the handler-service-repository pattern to keep business logic, data access, and API handling separate.
- Gin Framework: Leverages the powerful and minimalist Gin framework for efficient request handling and routing.
- ORM Support: Uses GORM for database interactions, providing an easy and familiar way to handle models and migrations.
- Configuration Management: Centralized configuration management to simplify environment-specific settings using Viper.
- JWT Authentication: Secure authentication mechanism using JSON Web Tokens (JWT) for stateless and secure user sessions.
- Custom Error Handling: Centralized error management to ensure consistent and informative API responses.
- Auto-Migrations: Automated database migrations using a dedicated migration script for seamless schema updates.
- Makefile Automation: Includes a Makefile for common tasks such as running the application, building binaries, and executing migrations.
This template is ideal for developers looking for a ready-to-use, yet flexible structure for their Go backend projects. By providing essential features out-of-the-box, it allows you to focus on building core functionalities without worrying about boilerplate code.
Xanny Go Template is built using a combination of powerful tools and libraries that enable fast, scalable, and secure backend development. Here's a list of the technologies and frameworks used in this project:
To get a local copy of Xanny Go Template up and running on your machine, follow these simple steps.
Before you begin, ensure you have the following installed on your system:
- Go (Golang): Version 1.22.1 or higher. You can download it from Go's official website.
- Database: You need a running instance of a relational database (e.g., PostgreSQL, MySQL, or MSSQL). Make sure to set up the database and configure the connection.
- Make: Optionally, you can install Make for automating tasks like building, running migrations, etc.
-
Clone the repository: Open your terminal and clone the project to your local machine using Git.
git clone https://github.com/ramadiaz/profile-api.git cd profile-api -
Install dependencies: Ensure your Go modules are set up and all dependencies are installed.
go mod tidy
-
Configure environment variables: Set up your environment variables by creating a
.envfile in the root of the project. Here's an example:DB_HOST=your-db-host DB_PORT=your-db-port DB_USER=your-db-username DB_PASSWORD=your-db-password DB_NAME=xanny-db PORT=your-desired-port JWT_SECRET=your-jwt-secret-key ENVIRONMENT=development ADMIN_USERNAME=your-internal-admin-username ADMIN_PASSWORD=your-internal-admin-password
Replace the values with your own database connection details and secret key.
-
Run database migrations: The template comes with a migration script to set up the necessary database tables. You can run it using the following command:
make migrate
-
Run the application: To start the application, you can use the following command:
make run
This will start the server, and the API will be accessible at
http://localhost:<PORT>.
If you'd like to build the application binary for production use, you can run:
make buildThis will compile the application into a binary located in the bin/ folder. You can then execute the binary directly.
Once you have set up the project and run the application, you can access the default API endpoint to verify that the server is working correctly.
By default, the API exposes a single endpoint:
- GET
/api/ping
This endpoint will return a simple JSON response with a status of 200 and a message of "pong."
curl http://localhost:<PORT>/api/pingReplace <PORT> with the port number specified in your .env file.
{
"status": 200,
"message": "pong"
}This response indicates that the server is up and running. You can modify this route or add more routes as needed to expand the functionality of the API.
To customize the port, change the PORT value in the .env file:
PORT=your-desired-portThen, restart the application with:
make runThe API will now be accessible at http://localhost:<your-desired-port>/api/ping.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the Unlicense License. See LICENSE.txt for more information.
Rama Diaz - @ramadiazr - ramadiaz221@gmail.com
Project Link: https://github.com/ramadiaz/profile-api