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.
 
 
 

244 lines
5.3 KiB

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