Go-gin is a starter for Gin Gonic, featuring Zerolog, Viper, Gorm, JWT, Go-Cache, rate limiting, cron scheduling, notifications, utility packages, and more.
If you want to develop with this project, you can follow the steps below.
-
Clone the repository and navigate to the project directory.
git clone git@github.com:funnyzak/go-gin.git && cd go-gin
-
Run the application.
go run cmd/main.go # or make dev
Note: The application will load the configuration from the config.yaml
file in the root directory by default. If you want to use a different configuration file, you can copy config.example.yaml
to prod.yaml
and update the values. specify it using the -c
parameter, for example: go run cmd/main.go -c prod
, it will load the prod.yaml
configuration file.
You can fork this repository and add Secrets Keys: DOCKER_USERNAME
and DOCKER_PASSWORD
in the repository settings. And when you push the code, it will automatically build binary and docker image and push to the Docker Hub.
├── Dockerfile // Dockerfile defines how to build a Docker image for the project
├── Makefile // Contains commands for building, running, testing, etc. the project
├── cmd
│ ├── main.go // The main entry point for the application
│ └── srv // Server controller
├── config.example.yaml // An example configuration file for the project
├── docker-compose.yml // Defines services, networks and volumes for docker-compose
├── internal
│ ├── gconfig // Internal package for configuration
│ └── gogin // Internal package for the gin framework
├── mappers
│ ├── auth.go // Data mapper for authentication
│ └── post.go // Data mapper for posts
├── model
│ ├── auth.go // Data model for authentication
│ ├── common.go // Common data models
│ ├── post.go // Data model for posts
│ └── user.go // Data model for users
├── pkg
│ ├── logger // Package for logging
│ ├── mygin // Custom package for the gin framework
│ └── utils // Utility packages
├── resource
│ ├── resource.go // Resource management
│ ├── static // Static files such as HTML, CSS, JavaScript, images
│ └── template // Templates for Gin framework
├── script
│ └── build.sh // A script for building the project
└── service
└── singleton // Singleton services for the application
Service configuration via yaml
format. The configuration file is located in the root directory of the project and is named config.example.yaml
. You can copy this file to config.yaml
and update the values. More details can be found in the config.example.yaml file.
# Compile multiple platforms architecture (Linux, Windows, MacOS)
make build
# Compile the specified platform architecture
GOOS=linux GOARCH=amd64 go build -o go-gin-linux-amd64 cmd/main.go
# Compile current platform architecture
go build -o go-gin cmd/main.go
You can deploy the application using the following ways.
Docker deployment requires the installation of Docker first, and then execute the command.
Start the service with default configuration parameters, as follows:
docker run -d \
--name go-gin \
--restart always \
-p 8080:8080 \
-v ./config.yaml:/app/config.yaml \
funnyzak/go-gin:latest
# Pull source code
git clone git@github.com:funnyzak/go-gin.git && cd go-gin
# Compose startup, startup parameter configuration can be done by modifying the docker-compose.yml file
docker compose up -d
If you need to update the container, you can re-pull the code and build the image in the source code folder, the command is as follows:
git pull && docker compose up -d --build
You can pull the source code to compile the binary executable file yourself, or download the binary executable file of the corresponding system architecture from the repository, and then execute the following command to start (note that go-gin is the name of the binary executable file, please replace it according to the actual name):
# Quick start (The config.yaml file needs to be in the same directory as the binary executable file)
./go-gin
# Specify the configuration file. eg. prod, the configuration file is prod.yaml
./go-gin -c prod
# View help, see available parameters
./go-gin -h
Note: Please make sure that executable permissions have been set before running. If there are no executable permissions, you can set them through the chmod +x go-gin
command.
In Linux, services are managed through Systemd. You can use the following commands to install, start, stop, restart, log, and view the status of services, etc.
bash -c "$(curl -fsSL https://raw.githubusercontent.com/funnyzak/go-gin/main/script/install.sh)"
You can also install it manually. The specific steps are as follows:
Click to expand
-
Download the binary executable file of the corresponding system architecture from the releases page or GitHub Actions page, and copy it to the
/opt/go-gin
directory. -
Grant the executable permission to the file by running the following command:
sudo chmod +x /opt/go-gin/go-gin
-
Download go-gin.service file to the
/etc/systemd/system
directory. -
Download config.example.yaml file to the
/opt/go-gin
directory and rename it togo-gin.yaml
, and update the values.
Finally, run the following command to start the service:
sudo systemctl enable go-gin
systemctl start go-gin
Service on MacOS is based on launchd. You can use the following steps to install and start the service.
-
Download the binary executable file of the corresponding system architecture from the releases page or GitHub Actions page, and copy it to the
/opt/go-gin
directory. -
Grant the executable permission to the file by running the following command:
sudo chmod +x /opt/go-gin/go-gin
-
Download com.go-gin.plist file to the
/Library/LaunchDaemons
directory. -
Download config.example.yaml file to the
/opt/go-gin
directory and rename it toconfig.yaml
, and update the values.
Finally, run the following command to start the service:
sudo launchctl load /Library/LaunchDaemons/com.go-gin.plist
sudo launchctl start /Library/LaunchDaemons/com.go-gin.plist
Service on Windows can be implemented using Task Scheduler. You can use the following steps to install and start the service.
- Dwnload the binary executable file of the corresponding system architecture from the releases page or GitHub Actions page, and unzip it to the
C:\go-gin
directory. - Download install.ps1 file to the
C:\go-gin
directory and rename it togo-gin.ps1
. - Download config.example.yaml file to the
C:\go-gin
directory and rename it toconfig.yaml
, and update the values.
Finally, run the following command to start the service:
cd C:\go-gin
.\go-gin.ps1 enable
The following are all the commands supported by the script:
./go-gin.ps1 enable # Enable and start the service
./go-gin.ps1 disable # Disable and stop the service
./go-gin.ps1 start # Start the service
./go-gin.ps1 stop # Stop the service
./go-gin.ps1 restart # Restart the service
./go-gin.ps1 status # View the service status