Commit 34fef64d authored by Vista's avatar Vista
Browse files

first commit

parents
# IDE
.idea/
.vscode/
# BUILD
build/
*.exe
*.so
*.dll
# DOCS & CONFIG
docs/
conf.yaml
# SECRET
*.pem
# LOGS
*.log
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
# Golang Service Template
Golang back-end service template. Using this template, you can get started with back-end projects quickly.
| Web Framework | ORM | Database Driver | Configuration Manager | Log Manager | API Documentation |
|:----------------:|:------------:|:--------------------:|:---------------------:|:---------------:|:-------------------:|
| labstack/echo/v4 | gorm.io/gorm | gorm.io/driver/mysql | spf13/viper | sirupsen/logrus | swaggo/echo-swagger |
> forked from go-svc-tpl
## 使用
1. 该模板未建立 model 层,请自己设计表结构,然后完成 model 部分
- 注意,若非使用 `automigrate` 而是手动建表,请附上相应 sql 文档
2. controller 部分请遵循 api 文档,暂时不用管 swagger 部分
## Init
1. create `conf.yaml`
2. use `swag init` to create swagger docs
3. go build & run
package app
import (
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)
var e *echo.Echo
func InitWebFramework() {
e = echo.New()
e.HideBanner = true
addRoutes()
logrus.Info("echo framework initialized")
}
func StartServer() {
e.Logger.Fatal(e.Start(":1926"))
}
package app
import (
"github.com/labstack/echo/v4"
echoSwagger "github.com/swaggo/echo-swagger"
)
func ping(c echo.Context) error {
return c.String(200, "pong!")
}
func addRoutes() {
api := e.Group("api")
api.GET("/doc/*", echoSwagger.WrapHandler)
api.GET("/ping", ping)
}
module go-svc-tpl
go 1.17
require (
github.com/go-playground/validator/v10 v10.10.0
github.com/labstack/echo/v4 v4.6.3
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.10.1
github.com/swaggo/echo-swagger v1.2.0
gorm.io/driver/mysql v1.2.3
gorm.io/gorm v1.22.5
)
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2 // indirect
github.com/swaggo/swag v1.7.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.8 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
This diff is collapsed.
# Golang Service Template
Golang back-end service template. Using this template, you can get started with back-end projects quickly.
| Web Framework | ORM | Database Driver | Configuration Manager | Log Manager | API Documentation |
|:----------------:|:------------:|:--------------------:|:---------------------:|:---------------:|:-------------------:|
| labstack/echo/v4 | gorm.io/gorm | gorm.io/driver/mysql | spf13/viper | sirupsen/logrus | swaggo/echo-swagger |
> forked from go-svc-tpl
## 使用
1. 该模板未建立 model 层,请自己设计表结构,然后完成 model 部分
- 注意,若非使用 `automigrate` 而是手动建表,请附上相应 sql 文档
2. controller 部分请遵循 api 文档,暂时不用管 swagger 部分
## Init
1. create `conf.yaml`
2. use `swag init` to create swagger docs
3. go build & run
// @title Golang Service Template
// @version 0.1
// @description Golang back-end service template, get started with back-end projects quickly
// @BasePath /api
package main
import (
"go-svc-tpl/app"
_ "go-svc-tpl/docs"
"go-svc-tpl/model"
)
func main() {
model.Init()
app.InitWebFramework()
app.StartServer()
}
package model
// TODO: add crud interface here
package model
// TODO: add new model
package model
import (
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func Init() {
connectDatabase()
err := DB.AutoMigrate() // TODO: add table structs here
if err != nil {
logrus.Fatal(err)
}
}
func connectDatabase() {
viper.SetConfigName("conf")
viper.AddConfigPath("./")
if err := viper.ReadInConfig(); err != nil {
logrus.Panic(err)
}
loginInfo := viper.GetStringMapString("sql")
dbArgs := loginInfo["username"] + ":" + loginInfo["password"] +
"@(localhost)/" + loginInfo["db_name"] + "?charset=utf8mb4&parseTime=True&loc=Local"
var err error
DB, err = gorm.Open(mysql.Open(dbArgs), &gorm.Config{})
if err != nil {
logrus.Panic(err)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment