beego.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package beego
  15. import (
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. )
  21. const (
  22. // VERSION represent beego web framework version.
  23. VERSION = "1.10.1"
  24. // DEV is for develop
  25. DEV = "dev"
  26. // PROD is for production
  27. PROD = "prod"
  28. )
  29. // Map shortcut
  30. type M map[string]interface{}
  31. // Hook function to run
  32. type hookfunc func() error
  33. var (
  34. hooks = make([]hookfunc, 0) //hook function slice to store the hookfunc
  35. )
  36. // AddAPPStartHook is used to register the hookfunc
  37. // The hookfuncs will run in beego.Run()
  38. // such as initiating session , starting middleware , building template, starting admin control and so on.
  39. func AddAPPStartHook(hf ...hookfunc) {
  40. hooks = append(hooks, hf...)
  41. }
  42. // Run beego application.
  43. // beego.Run() default run on HttpPort
  44. // beego.Run("localhost")
  45. // beego.Run(":8089")
  46. // beego.Run("127.0.0.1:8089")
  47. func Run(params ...string) {
  48. InitBeforeHTTPRun()
  49. if len(params) > 0 && params[0] != "" {
  50. strs := strings.Split(params[0], ":")
  51. if len(strs) > 0 && strs[0] != "" {
  52. BConfig.Listen.HTTPAddr = strs[0]
  53. }
  54. if len(strs) > 1 && strs[1] != "" {
  55. BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
  56. }
  57. BConfig.Listen.Domains = params
  58. }
  59. BeeApp.Run()
  60. }
  61. // RunWithMiddleWares Run beego application with middlewares.
  62. func RunWithMiddleWares(addr string, mws ...MiddleWare) {
  63. InitBeforeHTTPRun()
  64. strs := strings.Split(addr, ":")
  65. if len(strs) > 0 && strs[0] != "" {
  66. BConfig.Listen.HTTPAddr = strs[0]
  67. BConfig.Listen.Domains = []string{strs[0]}
  68. }
  69. if len(strs) > 1 && strs[1] != "" {
  70. BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
  71. }
  72. BeeApp.Run(mws...)
  73. }
  74. func InitBeforeHTTPRun() {
  75. //init hooks
  76. AddAPPStartHook(
  77. registerMime,
  78. registerDefaultErrorHandler,
  79. registerSession,
  80. registerTemplate,
  81. registerAdmin,
  82. registerGzip,
  83. )
  84. for _, hk := range hooks {
  85. if err := hk(); err != nil {
  86. panic(err)
  87. }
  88. }
  89. }
  90. // TestBeegoInit is for test package init
  91. func TestBeegoInit(ap string) {
  92. path := filepath.Join(ap, "conf", "app.conf")
  93. os.Chdir(ap)
  94. InitBeegoBeforeTest(path)
  95. }
  96. // InitBeegoBeforeTest is for test package init
  97. func InitBeegoBeforeTest(appConfigPath string) {
  98. if err := LoadAppConfig(appConfigProvider, appConfigPath); err != nil {
  99. panic(err)
  100. }
  101. BConfig.RunMode = "test"
  102. InitBeforeHTTPRun()
  103. }