blog_backend_api/main.go

250 lines
9.7 KiB
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package main
import (
2019-04-07 04:25:07 +00:00
"background/config"
"background/controller"
"background/controller/middle"
"background/db"
2020-06-24 17:15:46 +00:00
_ "background/docs"
2019-04-07 04:25:07 +00:00
"background/logs"
"background/model"
2019-01-25 09:11:15 +00:00
"log"
"strconv"
2020-08-30 07:14:49 +00:00
2021-08-25 16:53:20 +00:00
"tcptemplate/logger"
2021-08-15 16:19:25 +00:00
"tcptemplate/network"
2020-08-30 07:14:49 +00:00
"github.com/gin-gonic/gin"
2020-08-23 18:08:26 +00:00
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
2020-08-30 10:44:29 +00:00
"github.com/swaggo/gin-swagger/swaggerFiles"
2020-08-30 07:14:49 +00:00
sessions "github.com/tommy351/gin-sessions"
2019-01-25 09:11:15 +00:00
)
2020-06-24 17:15:46 +00:00
// @title 大厅功能api
// @version 1.0
// @host localhost:8080
// @BasePath /api/v1
2019-03-07 06:06:29 +00:00
var (
2021-03-22 04:10:37 +00:00
userController = controller.UserController{}
mailContoller = controller.MailController{}
fileController = controller.FileController{}
planController = controller.PlanController{}
2021-02-27 17:45:34 +00:00
openapiController = controller.OpenApiController{}
2019-03-07 06:06:29 +00:00
)
func CORSMiddleware(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
2020-08-30 07:14:49 +00:00
if config.ApiConfig().RunMode == "release" {
c.Writer.Header().Set("Access-Control-Allow-Origin", "https://testingcloud.club")
2020-08-30 07:14:49 +00:00
} else {
2022-03-30 15:58:20 +00:00
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:8080")
}
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Headers",
2020-08-30 07:14:49 +00:00
"X-Requested-With,"+
" Content-Type, Origin, Authorization,"+
"Accept,Client-Security-Token, Accept-Encoding,"+
"x-access-token,Access-Control-Request-Method,"+
"Access-Control-Request-Headers")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
2022-03-30 15:58:20 +00:00
log.Print("remote addr is ",c.Request.RemoteAddr)
if c.Request.Method == "OPTIONS" {
log.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
}
2019-03-12 06:01:25 +00:00
func InitConfig() {
2019-03-07 06:06:29 +00:00
e := config.Init("user.yaml")
if nil != e {
log.Println(e.Error())
}
}
2019-03-07 02:36:09 +00:00
func InitMysql() {
2019-01-25 09:11:15 +00:00
c := config.GetMysqlConfig()
2019-03-07 02:36:09 +00:00
if c == nil {
2019-01-25 09:11:15 +00:00
logs.Error("cannnot connect mysql server")
2019-03-07 02:36:09 +00:00
} else {
2019-01-25 09:11:15 +00:00
db.Init()
}
}
func InitRedisConfig() {
2019-03-07 06:06:29 +00:00
e := config.InitRedis()
if nil != e {
logs.Error(e.Error())
return
}
}
2020-08-30 07:14:49 +00:00
func InitElasticSearch() {
e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping())
if nil != e {
}
}
2019-03-12 06:01:25 +00:00
func InitLogs() {
2019-03-07 06:06:29 +00:00
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
}
2021-05-07 16:15:04 +00:00
2021-12-08 16:52:31 +00:00
type WsHandle struct {
2021-08-25 16:53:20 +00:00
}
2021-12-08 16:52:31 +00:00
2021-08-25 16:53:20 +00:00
func (this WsHandle) OnDisConected(*network.ConnectionContext) {
2021-12-08 16:52:31 +00:00
2021-08-25 16:53:20 +00:00
}
2021-12-08 16:52:31 +00:00
func (this WsHandle) OnConected(c *network.ConnectionContext) {
log.Print(c.IpAdress, c.Cnntime)
2021-08-25 16:53:20 +00:00
}
2021-12-08 16:52:31 +00:00
func (this WsHandle) OnDataRecv(p *network.ConnectionContext, dat []byte) {
2021-08-25 16:53:20 +00:00
log.Print(p.IpAdress)
logger.LogRealeaseInfo(string(dat))
}
2019-03-07 02:36:09 +00:00
func main() {
2019-03-07 06:06:29 +00:00
InitConfig()
InitLogs()
InitRedisConfig()
2019-03-07 06:06:29 +00:00
InitMysql()
2021-12-30 16:38:06 +00:00
db.InitELK()
2019-03-07 06:06:29 +00:00
2019-01-25 09:11:15 +00:00
r := gin.Default()
2019-01-29 13:57:53 +00:00
store := sessions.NewCookieStore([]byte("secret123"))
2019-03-07 06:06:29 +00:00
r.Use(sessions.Middleware("sess_store", store))
2021-12-08 16:52:31 +00:00
r.Use(CORSMiddleware) // Cross domain
2021-08-15 16:19:25 +00:00
2021-12-08 16:52:31 +00:00
go func() {
server := network.ServerFactory("0.0.0.0", config.ApiConfig().WsServerConf)
2021-08-25 16:53:20 +00:00
server.SetHandle(WsHandle{})
2021-08-15 16:19:25 +00:00
e := server.Run()
if nil != e {
}
}()
2020-06-24 17:15:46 +00:00
go func() {
2021-08-15 16:19:25 +00:00
2020-06-24 17:15:46 +00:00
// programatically set swagger info
r := gin.New()
// use ginSwagger middleware to serve the API docs
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
2020-06-26 10:20:36 +00:00
r.Run(":9992")
2020-06-24 17:15:46 +00:00
}()
2019-03-07 06:06:29 +00:00
api := r.Group("/api")
2019-01-25 09:11:15 +00:00
{
/** 添加或修改用户 **/
2019-03-27 07:03:45 +00:00
api.POST("/user", userController.SetUser)
2019-01-25 09:11:15 +00:00
/** 删除用户 **/
2019-03-27 07:03:45 +00:00
api.DELETE("/user", userController.DelUser)
2019-01-25 09:11:15 +00:00
/** 获取单独用户详情信息 methods(id) **/
2020-06-20 15:15:23 +00:00
api.GET("/user", middle.AuthMiddle, userController.GetUser)
2019-01-25 09:11:15 +00:00
/** 获取所有用户 **/
2020-06-20 15:15:23 +00:00
api.GET("/users", middle.AuthMiddle, userController.Users)
api.POST("/search_users", middle.AuthMiddle, userController.SerarchUsers)
2019-01-25 09:11:15 +00:00
/** 用户登录 **/
2019-03-27 07:03:45 +00:00
api.POST("/login", userController.Login)
2019-01-25 09:11:15 +00:00
/** 用户注册 **/
2019-03-27 07:03:45 +00:00
api.POST("/register", userController.Register)
2019-01-25 09:11:15 +00:00
/** 用户退出登陆 **/
2019-09-06 07:53:04 +00:00
api.GET("/logout/:token", userController.Logout)
2019-03-27 07:03:45 +00:00
api.POST("/verify", mailContoller.OnSendEmailCode)
2019-04-07 04:25:07 +00:00
/** 修改密码**/
2020-06-20 15:15:23 +00:00
api.POST("modify_pass", middle.AuthMiddle, userController.ModifyPasswd)
2021-12-08 16:52:31 +00:00
api.GET("/article/:id", controller.GetArticle) //获取文章
api.POST("/articles", controller.GetArticles) // 获取所有文章
2022-04-13 16:38:16 +00:00
api.GET("/articles_tree", controller.GetArticlesTree) // 获取所有文章
2021-12-08 16:52:31 +00:00
api.PUT("/article", controller.AddArticle) // 添加文章
api.PUT("/article_tree", controller.AddArticleTree) // 添加文章
2021-12-08 16:52:31 +00:00
api.PUT("/article_search", controller.SearchArticles) // 添加文章
api.GET("/article_type", controller.ArticlesType) //获取所有文章分类
api.PUT("/article_type", controller.AddArticleType) // 添加文章分类
api.DELETE("/article_type", controller.DeleteArticleType) // 删除文章分类
api.POST("/doc_search_term", controller.SearchArticleES) // 文章内容搜索,基于es的倒排
api.POST("/doc_match_search_hightlight", controller.SearchArticleESHightLight) // 文章内容搜索,基于es的倒排
api.POST("/article_update", controller.UpdateArtilce) //更新文章
api.POST("/article_update_tree", controller.UpdateArtilceTree) //更新文章
2021-12-08 16:52:31 +00:00
api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数
api.DELETE("/article/:id", controller.DeleteArticle) ////删除文章
api.DELETE("/article_tree/:id", controller.DeleteArticleTree) ////删除文章
2022-11-08 16:03:38 +00:00
api.GET("/article_history",controller.GetArticleHistory) // 历史
2021-12-08 16:52:31 +00:00
api.POST("/image_upload", fileController.OnUpload) // 上传图片,如果图片太大自动裁减为原来的一半
api.POST("/image_upload_origin", fileController.OnUploadOrigin) // 上传图片
api.GET("/image_download/:file", fileController.OnDownLoad) // 下载图片
api.GET("/image_thumbnail/:file", fileController.OnThumbnail) // 下载图片
api.GET("/doc_types", controller.ArticlesTypes) // 获取所有的文章类型
api.POST("/hardware", controller.AddHardware) // 新增硬件
api.GET("/hardware", controller.ReadHardWare) // 读取硬件
api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件
2021-05-22 17:27:55 +00:00
2020-06-20 15:15:23 +00:00
api.PUT("/file", fileController.OnFileUploadFile) // 上传文件
api.GET("/file", fileController.DownloadFile) // 下载 文件
api.GET("/filelist", fileController.FileList) // 文件列表
api.GET("/fileType", fileController.FileType) // 文件类型
2020-03-31 17:07:32 +00:00
2021-12-08 16:52:31 +00:00
api.PUT("/memo", controller.CreateMemo) // 备忘录新建
api.POST("/memo", controller.UpdateMemo) // 备忘录更新
api.POST("/memos", controller.GetMemos) // 备忘录批量
api.POST("/delmemo", controller.DeleteMemos) //删除备忘录
api.GET("/memo", controller.GetMemo) // 单独读取备忘录
api.GET("/memocnt", controller.GetMemoCnt) // 单独读取备忘录
2021-06-11 17:26:37 +00:00
2020-09-26 04:30:23 +00:00
api.GET("doc_groups", controller.GetDocGroup) // 获取所有的文章分组
api.POST("type_group", controller.GetDocTypeGroup) // 获取类所在的组
api.POST("group_type", controller.GetDoGroupcType) // 获取类所在的组
2020-08-23 18:08:26 +00:00
api.GET("doc_versions", nil) // 获取文章的某个版本
2021-01-09 14:55:22 +00:00
2021-06-11 17:26:37 +00:00
api.PUT("/book", controller.CreateBook) //新建书籍
api.POST("/book", controller.UpdateBook) // 更新书籍
2021-02-16 06:05:21 +00:00
api.POST("/getbook", controller.GetBook) // 单书籍数据
api.POST("/getbooks", controller.GetPageBook) // 批量书籍
api.POST("/delbook", controller.Delbook) // 删除书籍
2021-08-07 03:25:41 +00:00
api.GET("/bookcount", controller.BookCount) // 书籍名称
2021-02-16 06:05:21 +00:00
2021-02-16 07:44:26 +00:00
api.POST("/get_plans", planController.GetPlanDates) // 获取当天的所有计划
api.PUT("/plan", planController.AddPlan) // 添加计划
api.DELETE("/plan", planController.DelPlan) // 删除计划
api.POST("/plan_days", planController.PlanDay) // 获取本月有计划天数
2021-02-27 06:28:23 +00:00
api.GET("/plan_types", planController.Types)
2021-03-08 15:59:50 +00:00
2021-12-08 16:52:31 +00:00
api.PUT("/undo", planController.CreateUndo) // 新增待办事项
api.POST("/undo", planController.UpdateUndo) // 更新待办事项
api.POST("/undo_finish/:id", planController.FinishUndo) // 完成待办事项
api.POST("/undo_unfinish/:id", planController.UnFinishUndo) // 已完成事项改未完成
api.GET("/undo", planController.GetUndo) // 获得待办事项
api.GET("/done", planController.GetDone) // 获得待办事项
api.DELETE("/undo/:id", planController.DeleteUndo) // 删除待办事项
api.PUT("/doc_template", controller.CreateDocTemplate) // 创建文章模板
api.POST("/doc_template", controller.UpdateDocTemplate) // 更新文章模板
api.POST("/get_doc_template", controller.GetDocTemplate) // 获得文章模板
api.DELETE("/doc_template/:id", controller.DeleteDocTemplate) // 删除文章模板
api.GET("/doc_template/:id", controller.GetDocTemplateId) //获得单个文章模板信息
2019-03-07 02:36:09 +00:00
}
2020-11-12 04:08:22 +00:00
openapi := r.Group("openapi")
2020-06-20 15:15:23 +00:00
{
2021-12-08 16:52:31 +00:00
openapi.POST("/diff") // 比较两个文档差异
openapi.POST("/ddl2orm", openapiController.DDL2ORM) // sql ddl转gorm 结构体
openapi.POST("/ddl2markdown", openapiController.DDL2Markdown) // sql ddl转markdown 文档
openapi.POST("/ui2css", openapiController.UI2CSS) // qt ui文件转css文档
openapi.POST("/duilib_xml2cpp", openapiController.DuilibXml2Cpp) // duilib to cpp
2020-06-20 15:15:23 +00:00
}
2019-03-07 06:06:29 +00:00
e := r.Run(":" + strconv.Itoa(config.GetPort()))
if nil != e {
2019-03-07 02:36:09 +00:00
log.Print(e.Error())
2019-01-25 09:11:15 +00:00
}
}