middleware_main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. path := fmt.Sprintf("../project/include/go/app_initialize.go")
  62. if !utils.FileExists(path) {
  63. AppInitialize := G.NewFile("v1")
  64. AppInitialize.Id(`
  65. import(
  66. iris "github.com/kataras/iris/v12"
  67. )
  68. func AppInitialize(app *iris.Application) error {
  69. return nil
  70. }
  71. `)
  72. if err = Write(path, AppInitialize); err != nil {
  73. return
  74. }
  75. }
  76. return nil
  77. }
  78. // app.RegisterView(iris.HTML("./public", ".html"))
  79. // app.Get("/", func(ctx iris.Context) {
  80. // ctx.View("index.html")
  81. // })
  82. // app.HandleDir("/", "./public", iris.DirOptions{
  83. // Asset: Asset,
  84. // AssetInfo: AssetInfo,
  85. // AssetNames: AssetNames,
  86. // // IndexName: "index.html", // default.
  87. // // If you want to show a list of embedded files when inside a directory without an index file:
  88. // // ShowList: true,
  89. // // DirList: func(ctx iris.Context, dirName string, f http.File) error {
  90. // // // [Optional, custom code to show the html list].
  91. // // }
  92. // })
  93. // app.SPA(app.StaticHandler("./public", false, false))