You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.0 KiB
88 lines
2.0 KiB
package main
|
|
|
|
import (
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/swaggo/echo-swagger"
|
|
"gorm.io/gorm"
|
|
"net/http"
|
|
_ "sorbet/docs" // 开发文档
|
|
"sorbet/internal"
|
|
"sorbet/internal/entities"
|
|
"sorbet/internal/middleware"
|
|
"sorbet/internal/repositories"
|
|
"sorbet/internal/util"
|
|
"sorbet/pkg/env"
|
|
"sorbet/pkg/ioc"
|
|
"sorbet/pkg/rsp"
|
|
)
|
|
|
|
// @title 博客系统
|
|
// @version 1.0
|
|
// @description 基于 Echo 框架的基本库
|
|
//
|
|
// @contact.name API Support
|
|
// @contact.url http://www.swagger.io/support
|
|
// @contact.email support@swagger.io
|
|
//
|
|
// @license.name Apache 2.0
|
|
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
|
//
|
|
// @accept json
|
|
func main() {
|
|
if err := env.Init(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := internal.Init(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
repositories.Init()
|
|
|
|
e := echo.New()
|
|
e.HideBanner = true
|
|
e.HidePort = true
|
|
e.HTTPErrorHandler = func(err error, c echo.Context) {
|
|
if !c.Response().Committed {
|
|
http.Error(c.Response(), err.Error(), 500)
|
|
}
|
|
}
|
|
e.Logger = util.NewLogger()
|
|
e.Use(middleware.Recover())
|
|
e.Use(middleware.CORS())
|
|
e.Use(middleware.Logger)
|
|
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
db := ioc.MustGet[gorm.DB]().WithContext(c.Request().Context())
|
|
ci := ioc.Fork()
|
|
ci.Bind(db)
|
|
c.Set("db", db)
|
|
c.Set("ioc", ci)
|
|
return next(c)
|
|
}
|
|
})
|
|
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
|
e.GET("/", func(c echo.Context) error {
|
|
repo := repositories.NewCompanyRepository(c.Get("db").(*gorm.DB))
|
|
//err := c.Get("ioc").(*ioc.Container).Resolve(&repo)
|
|
//if err != nil {
|
|
// return err
|
|
//}
|
|
//db := ioc.MustGet[gorm.DB]().WithContext(c.Request().Context())
|
|
//ioc.Fork().Bind(db)
|
|
//repo := ioc.MustGet[repositories.CompanyRepository]()
|
|
repo.Create(&entities.Company{Name: "海苔一诺"})
|
|
pager, err := repo.Paginate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return rsp.Ok(c, pager)
|
|
})
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
}
|
|
|
|
func panicIf(e error) {
|
|
if e != nil {
|
|
panic(e)
|
|
}
|
|
}
|
|
|