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.
 
 
 

199 lines
4.3 KiB

  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/urfave/cli/v2"
  5. "golib.gaore.com/GaoreGo/beegoinit-cmd/utils"
  6. "os"
  7. "path"
  8. )
  9. var gitignore = `# Created by .ignore support plugin (hsz.mobi)
  10. ### Example user template template
  11. ### Example user template
  12. # IntelliJ project files
  13. .idea
  14. *.iml
  15. out
  16. gen
  17. /runtime
  18. ### Go template
  19. # Binaries for programs and plugins
  20. *.exe
  21. *.exe~
  22. *.dll
  23. *.so
  24. *.dylib
  25. # Test binary, built with 'go test -c\'
  26. *.test
  27. # Output of the go coverage tool, specifically when used with LiteIDE
  28. *.out
  29. # Dependency directories (remove the comment below to include it)
  30. # vendor/`
  31. var tree = `
  32. .
  33. ├── applications
  34. │ ├── _common
  35. │ ├── api
  36. │ | └── index.go
  37. │ └── init.go
  38. ├── cli
  39. │ └── test
  40. ├── conf
  41. │ └──app.conf
  42. ├── main.go
  43. ├── runtime
  44. │ └── logs
  45. ├── static
  46. ├── Makefile
  47. └── README.md
  48. `
  49. var initfun = `
  50. package applications
  51. import (
  52. "errors"
  53. "github.com/astaxie/beego"
  54. "golib.gaore.com/GaoreGo/beegoinit/response"
  55. router "golib.gaore.com/GaoreGo/beegoinit/routers"
  56. "golib.gaore.com/GaoreGo/grconfig"
  57. "golib.gaore.com/GaoreGo/grlogs"
  58. common "jedi.gaore.com/applications/_common/controllers"
  59. api "jedi.gaore.com/applications/api/controllers"
  60. "strings"
  61. )
  62. func init() {
  63. router.AddController("api", new(api.IndexController))
  64. // 通用路由处理
  65. router.ErrorController(&common.ErrorController{})
  66. beego.Router("/", &api.IndexController{}, "get:Index")
  67. beego.SetStaticPath("/assets", "static")
  68. if err := beego.AddViewPath("applications/_common/views"); err != nil {
  69. grlogs.Error(err)
  70. }
  71. }
  72. `
  73. var indexcontroller = `
  74. package controllers
  75. import (
  76. )
  77. type IndexController struct {
  78. baseController
  79. }
  80. func (this *IndexController) Index() {
  81. this.Ctx.WriteString("ok")
  82. this.StopRun()
  83. }
  84. `
  85. var basecontroller = `
  86. package controllers
  87. import (
  88. "github.com/astaxie/beego"
  89. "golib.gaore.com/GaoreGo/beegoinit/mvc"
  90. "golib.gaore.com/GaoreGo/grconfig"
  91. "path"
  92. )
  93. type baseController struct {
  94. Configer *grconfig.Configer
  95. mvc.BaseController
  96. }
  97. func (this *baseController) NestPrepare() {
  98. this.Configer = grconfig.New(path.Join(beego.WorkPath, "conf/"))
  99. this.EnableXSRF = false
  100. this.StopRender()
  101. }
  102. `
  103. var errcontroller = `
  104. package controllers
  105. import (
  106. "golib.gaore.com/GaoreGo/beegoinit/mvc"
  107. "golib.gaore.com/GaoreGo/beegoinit/response"
  108. "strings"
  109. )
  110. type ErrorController struct {
  111. mvc.BaseController
  112. }
  113. func (this *ErrorController) NestPrepare() {
  114. this.ViewPath = "applications/_common/views"
  115. }
  116. func (this *ErrorController) Error404() {
  117. if this.IsAjax() || strings.Contains(strings.ToLower(this.Ctx.Input.Header("Accept")), "json") {
  118. resp := response.NewJsonByDefaultFailed()
  119. this.Data["json"] = resp
  120. this.ServeJSON()
  121. } else {
  122. this.Data["content"] = "page not found"
  123. this.TplName = "404.tpl"
  124. }
  125. }
  126. `
  127. var mainfun = `
  128. package main
  129. import (
  130. "github.com/astaxie/beego"
  131. "github.com/astaxie/beego/toolbox"
  132. _ "golib.gaore.com/GaoreGo/beegoinit"
  133. _ "jedi.gaore.com/applications"
  134. )
  135. func main() {
  136. defer toolbox.StopTask()
  137. toolbox.StartTask()
  138. beego.Run()
  139. }
  140. `
  141. var indextpl = `<h1 class="animation-slide-top">400</h1>`
  142. func New(c *cli.Context) error {
  143. var appPath string
  144. var packPath string
  145. if packPath == "." {
  146. packPath = path.Base(appPath)
  147. }
  148. fmt.Println(tree)
  149. appPath = "."
  150. os.Mkdir(path.Join(appPath, "static"), 0755)
  151. os.Mkdir(path.Join(appPath, "conf"), 0755)
  152. os.Mkdir(path.Join(appPath, "cli"), 0755)
  153. os.MkdirAll(path.Join(appPath, "runtime", "logs"), 0755)
  154. os.MkdirAll(path.Join(appPath, "applications", "_common", "controllers"), 0755)
  155. os.MkdirAll(path.Join(appPath, "applications", "_common", "views"), 0755)
  156. os.MkdirAll(path.Join(appPath, "applications", "api", "controlelrs"), 0755)
  157. utils.WriteToFile(path.Join(appPath, "main.go"), mainfun)
  158. utils.WriteToFile(path.Join(appPath, "applications", "init.go"), initfun)
  159. utils.WriteToFile(path.Join(appPath, "applications", "_common", "views", "404.tpl"), indextpl)
  160. utils.WriteToFile(path.Join(appPath, "applications", "_common", "views", "403.tpl"), indextpl)
  161. utils.WriteToFile(path.Join(appPath, "applications", "_common", "controllers", "error.go"), errcontroller)
  162. utils.WriteToFile(path.Join(appPath, "applications", "api", "controllers", "base.go"), basecontroller)
  163. utils.WriteToFile(path.Join(appPath, "applications", "api", "controllers", "index.go"), indexcontroller)
  164. return nil
  165. }