middleware_main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package got
  2. import (
  3. "fmt"
  4. "strings"
  5. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  6. "git.eugeniocarvalho.dev/eugeniucarvalho/utils"
  7. G "github.com/dave/jennifer/jen"
  8. "github.com/davecgh/go-spew/spew"
  9. // . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/gen"
  10. )
  11. func CreateMainFile(project *Project) (err error) {
  12. spew.Dump(project.Custom)
  13. var (
  14. Index = G.NewFile("main")
  15. address = strings.Join(strings.Split(project.BaseURL, ":")[1:], ":")
  16. goPackageRepository = project.Custom["go.package.repository"].(string)
  17. )
  18. address = strings.Replace(address, "//", "", -1)
  19. Index.Id(`import (
  20. `).Id(project.ID).Lit(goPackageRepository+"/build/v1").Id(`
  21. models `).Lit(goPackageRepository+"/build/v1/models").Id(`
  22. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api" // Pacotes do framework web
  23. "os"
  24. "fmt"
  25. iris "github.com/kataras/iris/v12"
  26. "github.com/kataras/iris/v12/middleware/recover"
  27. )
  28. func main() {
  29. var (
  30. err error
  31. )
  32. if err = `).Id(project.ID).Id(`.InitEnvironment(); err != nil {
  33. panic(err)
  34. }
  35. app := iris.New()
  36. app.Logger().SetLevel("debug")
  37. // Optionally, add two built'n handlers
  38. // that can recover from any http-relative panics
  39. // and log the requests to the terminal.
  40. app.Use(recover.New())
  41. app.Use(`).Qual("github.com/kataras/iris/v12/middleware/logger", "New").Id(`())
  42. app.Use(api.DefaultCorsHandler())
  43. api.BuildVersion = `).Id(fmt.Sprintf(`"%s"`, project.BuildVersion)).Id(`
  44. // session := &api.Mongo{ Config: os.Getenv("MONGO_CONFIG") }
  45. models.Api.Config = os.Getenv("MONGO_CONFIG")
  46. if err = models.Api.Init(); err != nil {
  47. panic(err)
  48. }
  49. if err = `).Id(fmt.Sprintf("%s.AppInitialize(app)", project.ID)).Id(`; err != nil {
  50. panic(err)
  51. }
  52. `).Id(project.ID).Id(`.Register(app)
  53. fmt.Println("API_VERSION: ", api.BuildVersion)
  54. app.Run(iris.Addr(os.Getenv("APP_ADDRS")), iris.WithoutServerError(iris.ErrServerClosed))
  55. }
  56. `)
  57. if err = Write(fmt.Sprintf("%s/main.go", project.OutPath), Index); err != nil {
  58. return
  59. }
  60. path := fmt.Sprintf("%s/v1/app_initialize.go", project.OutPath)
  61. if !utils.FileExists(path) {
  62. AppInitialize := G.NewFile("v1")
  63. AppInitialize.Id(`
  64. import(
  65. iris "github.com/kataras/iris/v12"
  66. )
  67. func AppInitialize(app *iris.Application) error {
  68. return nil
  69. }
  70. `)
  71. if err = Write(path, AppInitialize); err != nil {
  72. return
  73. }
  74. }
  75. return nil
  76. }
  77. // app.RegisterView(iris.HTML("./public", ".html"))
  78. // app.Get("/", func(ctx iris.Context) {
  79. // ctx.View("index.html")
  80. // })
  81. // app.HandleDir("/", "./public", iris.DirOptions{
  82. // Asset: Asset,
  83. // AssetInfo: AssetInfo,
  84. // AssetNames: AssetNames,
  85. // // IndexName: "index.html", // default.
  86. // // If you want to show a list of embedded files when inside a directory without an index file:
  87. // // ShowList: true,
  88. // // DirList: func(ctx iris.Context, dirName string, f http.File) error {
  89. // // // [Optional, custom code to show the html list].
  90. // // }
  91. // })
  92. // app.SPA(app.StaticHandler("./public", false, false))