42637236 by astaxie

modify beego

1 parent 625dec0a
Showing 1 changed file with 127 additions and 127 deletions
1 package beego 1 package beego
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "net/http" 5 "net/http"
6 "os" 6 "os"
7 "path" 7 "path"
8 ) 8 )
9 9
10 var ( 10 var (
11 BeeApp *App 11 BeeApp *App
12 AppName string 12 AppName string
13 AppPath string 13 AppPath string
14 StaticDir map[string]string 14 StaticDir map[string]string
15 HttpAddr string 15 HttpAddr string
16 HttpPort int 16 HttpPort int
17 RecoverPanic bool 17 RecoverPanic bool
18 AutoRender bool 18 AutoRender bool
19 ViewsPath string 19 ViewsPath string
20 RunMode string //"dev" or "prod" 20 RunMode string //"dev" or "prod"
21 AppConfig *Config 21 AppConfig *Config
22 ) 22 )
23 23
24 func init() { 24 func init() {
25 BeeApp = NewApp() 25 BeeApp = NewApp()
26 AppPath, _ = os.Getwd() 26 AppPath, _ = os.Getwd()
27 StaticDir = make(map[string]string) 27 StaticDir = make(map[string]string)
28 var err error 28 var err error
29 AppConfig, err = LoadConfig(path.Join(AppPath, "conf", "app.conf")) 29 AppConfig, err = LoadConfig(path.Join(AppPath, "conf", "app.conf"))
30 if err != nil { 30 if err != nil {
31 //Trace("open Config err:", err) 31 //Trace("open Config err:", err)
32 HttpAddr = "" 32 HttpAddr = ""
33 HttpPort = 8080 33 HttpPort = 8080
34 AppName = "beego" 34 AppName = "beego"
35 RunMode = "prod" 35 RunMode = "prod" //default runmod
36 AutoRender = true 36 AutoRender = true
37 RecoverPanic = true 37 RecoverPanic = true
38 ViewsPath = "views" 38 ViewsPath = "views"
39 } else { 39 } else {
40 HttpAddr = AppConfig.String("httpaddr") 40 HttpAddr = AppConfig.String("httpaddr")
41 if v, err := AppConfig.Int("httpport"); err != nil { 41 if v, err := AppConfig.Int("httpport"); err != nil {
42 HttpPort = 8080 42 HttpPort = 8080
43 } else { 43 } else {
44 HttpPort = v 44 HttpPort = v
45 } 45 }
46 AppName = AppConfig.String("appname") 46 AppName = AppConfig.String("appname")
47 if runmode := AppConfig.String("runmode"); runmode != "" { 47 if runmode := AppConfig.String("runmode"); runmode != "" {
48 RunMode = runmode 48 RunMode = runmode
49 } else { 49 } else {
50 RunMode = "prod" 50 RunMode = "prod"
51 } 51 }
52 if ar, err := AppConfig.Bool("autorender"); err != nil { 52 if ar, err := AppConfig.Bool("autorender"); err != nil {
53 AutoRender = true 53 AutoRender = true
54 } else { 54 } else {
55 AutoRender = ar 55 AutoRender = ar
56 } 56 }
57 if ar, err := AppConfig.Bool("autorecover"); err != nil { 57 if ar, err := AppConfig.Bool("autorecover"); err != nil {
58 RecoverPanic = true 58 RecoverPanic = true
59 } else { 59 } else {
60 RecoverPanic = ar 60 RecoverPanic = ar
61 } 61 }
62 if views := AppConfig.String("viewspath"); views == "" { 62 if views := AppConfig.String("viewspath"); views == "" {
63 ViewsPath = "views" 63 ViewsPath = "views"
64 } else { 64 } else {
65 ViewsPath = views 65 ViewsPath = views
66 } 66 }
67 } 67 }
68 StaticDir["/static"] = "static" 68 StaticDir["/static"] = "static"
69 69
70 } 70 }
71 71
72 type App struct { 72 type App struct {
73 Handlers *ControllerRegistor 73 Handlers *ControllerRegistor
74 } 74 }
75 75
76 // New returns a new PatternServeMux. 76 // New returns a new PatternServeMux.
77 func NewApp() *App { 77 func NewApp() *App {
78 cr := NewControllerRegistor() 78 cr := NewControllerRegistor()
79 app := &App{Handlers: cr} 79 app := &App{Handlers: cr}
80 return app 80 return app
81 } 81 }
82 82
83 func (app *App) Run() { 83 func (app *App) Run() {
84 addr := fmt.Sprintf("%s:%d", HttpAddr, HttpPort) 84 addr := fmt.Sprintf("%s:%d", HttpAddr, HttpPort)
85 err := http.ListenAndServe(addr, app.Handlers) 85 err := http.ListenAndServe(addr, app.Handlers)
86 if err != nil { 86 if err != nil {
87 BeeLogger.Fatal("ListenAndServe: ", err) 87 BeeLogger.Fatal("ListenAndServe: ", err)
88 } 88 }
89 } 89 }
90 90
91 func (app *App) RegisterController(path string, c ControllerInterface) *App { 91 func (app *App) RegisterController(path string, c ControllerInterface) *App {
92 app.Handlers.Add(path, c) 92 app.Handlers.Add(path, c)
93 return app 93 return app
94 } 94 }
95 95
96 func (app *App) Filter(filter http.HandlerFunc) *App { 96 func (app *App) Filter(filter http.HandlerFunc) *App {
97 app.Handlers.Filter(filter) 97 app.Handlers.Filter(filter)
98 return app 98 return app
99 } 99 }
100 100
101 func (app *App) FilterParam(param string, filter http.HandlerFunc) *App { 101 func (app *App) FilterParam(param string, filter http.HandlerFunc) *App {
102 app.Handlers.FilterParam(param, filter) 102 app.Handlers.FilterParam(param, filter)
103 return app 103 return app
104 } 104 }
105 105
106 func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App { 106 func (app *App) FilterPrefixPath(path string, filter http.HandlerFunc) *App {
107 app.Handlers.FilterParam(path, filter) 107 app.Handlers.FilterParam(path, filter)
108 return app 108 return app
109 } 109 }
110 110
111 func (app *App) SetViewsPath(path string) *App { 111 func (app *App) SetViewsPath(path string) *App {
112 ViewsPath = path 112 ViewsPath = path
113 return app 113 return app
114 } 114 }
115 115
116 func (app *App) SetStaticPath(url string, path string) *App { 116 func (app *App) SetStaticPath(url string, path string) *App {
117 StaticDir[url] = path 117 StaticDir[url] = path
118 return app 118 return app
119 } 119 }
120 120
121 func (app *App) ErrorLog(ctx *Context) { 121 func (app *App) ErrorLog(ctx *Context) {
122 BeeLogger.Printf("[ERR] host: '%s', request: '%s %s', proto: '%s', ua: '%s', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr) 122 BeeLogger.Printf("[ERR] host: '%s', request: '%s %s', proto: '%s', ua: '%s', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr)
123 } 123 }
124 124
125 func (app *App) AccessLog(ctx *Context) { 125 func (app *App) AccessLog(ctx *Context) {
126 BeeLogger.Printf("[ACC] host: '%s', request: '%s %s', proto: '%s', ua: %s'', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr) 126 BeeLogger.Printf("[ACC] host: '%s', request: '%s %s', proto: '%s', ua: %s'', remote: '%s'\n", ctx.Request.Host, ctx.Request.Method, ctx.Request.URL.Path, ctx.Request.Proto, ctx.Request.UserAgent(), ctx.Request.RemoteAddr)
127 } 127 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!