Skip to content

Commit

Permalink
chore: change file name & add config example & README modify
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayheaven committed Jan 11, 2024
1 parent 9f2a049 commit 6b6783e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
go.work

# local config
configs/config.yaml
config.yaml
tmp
logs
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<h1 align="center" >Go Gin Boilerplate</h1>
# Go Gin Boilerplate

<div align="center">
<div>

一个基于 Gin 框架的开发脚手架,旨在帮助开发者快速搭建和开发 Web 应用程序
<img src="./assets/golang.png" align="right"/>

![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/sanjayheaven/go-gin-boilerplate)
![CI](https://github.com/sanjayheaven/go-gin-boilerplate/workflows/Go/badge.svg)
![License](https://img.shields.io/github/license/sanjayheaven/go-gin-boilerplate)

[Swagger 接口](https://google.com)


</div>

## 项目介绍

一个基于 Gin 框架的开发脚手架,旨在帮助开发者快速搭建和开发 Web 应用程序。

## 关键词

[Go](https://github.com/golang/go)
Expand All @@ -25,22 +29,17 @@

## 特性

- **模块化架构**: 遵循 project-layout 规范, 结构清晰,便于扩展和维护。
- **强大的中间件支持**: 整合常用中间件,轻松实现日志、认证等功能。
- **数据库集成**: 提供简单的接口和 ORM 工具,方便与数据库交互。
- **日志记录**: 集成日志记录功能,方便跟踪应用程序运行状态。
- **快速开发**: 使用 Gin 框架和相关工具,加速项目的开发和迭代过程。
- **简单易用**: 遵循 [project-layout](https://github.com/golang-standards/project-layout/tree/master) 规范, 提供清晰简单的代码结构,使新手也能轻松上手。
- **一体化日志系统**: 集成 Logrus 和 Lumberjack,实现全方位的日志记录和管理。
- **热重载**: 使用 Air 工具,支持热重载,提高开发效率。
- **数据库支持**: 集成 Gorm,支持主流数据库,如 MySQL、PostgreSQL 等。
- **灵活的中间件**: 整合常用中间件,轻松实现日志、认证、跨域等功能。

## 快速开始

### 拉取项目

```sh
git clone https://github.com/sanjayheaven/go-gin-boilerplate.git
```

### 进入目录,安装依赖

```sh
cd go-gin-boilerplate
go mod download
```
Expand Down
Binary file added assets/golang.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 11 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ import (
"syscall"
"time"

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

func main() {

// init routes
r, err := routes.Init()
if err != nil {
panic(err)
}
r.Use(gin.Recovery())

// init logger
Logger.Init()
logger := Logger.Logger

// load env config
EnvConfig := configs.LoadConfig()

// init routes
r, err := routes.Init()
if err != nil {
panic(err)
}

// init swagger
swagger.SwaggerInfo.BasePath = "/"
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
Expand All @@ -47,6 +49,8 @@ func main() {
Handler: r,
}

logger.Printf("👻 Server is now listening at port: %s\n", EnvConfig.Server.Port)

go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("server listen error: %s\n", err)
Expand All @@ -63,10 +67,6 @@ func main() {
if err := server.Shutdown(ctx); err != nil {
logger.Fatalf("server shutdown error: %s\n", err)
}
logger.Println("Server exiting")

<-ctx.Done()
logger.Println("timeout of 5 seconds.")
// catching ctx.Done(). timeout of 5 seconds.

logger.Printf("👻 Server is now listening at port: %s\n", EnvConfig.Server.Port)
}
11 changes: 11 additions & 0 deletions configs/config.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# HERE IS JUST THE CONFIGURATION EXAMPLE FOR THE APP
# DO NOT USING THIS FILE AS YOUR CONFIGURATION DIRECTLY
# PLEASE COPY THIS FILE TO configs/config.yaml AND MODIFY IT

# server
server:
port: ":8080"

# mysql config
mysql:
dsn: "root:123456@tcp(127.0.0.1:3306)/beta?charset=utf8mb4&parseTime=True&loc=Local"
2 changes: 1 addition & 1 deletion configs/env.go → configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func LoadConfig() *Config {

viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(path)
viper.AddConfigPath(path + "/configs")

if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
Expand Down
2 changes: 1 addition & 1 deletion internal/middlewares/errorHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ErrorHandle(c *gin.Context) {

// return error response
c.JSON(http.StatusInternalServerError, gin.H{
"code": 500,
"code": http.StatusInternalServerError,
"message": "Internal Server Error",
})

Expand Down
File renamed without changes.

0 comments on commit 6b6783e

Please sign in to comment.