middleware_main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "go.mongodb.org/mongo-driver/bson/primitive"
  10. // . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/gen"
  11. )
  12. func CreateMainFile(project *Project) (err error) {
  13. spew.Dump(project.Custom)
  14. var (
  15. Index = G.NewFile("main")
  16. address = strings.Join(strings.Split(project.BaseURL, ":")[1:], ":")
  17. goPackageRepository = project.Custom["go.package.repository"].(string)
  18. )
  19. address = strings.Replace(address, "//", "", -1)
  20. Index.Id(`import (
  21. env `).Lit(goPackageRepository+"/build/v1/env").Id(`
  22. `).Id(project.ID).Lit(goPackageRepository+"/build/v1").Id(`
  23. models `).Lit(goPackageRepository+"/build/v1/models").Id(`
  24. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api" // Pacotes do framework web
  25. "os"
  26. "fmt"
  27. iris "github.com/kataras/iris/v12"
  28. "github.com/kataras/iris/v12/middleware/recover"
  29. )
  30. func main() {
  31. var (
  32. err error
  33. )
  34. if err = env.InitEnvironment(); err != nil {
  35. panic(err)
  36. }
  37. app := iris.New()
  38. app.Logger().SetLevel("debug")
  39. // Optionally, add two built'n handlers
  40. // that can recover from any http-relative panics
  41. // and log the requests to the terminal.
  42. app.Use(recover.New())
  43. app.Use(`).Qual("github.com/kataras/iris/v12/middleware/logger", "New").Id(`())
  44. app.Use(api.DefaultCorsHandler())
  45. api.BuildVersion = `).Id(fmt.Sprintf(`"%s"`, project.BuildVersion)).Id(`
  46. // session := &api.Mongo{ Config: os.Getenv("MONGO_CONFIG") }
  47. models.Api.Config = os.Getenv("MONGO_CONFIG")
  48. if models.Api.Config != "" {
  49. if err = models.Api.Init(); err != nil {
  50. panic(err)
  51. }
  52. }
  53. if err = `).Id(fmt.Sprintf("%s.AppInitialize(app)", project.ID)).Id(`; err != nil {
  54. panic(err)
  55. }
  56. `).Id(project.ID).Id(`.Register(app)
  57. fmt.Println("API_VERSION: ", api.BuildVersion)
  58. fmt.Println("BUILD_VERSION: ", `).Lit(primitive.NewObjectID().Hex()).Id(`)
  59. // app.Run(iris.Addr(os.Getenv("APP_ADDRS")), iris.WithoutServerError(iris.ErrServerClosed))
  60. // app.Run(, iris.WithoutServerError(iris.ErrServerClosed))
  61. // $ openssl req -new -newkey rsa:4096 -x509 -sha256 \
  62. // -days 365 -nodes -out cert.crt -keyout key.key
  63. app.Run(
  64. iris.TLS(os.Getenv("APP_ADDRS"),"cert.crt","key.key"),
  65. iris.WithoutServerError(iris.ErrServerClosed),
  66. )
  67. }
  68. `)
  69. if err = Write(project.Paths.Build("/main.go"), Index); err != nil {
  70. return
  71. }
  72. path := project.Paths.Include("/go/app_initialize.go")
  73. if !utils.FileExists(path) {
  74. AppInitialize := G.NewFile("v1")
  75. AppInitialize.Id(`
  76. import(
  77. iris "github.com/kataras/iris/v12"
  78. )
  79. func AppInitialize(app *iris.Application) error {
  80. return nil
  81. }
  82. `)
  83. if err = Write(path, AppInitialize); err != nil {
  84. return
  85. }
  86. }
  87. return nil
  88. }
  89. // app.RegisterView(iris.HTML("./public", ".html"))
  90. // app.Get("/", func(ctx iris.Context) {
  91. // ctx.View("index.html")
  92. // })
  93. // app.HandleDir("/", "./public", iris.DirOptions{
  94. // Asset: Asset,
  95. // AssetInfo: AssetInfo,
  96. // AssetNames: AssetNames,
  97. // // IndexName: "index.html", // default.
  98. // // If you want to show a list of embedded files when inside a directory without an index file:
  99. // // ShowList: true,
  100. // // DirList: func(ctx iris.Context, dirName string, f http.File) error {
  101. // // // [Optional, custom code to show the html list].
  102. // // }
  103. // })
  104. // app.SPA(app.StaticHandler("./public", false, false))