Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

248 wiersze
5.4 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. "errors"
  69. "github.com/astaxie/beego"
  70. "golib.gaore.com/GaoreGo/beegoinit/response"
  71. router "golib.gaore.com/GaoreGo/beegoinit/routers"
  72. "golib.gaore.com/GaoreGo/grconfig"
  73. "golib.gaore.com/GaoreGo/grlogs"
  74. common "{{ packageName }}/applications/_common/controllers"
  75. api "{{ packageName }}/applications/api/controllers"
  76. "strings"
  77. )
  78. func init() {
  79. router.AddController("api", new(api.IndexController))
  80. // 通用路由处理
  81. router.ErrorController(&common.ErrorController{})
  82. beego.Router("/", &api.IndexController{}, "get:Index")
  83. beego.SetStaticPath("/assets", "static")
  84. if err := beego.AddViewPath("applications/_common/views"); err != nil {
  85. grlogs.Error(err)
  86. }
  87. }
  88. `
  89. var indexcontroller = `
  90. package controllers
  91. import (
  92. )
  93. type IndexController struct {
  94. baseController
  95. }
  96. func (this *IndexController) Index() {
  97. this.Ctx.WriteString("ok")
  98. this.StopRun()
  99. }
  100. `
  101. var basecontroller = `
  102. package controllers
  103. import (
  104. "github.com/astaxie/beego"
  105. "golib.gaore.com/GaoreGo/beegoinit/mvc"
  106. "golib.gaore.com/GaoreGo/grconfig"
  107. "path"
  108. )
  109. type baseController struct {
  110. Configer *grconfig.Configer
  111. mvc.BaseController
  112. }
  113. func (this *baseController) NestPrepare() {
  114. this.Configer = grconfig.New(path.Join(beego.WorkPath, "conf/"))
  115. this.EnableXSRF = false
  116. this.StopRender()
  117. }
  118. `
  119. var errcontroller = `
  120. package controllers
  121. import (
  122. "golib.gaore.com/GaoreGo/beegoinit/mvc"
  123. "golib.gaore.com/GaoreGo/beegoinit/response"
  124. "strings"
  125. )
  126. type ErrorController struct {
  127. mvc.BaseController
  128. }
  129. func (this *ErrorController) NestPrepare() {
  130. this.ViewPath = "applications/_common/views"
  131. }
  132. func (this *ErrorController) Error404() {
  133. if this.IsAjax() || strings.Contains(strings.ToLower(this.Ctx.Input.Header("Accept")), "json") {
  134. resp := response.NewJsonByDefaultFailed()
  135. this.Data["json"] = resp
  136. this.ServeJSON()
  137. } else {
  138. this.Data["content"] = "page not found"
  139. this.TplName = "404.tpl"
  140. }
  141. }
  142. `
  143. var mainfun = `
  144. package main
  145. import (
  146. "github.com/astaxie/beego"
  147. "github.com/astaxie/beego/toolbox"
  148. _ "golib.gaore.com/GaoreGo/beegoinit"
  149. _ "{{ packageName }}/applications"
  150. )
  151. func main() {
  152. defer toolbox.StopTask()
  153. toolbox.StartTask()
  154. beego.Run()
  155. }
  156. `
  157. var indextpl = `<h1 class="animation-slide-top">400</h1>`
  158. func New(c *cli.Context) error {
  159. var appPath string
  160. var packPath string
  161. var packageName string
  162. var err error
  163. if packPath == "." {
  164. packPath = path.Base(appPath)
  165. }
  166. fmt.Println(tree)
  167. appPath = c.String("apppath")
  168. if appPath == "" {
  169. if appPath, err = os.Getwd(); err != nil {
  170. return err
  171. }
  172. }
  173. packageName = c.String("package")
  174. if packageName == "" {
  175. packageName = path.Base(appPath)
  176. }
  177. color.Yellow.Println("Now current working directory is :", appPath)
  178. color.Yellow.Println("Now package name is default to :", packageName)
  179. os.Mkdir(path.Join(appPath, "static"), 0755)
  180. os.Mkdir(path.Join(appPath, "conf"), 0755)
  181. os.Mkdir(path.Join(appPath, "cli"), 0755)
  182. os.MkdirAll(path.Join(appPath, "runtime", "logs"), 0755)
  183. os.MkdirAll(path.Join(appPath, "applications", "_common", "controllers"), 0755)
  184. os.MkdirAll(path.Join(appPath, "applications", "_common", "views"), 0755)
  185. os.MkdirAll(path.Join(appPath, "applications", "api", "controllers"), 0755)
  186. utils.WriteToFile(path.Join(appPath, "main1.go"), strings.ReplaceAll(mainfun, "{{ packageName }}", packageName))
  187. utils.WriteToFile(path.Join(appPath, "applications", "init.go"), strings.ReplaceAll(initfun, "{{ packageName }}", packageName))
  188. utils.WriteToFile(path.Join(appPath, "conf", "app.conf"), strings.ReplaceAll(appconf, "{{ packageName }}", packageName))
  189. utils.WriteToFile(path.Join(appPath, "applications", "_common", "views", "404.tpl"), indextpl)
  190. utils.WriteToFile(path.Join(appPath, "applications", "_common", "views", "403.tpl"), indextpl)
  191. utils.WriteToFile(path.Join(appPath, "applications", "_common", "controllers", "error.go"), errcontroller)
  192. utils.WriteToFile(path.Join(appPath, "applications", "api", "controllers", "base.go"), basecontroller)
  193. utils.WriteToFile(path.Join(appPath, "applications", "api", "controllers", "index.go"), indexcontroller)
  194. color.Yellow.Println("Finish ...")
  195. return nil
  196. }