forked from LemonNekoGH/NekoQuestionBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3323e6e
commit e961f8a
Showing
10 changed files
with
309 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package logger | ||
|
||
func Info(message string) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package services | ||
|
||
func GetAllQuestions() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |