Skip to content

Commit

Permalink
调整项目结构
Browse files Browse the repository at this point in the history
  • Loading branch information
Chengka committed Nov 15, 2018
1 parent c10a8cc commit 8bab5e1
Show file tree
Hide file tree
Showing 547 changed files with 295,663 additions and 48 deletions.
6 changes: 3 additions & 3 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package api
import (
"encoding/json"
"fmt"
"gin_example/conf"
"gin_example/model"
"gin_example/serializer"
"gin_example/util"

"github.com/gin-gonic/gin"
validator "gopkg.in/go-playground/validator.v8"
Expand Down Expand Up @@ -33,8 +33,8 @@ func CurrentUser(c *gin.Context) *model.User {
func ErrorResponse(err error) serializer.Response {
if ve, ok := err.(validator.ValidationErrors); ok {
for _, e := range ve {
field := util.T(fmt.Sprintf("Field.%s", e.Field))
tag := util.T(fmt.Sprintf("Tag.Valid.%s", e.Tag))
field := conf.T(fmt.Sprintf("Field.%s", e.Field))
tag := conf.T(fmt.Sprintf("Tag.Valid.%s", e.Tag))
return serializer.Response{
Status: 40001,
Msg: fmt.Sprintf("%s%s", field, tag),
Expand Down
16 changes: 16 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package conf

import (
"github.com/joho/godotenv"
)

// Init 初始化配置项
func Init() {
// 从本地读取环境变量
godotenv.Load()

// 读取翻译文件
if err := LoadLocales("conf/locales/zh-cn.yaml"); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion util/internationalization.go → conf/i18n.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package conf

import (
"io/ioutil"
Expand Down
49 changes: 5 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,15 @@
package main

import (
"gin_example/api"
"gin_example/cache"
"gin_example/middleware"
"gin_example/model"
"gin_example/util"
"os"

"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"gin_example/conf"
"gin_example/server"
)

func main() {
godotenv.Load()

// 从配置文件读取配置
if err := util.LoadLocales("conf/locales/zh-cn.yaml"); err != nil {
panic(err)
}

r := gin.Default()

// 中间件, 顺序不能改
r.Use(model.Database(os.Getenv("MYSQL_DSN")))
r.Use(cache.Redis())
r.Use(middleware.Session(os.Getenv("SESSION_SECRET")))
r.Use(middleware.Cors())
r.Use(middleware.CurrentUser())

// 路由
v1 := r.Group("/api/v1")
{
v1.POST("ping", api.Ping)

// 用户登录
v1.POST("user/register", api.UserRegister)

// 用户登录
v1.POST("user/login", api.UserLogin)

// 需要登录保护的
v1.Use(middleware.AuthRequired())
{
// User Routing
v1.GET("user/me", api.UserMe)
v1.DELETE("user/logout", api.UserLogout)
}
}
conf.Init()

// 监听
// 装载路由
r := server.NewRouter()
r.Run(":3000")
}
44 changes: 44 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package server

import (
"gin_example/api"
"gin_example/cache"
"gin_example/middleware"
"gin_example/model"
"os"

"github.com/gin-gonic/gin"
)

// NewRouter 路由配置
func NewRouter() *gin.Engine {
r := gin.Default()

// 中间件, 顺序不能改
r.Use(model.Database(os.Getenv("MYSQL_DSN")))
r.Use(cache.Redis())
r.Use(middleware.Session(os.Getenv("SESSION_SECRET")))
r.Use(middleware.Cors())
r.Use(middleware.CurrentUser())

// 路由
v1 := r.Group("/api/v1")
{
v1.POST("ping", api.Ping)

// 用户登录
v1.POST("user/register", api.UserRegister)

// 用户登录
v1.POST("user/login", api.UserLogin)

// 需要登录保护的
v1.Use(middleware.AuthRequired())
{
// User Routing
v1.GET("user/me", api.UserMe)
v1.DELETE("user/logout", api.UserLogout)
}
}
return r
}
21 changes: 21 additions & 0 deletions vendor/github.com/gin-contrib/cors/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions vendor/github.com/gin-contrib/cors/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions vendor/github.com/gin-contrib/cors/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8bab5e1

Please sign in to comment.