1 Star 0 Fork 2

happylys/label_sys

forked from 暴明坤/label_sys 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 5.76 KB
一键复制 编辑 原始数据 按行查看 历史
xinlong 提交于 2023-01-16 20:56 . feat:添加任务分配功能
// Code generated by hertz generator.
package main
import (
"context"
"crypto/rand"
"fmt"
"hertz/demo/biz/boot"
handler "hertz/demo/biz/handler"
"hertz/demo/biz/mw"
"log"
"os"
"strings"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/spf13/viper"
)
func setDefaultConfig() {
viper.SetDefault("addr", "127.0.0.1")
viper.SetDefault("port", 8080)
viper.SetDefault("db.path", "./db.sqlite")
viper.SetDefault("user.default_root_user", "root")
viper.SetDefault("user.default_root_password", "test-password")
}
func initViper() error {
configPath := os.Getenv("LABEL_SYS_CONFIG_PATH")
if configPath == "" {
configPath = "./config.yaml"
}
setDefaultConfig()
viper.SetConfigFile(configPath)
viper.SetEnvPrefix("LABEL_SYS")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "__"))
viper.AutomaticEnv()
return viper.ReadInConfig()
}
func main() {
// init viper
if err := initViper(); err != nil {
log.Fatalf("init viper failed: %s\n", err.Error())
}
addr := viper.GetString("addr")
port := viper.GetInt("port")
listenAddr := fmt.Sprintf("%s:%d", addr, port)
h := server.Default(server.WithHostPorts(listenAddr))
db := boot.InitDB()
boot.InitMeta()
// 向整个服务器注入数据库上下文.
injectDB := mw.InjectDB(db)
h.Use(injectDB)
timeout := viper.GetInt64("jwt.timeout")
if timeout == 0 {
timeout = 30
}
secrets := make([]byte, 64)
rand.Read(secrets)
authMiddleware, err := mw.BuildAuthMiddleware(secrets, time.Duration(timeout)*time.Second)
if err != nil {
log.Fatalf("build auth middleware failed: %s", err.Error())
}
api := h.Group("/api/v1")
api.POST("/login", authMiddleware.LoginHandler)
auth := api.Group("/auth")
auth.Use(authMiddleware.MiddlewareFunc())
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
api.GET("/ping", authMiddleware.MiddlewareFunc(), handler.Ping)
/// 用户相关API, 只有管理员才能进行用户管理, 其他用户只有查询用户的权力
user := api.Group("/user")
user.Use(authMiddleware.MiddlewareFunc())
user.GET("/", handler.GetAllUser)
user.GET("/:id", handler.GetUserById)
// 注册用户
user.POST("/", mw.RequireAdmin(), handler.Register)
// 启用和禁用用户
user.POST("/:id/enabled", mw.RequireAdmin(), handler.EnableUser)
user.DELETE("/:id/enabled", mw.RequireAdmin(), handler.DisableUser)
// 更改用户属性
user.PATCH("/:id", mw.RequireAdmin(), handler.UpdateUser)
user.PATCH("/:id/password", mw.RequireAdmin(), handler.UpdatePassword)
/// 样本相关API, 只有登录的用户才能访问
sample := api.Group("/sample")
sample.Use(authMiddleware.MiddlewareFunc())
sample.PATCH("/", handler.SyncSample)
sample.GET("/", handler.ListSample)
sample.GET("/:id", handler.GetSampleByID)
// 获取一个sample下所有的selections
// 额外的查询参数,
// status string 仅查询某个状态的selections,
// author uint64 仅查询某个用户的selection,
sample.GET("/:id/selections", handler.GetSampleSelection)
// 获取一个sample下的media, 无查询参数时返回所有的media
// 额外的查询参数, confirmed 仅查询已经确认的, owned 仅查询用户自己的,
// sample.GET("/:id/media")
/// 筛选相关API, 只有登录的用户才能访问
selection := api.Group("/selection")
selection.Use(authMiddleware.MiddlewareFunc())
// 获取所有筛选结果
// 额外的查询参数,
// status string 仅查询某个状态的selections,
// author uint64 仅查询某个用户的selection,
selection.GET("/", handler.ListSelection)
// 创建新的筛选结果
selection.POST("/", handler.AddSelection)
// 按照ID查找筛选结果
selection.GET("/:id", handler.FindSelectionById)
// 修改筛选结果的状态
selection.POST("/:id/status", handler.UpdateSelectionStatus)
// 修改筛选结果的注释
selection.POST("/:id/comment", handler.UpdateSelectionComment)
// 修改筛选结果的内容
selection.PATCH("/:id", handler.UpdateSelection)
// 删除筛选结果
selection.DELETE("/:id", handler.DeleteSelection)
/// 媒体相关API
media := api.Group("/media")
media.Use(authMiddleware.MiddlewareFunc())
// 查询所有视频
media.GET("/", handler.ListAllMedia)
// 按id查询media
media.GET("/:id", handler.FindMediaByID)
// 查询media下所有的label,
// status string 仅查询某个状态的label,
// author uint64 仅查询某个用户的label,
media.GET("/:id/label", handler.ListMediaLabel)
label := api.Group("/label")
label.Use(authMiddleware.MiddlewareFunc())
// 获取所有筛选结果
// 额外的查询参数,
// status string 仅查询某个状态的label,
// author uint64 仅查询某个用户的label,
label.GET("/", handler.ListLabel)
// 创建新的label
label.POST("/", handler.AddLabel)
// 修改label的状态
label.POST("/:id/status", handler.UpdateLabelStatus)
label.POST("/:id/comment", handler.UpdateLabelComment)
label.GET("/:id", handler.GetLabelByID)
// 修改label的内容
label.PATCH("/:id", handler.UpdateLabel)
// 按id删除
label.DELETE("/:id", handler.DeleteLabelById)
meta := api.Group("/meta")
// meta.Use(authMiddleware.MiddlewareFunc())
meta.GET("/view", handler.GetAllViews)
meta.GET("/instance", handler.GetInstanceLabel)
meta.GET("/view/:view", handler.GetView)
job := api.Group("/job")
job.Use(authMiddleware.MiddlewareFunc())
// 创建新的job
job.POST("/", handler.AddJob)
// 查询某个用户id被分配的样本任务
job.GET("/:id/sample", handler.ListSampleJob)
// 查询某个用户id被分配的筛选任务
job.GET("/:id/selection", handler.ListSelectionJob)
customizedRegister(h)
// set NoRoute handler
h.NoRoute(func(c context.Context, ctx *app.RequestContext) {
ctx.String(consts.StatusNotFound, "no route")
})
h.Spin()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ryanlu/label_sys.git
[email protected]:ryanlu/label_sys.git
ryanlu
label_sys
label_sys
master

搜索帮助