beego.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.8.0"
  24. // DEV is for develop
  25. DEV = "dev"
  26. // PROD is for production
  27. PROD = "prod"
  28. )
  29. //hook function to run
  30. type hookfunc func() error
  31. var (
  32. hooks = make([]hookfunc, 0) //hook function slice to store the hookfunc
  33. )
  34. // AddAPPStartHook is used to register the hookfunc
  35. // The hookfuncs will run in beego.Run()
  36. // such as sessionInit, middlerware start, buildtemplate, admin start
  37. func AddAPPStartHook(hf hookfunc) {
  38. hooks = append(hooks, hf)
  39. }
  40. // Run beego application.
  41. // beego.Run() default run on HttpPort
  42. // beego.Run("localhost")
  43. // beego.Run(":8089")
  44. // beego.Run("127.0.0.1:8089")
  45. func Run(params ...string) {
  46. initBeforeHTTPRun()
  47. if len(params) > 0 && params[0] != "" {
  48. strs := strings.Split(params[0], ":")
  49. if len(strs) > 0 && strs[0] != "" {
  50. BConfig.Listen.HTTPAddr = strs[0]
  51. }
  52. if len(strs) > 1 && strs[1] != "" {
  53. BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
  54. }
  55. }
  56. BeeApp.Run()
  57. }
  58. func initBeforeHTTPRun() {
  59. //init hooks
  60. AddAPPStartHook(registerMime)
  61. AddAPPStartHook(registerDefaultErrorHandler)
  62. AddAPPStartHook(registerSession)
  63. AddAPPStartHook(registerTemplate)
  64. AddAPPStartHook(registerAdmin)
  65. AddAPPStartHook(registerGzip)
  66. for _, hk := range hooks {
  67. if err := hk(); err != nil {
  68. panic(err)
  69. }
  70. }
  71. }
  72. // TestBeegoInit is for test package init
  73. func TestBeegoInit(ap string) {
  74. path := filepath.Join(ap, "conf", "app.conf")
  75. os.Chdir(ap)
  76. InitBeegoBeforeTest(path)
  77. }
  78. // InitBeegoBeforeTest is for test package init
  79. func InitBeegoBeforeTest(appConfigPath string) {
  80. if err := LoadAppConfig(appConfigProvider, appConfigPath); err != nil {
  81. panic(err)
  82. }
  83. BConfig.RunMode = "test"
  84. initBeforeHTTPRun()
  85. }