mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-22 23:02:15 +08:00
初始化项目
This commit is contained in:
commit
cff88d102f
48
.air.toml
Normal file
48
.air.toml
Normal file
@ -0,0 +1,48 @@
|
||||
# 工作目录
|
||||
# 使用 . 或绝对路径,请注意 `tmp_dir` 目录必须在 `root` 目录下
|
||||
root = "."
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
# 只需要写你平常编译使用的shell命令。你也可以使用 `make`
|
||||
# Windows平台示例: cmd = "go build -o tmp\main.exe ."
|
||||
cmd = "go build -o ./tmp/main ."
|
||||
|
||||
# 由`cmd`命令得到的二进制文件名
|
||||
# Windows平台示例:bin = "tmp\main.exe"
|
||||
bin = "tmp/main"
|
||||
|
||||
# 自定义执行程序的命令,可以添加额外的编译标识例如添加 GIN_MODE=release
|
||||
# Windows平台示例:full_bin = "tmp\main.exe"
|
||||
#full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
|
||||
full_bin = "tmp/main
|
||||
|
||||
# 监听以下文件扩展名的文件.
|
||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||
# 忽略这些文件扩展名或目录
|
||||
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
|
||||
# 监听以下指定目录的文件
|
||||
include_dir = []
|
||||
# 排除以下文件
|
||||
exclude_file = []
|
||||
# 如果文件更改过于频繁,则没有必要在每次更改时都触发构建。可以设置触发构建的延迟时间
|
||||
delay = 1000 # ms
|
||||
# 发生构建错误时,停止运行旧的二进制文件。
|
||||
stop_on_error = true
|
||||
# air的日志文件名,该日志文件放置在你的`tmp_dir`中
|
||||
log = "air_errors.log"
|
||||
|
||||
[log]
|
||||
# 显示日志时间
|
||||
time = true
|
||||
|
||||
[color]
|
||||
# 自定义每个部分显示的颜色。如果找不到颜色,使用原始的应用程序日志。
|
||||
main = "magenta"
|
||||
watcher = "cyan"
|
||||
build = "yellow"
|
||||
runner = "green"
|
||||
|
||||
[misc]
|
||||
# 退出时删除tmp目录
|
||||
clean_on_exit = true
|
91
.gitignore
vendored
Normal file
91
.gitignore
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
### JetBrains template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### Go template
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
etc
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
9
.idea/gofiber-study.iml
Normal file
9
.idea/gofiber-study.iml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/gofiber-study.iml" filepath="$PROJECT_DIR$/.idea/gofiber-study.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
14
README.MD
Normal file
14
README.MD
Normal file
@ -0,0 +1,14 @@
|
||||
## 使用 gofiber 作为 http 服务框架
|
||||
|
||||
## air 热重载
|
||||
|
||||
安装
|
||||
```shell
|
||||
go install github.com/cosmtrek/air@latest
|
||||
```
|
||||
配置文件 runner.conf
|
||||
|
||||
运行项目
|
||||
```shell
|
||||
air
|
||||
```
|
41
common/errorx/errorx.go
Normal file
41
common/errorx/errorx.go
Normal file
@ -0,0 +1,41 @@
|
||||
package errorx
|
||||
|
||||
import "gofiber.study.skcks.cn/common/response"
|
||||
|
||||
type CodeError struct {
|
||||
*response.Response
|
||||
}
|
||||
|
||||
type CodeErrorResponse struct {
|
||||
*response.Response
|
||||
}
|
||||
|
||||
func NewCustomError(code int, data interface{}, msg string) error {
|
||||
return &CodeError{
|
||||
Response: response.NewCustomResponse(code, data, msg),
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorWithCode(code int, msg string) error {
|
||||
return &CodeError{
|
||||
Response: response.NewCustomResponse(code, nil, msg),
|
||||
}
|
||||
}
|
||||
|
||||
func NewDefaultError(msg string) error {
|
||||
return NewErrorWithCode(response.ERROR, msg)
|
||||
}
|
||||
|
||||
func (e *CodeError) Error() string {
|
||||
return e.Msg
|
||||
}
|
||||
|
||||
func (e *CodeError) Resp() *CodeErrorResponse {
|
||||
return &CodeErrorResponse{
|
||||
Response: &response.Response{
|
||||
Code: e.Code,
|
||||
Data: e.Data,
|
||||
Msg: e.Msg,
|
||||
},
|
||||
}
|
||||
}
|
40
common/response/response.go
Normal file
40
common/response/response.go
Normal file
@ -0,0 +1,40 @@
|
||||
package response
|
||||
|
||||
type Response struct {
|
||||
Code Code `json:"code"`
|
||||
Data interface{} `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type Code = int
|
||||
|
||||
const (
|
||||
SUCCESS = 200
|
||||
UNAUTHORIZED = 401
|
||||
FORBIDDEN = 403
|
||||
ERROR = 500
|
||||
)
|
||||
|
||||
func NewCustomResponse(code int, data interface{}, msg string) *Response {
|
||||
return &Response{
|
||||
Code: code,
|
||||
Data: data,
|
||||
Msg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func NewResponseWithCode(code int, data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: SUCCESS,
|
||||
Data: data,
|
||||
Msg: "OK",
|
||||
}
|
||||
}
|
||||
|
||||
func NewResponse(data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: SUCCESS,
|
||||
Data: data,
|
||||
Msg: "OK",
|
||||
}
|
||||
}
|
19
go.mod
Normal file
19
go.mod
Normal file
@ -0,0 +1,19 @@
|
||||
module gofiber.study.skcks.cn
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/gofiber/fiber/v2 v2.38.1
|
||||
go.uber.org/zap v1.23.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||
github.com/klauspost/compress v1.15.0 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasthttp v1.40.0 // indirect
|
||||
github.com/valyala/tcplisten v1.0.0 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
|
||||
)
|
45
go.sum
Normal file
45
go.sum
Normal file
@ -0,0 +1,45 @@
|
||||
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
|
||||
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gofiber/fiber/v2 v2.38.1 h1:GEQ/Yt3Wsf2a30iTqtLXlBYJZso0JXPovt/tmj5H9jU=
|
||||
github.com/gofiber/fiber/v2 v2.38.1/go.mod h1:t0NlbaXzuGH7I+7M4paE848fNWInZ7mfxI/Er1fTth8=
|
||||
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
|
||||
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.40.0 h1:CRq/00MfruPGFLTQKY8b+8SfdK60TxNztjRMnH0t1Yc=
|
||||
github.com/valyala/fasthttp v1.40.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
|
||||
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
|
||||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
|
||||
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
47
logger/logger.go
Normal file
47
logger/logger.go
Normal file
@ -0,0 +1,47 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"os"
|
||||
)
|
||||
|
||||
var logger *zap.Logger
|
||||
|
||||
func init() {
|
||||
encoder := zapcore.NewConsoleEncoder(DefaultEncoderConfig())
|
||||
multiWriteSyncer := zapcore.NewMultiWriteSyncer(DefaultConsoleSyncer())
|
||||
core := zapcore.NewCore(encoder, multiWriteSyncer, zapcore.DebugLevel)
|
||||
logger = zap.New(core, zap.AddCaller())
|
||||
defer func(logger *zap.Logger) {
|
||||
_ = logger.Sync()
|
||||
}(logger)
|
||||
}
|
||||
|
||||
func DefaultTimeEncoder() (timeEncoder zapcore.TimeEncoder) {
|
||||
timeEncoder = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
|
||||
return
|
||||
}
|
||||
|
||||
func DefaultEncoderConfig() (encoderConfig zapcore.EncoderConfig) {
|
||||
encoderConfig = zap.NewProductionEncoderConfig()
|
||||
encoderConfig.EncodeTime = DefaultTimeEncoder()
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
return
|
||||
}
|
||||
|
||||
func DefaultConsoleSyncer() zapcore.WriteSyncer {
|
||||
return zapcore.AddSync(os.Stdout)
|
||||
}
|
||||
|
||||
func Logger() *zap.SugaredLogger {
|
||||
sugarLogger := logger.Sugar()
|
||||
defer func(sugarLogger *zap.SugaredLogger) {
|
||||
err := sugarLogger.Sync()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(sugarLogger)
|
||||
|
||||
return sugarLogger
|
||||
}
|
55
main.go
Normal file
55
main.go
Normal file
@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gofiber.study.skcks.cn/common/errorx"
|
||||
"gofiber.study.skcks.cn/common/response"
|
||||
"gofiber.study.skcks.cn/logger"
|
||||
"os"
|
||||
"os/signal"
|
||||
)
|
||||
|
||||
var log = logger.Logger()
|
||||
|
||||
func main() {
|
||||
app := fiber.New(fiber.Config{
|
||||
Prefork: true,
|
||||
CaseSensitive: true,
|
||||
StrictRouting: false,
|
||||
ServerHeader: "SkServer",
|
||||
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
||||
code := fiber.StatusInternalServerError
|
||||
|
||||
if e, ok := err.(*fiber.Error); ok {
|
||||
code = e.Code
|
||||
}
|
||||
|
||||
err = ctx.Status(code).JSON(errorx.NewDefaultError(err.Error()))
|
||||
if err != nil {
|
||||
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
log.Infof("\n%s", c.Request().String())
|
||||
return c.JSON(response.NewResponse("Hello, World 👋!"))
|
||||
})
|
||||
|
||||
app.Get("/routes", func(ctx *fiber.Ctx) error {
|
||||
return ctx.JSON(response.NewResponse(app.Stack()))
|
||||
})
|
||||
|
||||
go func() {
|
||||
if err := app.Listen(":3000"); err != nil {
|
||||
log.Fatalf("致命错误: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal)
|
||||
signal.Notify(quit, os.Interrupt)
|
||||
<-quit
|
||||
log.Infof("关闭服务")
|
||||
}
|
Loading…
Reference in New Issue
Block a user