Skip to content

Commit

Permalink
🚧 正在迁移至 gin、外置配置文件、gorm
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNekoGH committed Sep 18, 2022
1 parent 3323e6e commit e961f8a
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 18 deletions.
7 changes: 3 additions & 4 deletions be/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ require (
github.com/Joker/hpp v1.0.0 // indirect
github.com/Shopify/goreferrer v0.0.0-20210407190730-c9ba3cb61340 // indirect
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f
github.com/gin-gonic/gin v1.8.1
github.com/go-sql-driver/mysql v1.6.0
github.com/iris-contrib/middleware/cors v0.0.0-20210110101738-6d0a4d799b5d
github.com/json-iterator/go v1.1.11 // indirect
github.com/kataras/golog v0.1.7
github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210528161351-32291fa59eff
github.com/klauspost/compress v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/microcosm-cc/bluemonday v1.0.9 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/tdewolff/minify/v2 v2.9.17 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1
gorm.io/gorm v1.23.9
)
75 changes: 61 additions & 14 deletions be/go.sum

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions be/internal/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package api

import (
"neko-question-box-be/pkg/handler"
"net/http"

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

// 验证 Captcha
func postCaptcha(ctx *gin.Context) (handler.HandlerResponse, error) {
return nil, nil
}

// 获取新的 captcha 图片
func getCaptcha(ctx *gin.Context) (handler.HandlerResponse, error) {
return nil, nil
}

// 获取 bing 每日壁纸
func getBingWallpaper(ctx *gin.Context) (handler.HandlerResponse, error) {
return nil, nil
}

// 获取已经有的问题和答案
func getQuestion(ctx *gin.Context) (handler.HandlerResponse, error) {
return nil, nil
}

// 提交新的问题
func postQuestion(ctx *gin.Context) (handler.HandlerResponse, error) {
return nil, nil
}

// 检查服务器状态
func getPing(ctx *gin.Context) (handler.HandlerResponse, error) {
return nil, nil
}

func Handlers() handler.HandlerGroup {
return handler.HandlerGroup{
Name: "",
Group: map[string][]handler.Handler{
"captcha": {
handler.NewHandler(http.MethodPost, postCaptcha),
handler.NewHandler(http.MethodGet, getCaptcha),
},
"bing": {handler.NewHandler(http.MethodGet, getBingWallpaper)},
"question": {
handler.NewHandler(http.MethodGet, getQuestion),
handler.NewHandler(http.MethodPost, postQuestion),
},
"ping": {
handler.NewHandler(http.MethodGet, getPing),
},
},
}
}
54 changes: 54 additions & 0 deletions be/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config

import (
"fmt"
"io/ioutil"
"os"

"gopkg.in/yaml.v3"
)

type Config struct {
Port int `yaml:"port"`
Database struct {
Host string `yaml:"host"` // 主机
Port string `yaml:"port"` // 端口号
Username string `yaml:"username"` // 用户名
Password string `yaml:"password"` // 密码
Database string `yaml:"database"` // 使用的数据库
} `yaml:"database"`
}

//embed:config.test.yaml
var testCfg []byte
var Conf Config // 配置

// 初始化配置
func InitConfig(forTest bool) {
confContent := make([]byte, 0)
var err error = nil
// 读取环境变量
configFile := os.Getenv("QBOX_CONFIG_PATH")
if forTest {
// 尝试读取根目录的配置文件
confContent = testCfg
} else {
var file *os.File
// 读取环境变量中指定的文件
file, err = os.Open(configFile)
if err != nil {
panic(err)
}
confContent, err = ioutil.ReadAll(file)
if err != nil {
panic(err)
}
}

// 读取完成,映射成对象
err = yaml.Unmarshal(confContent, Conf)
if err != nil {
panic(err)
}
fmt.Printf("")
}
Empty file.
17 changes: 17 additions & 0 deletions be/internal/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package database

import (
"errors"

"gorm.io/gorm"
)

// 初始化数据库
func InitDB() {

}

// 是否是「记录未找到」错误
func IsNoRecordFoundError(err error) bool {
return errors.Is(err, gorm.ErrRecordNotFound)
}
5 changes: 5 additions & 0 deletions be/internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package logger

func Info(message string) {

}
14 changes: 14 additions & 0 deletions be/internal/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package server

import (
"neko-question-box-be/internal/api"

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

func InitServer() *gin.Engine {
r := gin.Default()
api.Handlers().Install(r.Group(""))

return r
}
5 changes: 5 additions & 0 deletions be/internal/services/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package services

func GetAllQuestions() {

}
92 changes: 92 additions & 0 deletions be/pkg/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package handler

import (
"fmt"
"net/http"

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

// 控制器要返回的错误
type HandlerError struct {
Code int `json:"code"`
Message string `json:"message"`
Status int `json:"-"`
}

// 控制器的正常返回
type HandlerResponse interface{}

// 实现错误接口
func (h HandlerError) Error() string {
return fmt.Sprintf("code: %d, message: %s", h.Code, h.Message)
}

// 新建一个控制器错误
func NewHandlerError(status int, code int, message string) HandlerError {
return HandlerError{
Status: status,
Code: code,
Message: message,
}
}

type Handler struct {
Func gin.HandlerFunc // 实际函数
Mehtod string // 方法
}

type HandlerFunc func(ctx *gin.Context) (HandlerResponse, error)

// 新建控制器
func NewHandler(method string, fun HandlerFunc) Handler {
return Handler{
Mehtod: method,
Func: func(ctx *gin.Context) {
resp, err := fun(ctx)
// 错误处理
if err != nil {
switch e := err.(type) {
case HandlerError:
ctx.AbortWithStatusJSON(e.Status, e)
return
// 其它错误,返回 500
default:
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"code": 50001,
"message": e.Error(),
})
return
}
}
// 正常
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": resp,
})
},
}
}

type HandlerGroup struct {
Name string // 控制器组名
Group map[string][]Handler // 控制器
SubHandlers []HandlerGroup // 子控制器组
}

// 挂载控制器组
func (g HandlerGroup) Install(parent *gin.RouterGroup) {
group := parent.Group(g.Name)

// 挂载控制器
for name, handlers := range g.Group {
for _, handler := range handlers {
group.Handle(handler.Mehtod, name, handler.Func)
}
}

// 挂载子控制器组
for _, subGroup := range g.SubHandlers {
subGroup.Install(group)
}
}

0 comments on commit e961f8a

Please sign in to comment.