goweb-structure is a predefined directory structure for building web applications using the Golang Gin web framework. This project aims to provide a standardized and organized approach to structuring your Golang web projects, making it easier to maintain and scale your applications.
The directory structure follows a modular approach, separating different components of the application into their respective directories. Here is an overview of the predefined structure:
.
├── cmd
│ └── main.go
├── config
│ └── config.go
├── controllers
│ └── ...
├── middlewares
│ └── ...
├── models
│ └── ...
├── services
│ └── ...
├── web
│ ├── static
│ │ ├── css
│ │ │ └── ...
│ │ └── js
│ │ └── ...
│ └── templates
│ └── ...
└── utils
└── ...
The cmd directory contains the entry point of the application, main.go. This is where you initialize the Gin router and any other application-specific configurations.
The config directory is used to store configuration files or modules related to your application's settings. It typically includes database configurations, environment variables, and other application-specific settings.
The controllers directory houses the handlers for different routes in your application. Each controller file represents a logical grouping of related routes and their corresponding handlers.
The middlewares directory contains custom middleware functions that can be applied to specific routes or groups of routes. These middleware functions can be used for authentication, logging, error handling, etc.
The models directory is used to define your application's data models or structures. These models represent the entities in your application and provide methods for interacting with the underlying data storage.
The services directory houses the business logic of your application. It contains the implementation of various services that your application provides, such as user management, email sending, file handling, etc.
The static directory is used to store static files like CSS, JavaScript, images, etc., that are served by your application.
The templates directory contains the HTML templates used for rendering dynamic content in your application. These templates can be rendered using a templating engine like html/template or any other engine of your choice.
To use this predefined directory structure for your Golang Gin web project, follow these steps:
- Clone this repository or copy the contents of the goweb-structure directory to your project's root directory.
- Update the cmd/main.go file with your application-specific configurations and routes.
- Customize and add your controllers, middlewares, models, repositories, services, static files, templates, and utility functions as per your application's requirements.
- Change module name as per your choice.
- Run go program:
go run cmd/main.go
- Visit localhost:8080 OR localhost:8080/ping to check whether it is working or not.
OR
You can do it shortly:
mkdir config/ controllers/ db/ cmd/ middlewares/ models/ server/ services/ web/
Make sure to refer to the official documentation of the Golang Gin web framework (https://github.com/gin-gonic/gin) for more information on how to use Gin and its features.
If you have any suggestions or improvements for this predefined directory structure, feel free to open an issue or submit a pull request. Your contributions are highly appreciated!
This project is licensed under the MIT License.