update middleware & beego's error
Showing
4 changed files
with
33 additions
and
299 deletions
| 1 | package beego | 1 | package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "github.com/astaxie/beego/middleware" | ||
| 4 | "github.com/astaxie/beego/session" | 5 | "github.com/astaxie/beego/session" |
| 5 | "net/http" | 6 | "net/http" |
| 6 | "path" | 7 | "path" |
| ... | @@ -25,7 +26,7 @@ func AutoRouter(c ControllerInterface) *App { | ... | @@ -25,7 +26,7 @@ func AutoRouter(c ControllerInterface) *App { |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | func Errorhandler(err string, h http.HandlerFunc) *App { | 28 | func Errorhandler(err string, h http.HandlerFunc) *App { |
| 28 | ErrorMaps[err] = h | 29 | middleware.Errorhandler(err, h) |
| 29 | return BeeApp | 30 | return BeeApp |
| 30 | } | 31 | } |
| 31 | 32 | ||
| ... | @@ -78,6 +79,10 @@ func Run() { | ... | @@ -78,6 +79,10 @@ func Run() { |
| 78 | } | 79 | } |
| 79 | } | 80 | } |
| 80 | } | 81 | } |
| 81 | registerErrorHander() | 82 | |
| 83 | middleware.VERSION = VERSION | ||
| 84 | middleware.AppName = AppName | ||
| 85 | middleware.RegisterErrorHander() | ||
| 86 | |||
| 82 | BeeApp.Run() | 87 | BeeApp.Run() |
| 83 | } | 88 | } | ... | ... |
errors.go
deleted
100644 → 0
| 1 | package beego | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "html/template" | ||
| 6 | "net/http" | ||
| 7 | "runtime" | ||
| 8 | ) | ||
| 9 | |||
| 10 | var tpl = ` | ||
| 11 | <!DOCTYPE html> | ||
| 12 | <html> | ||
| 13 | <head> | ||
| 14 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
| 15 | <title>beego application error</title> | ||
| 16 | <style> | ||
| 17 | html, body, body * {padding: 0; margin: 0;} | ||
| 18 | #header {background:#ffd; border-bottom:solid 2px #A31515; padding: 20px 10px;} | ||
| 19 | #header h2{ } | ||
| 20 | #footer {border-top:solid 1px #aaa; padding: 5px 10px; font-size: 12px; color:green;} | ||
| 21 | #content {padding: 5px;} | ||
| 22 | #content .stack b{ font-size: 13px; color: red;} | ||
| 23 | #content .stack pre{padding-left: 10px;} | ||
| 24 | table {} | ||
| 25 | td.t {text-align: right; padding-right: 5px; color: #888;} | ||
| 26 | </style> | ||
| 27 | <script type="text/javascript"> | ||
| 28 | </script> | ||
| 29 | </head> | ||
| 30 | <body> | ||
| 31 | <div id="header"> | ||
| 32 | <h2>{{.AppError}}</h2> | ||
| 33 | </div> | ||
| 34 | <div id="content"> | ||
| 35 | <table> | ||
| 36 | <tr> | ||
| 37 | <td class="t">Request Method: </td><td>{{.RequestMethod}}</td> | ||
| 38 | </tr> | ||
| 39 | <tr> | ||
| 40 | <td class="t">Request URL: </td><td>{{.RequestURL}}</td> | ||
| 41 | </tr> | ||
| 42 | <tr> | ||
| 43 | <td class="t">RemoteAddr: </td><td>{{.RemoteAddr }}</td> | ||
| 44 | </tr> | ||
| 45 | </table> | ||
| 46 | <div class="stack"> | ||
| 47 | <b>Stack</b> | ||
| 48 | <pre>{{.Stack}}</pre> | ||
| 49 | </div> | ||
| 50 | </div> | ||
| 51 | <div id="footer"> | ||
| 52 | <p>beego {{ .BeegoVersion }} (beego framework)</p> | ||
| 53 | <p>golang version: {{.GoVersion}}</p> | ||
| 54 | </div> | ||
| 55 | </body> | ||
| 56 | </html> | ||
| 57 | ` | ||
| 58 | |||
| 59 | func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) { | ||
| 60 | t, _ := template.New("beegoerrortemp").Parse(tpl) | ||
| 61 | data := make(map[string]string) | ||
| 62 | data["AppError"] = AppName + ":" + fmt.Sprint(err) | ||
| 63 | data["RequestMethod"] = r.Method | ||
| 64 | data["RequestURL"] = r.RequestURI | ||
| 65 | data["RemoteAddr"] = r.RemoteAddr | ||
| 66 | data["Stack"] = Stack | ||
| 67 | data["BeegoVersion"] = VERSION | ||
| 68 | data["GoVersion"] = runtime.Version() | ||
| 69 | t.Execute(rw, data) | ||
| 70 | } | ||
| 71 | |||
| 72 | var errtpl = ` | ||
| 73 | <!DOCTYPE html> | ||
| 74 | <html lang="en"> | ||
| 75 | <head> | ||
| 76 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| 77 | <title>{{.Title}}</title> | ||
| 78 | <style type="text/css"> | ||
| 79 | * { | ||
| 80 | margin:0; | ||
| 81 | padding:0; | ||
| 82 | } | ||
| 83 | |||
| 84 | body { | ||
| 85 | background-color:#EFEFEF; | ||
| 86 | font: .9em "Lucida Sans Unicode", "Lucida Grande", sans-serif; | ||
| 87 | } | ||
| 88 | |||
| 89 | #wrapper{ | ||
| 90 | width:600px; | ||
| 91 | margin:40px auto 0; | ||
| 92 | text-align:center; | ||
| 93 | -moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3); | ||
| 94 | -webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3); | ||
| 95 | box-shadow: 5px 5px 10px rgba(0,0,0,0.3); | ||
| 96 | } | ||
| 97 | |||
| 98 | #wrapper h1{ | ||
| 99 | color:#FFF; | ||
| 100 | text-align:center; | ||
| 101 | margin-bottom:20px; | ||
| 102 | } | ||
| 103 | |||
| 104 | #wrapper a{ | ||
| 105 | display:block; | ||
| 106 | font-size:.9em; | ||
| 107 | padding-top:20px; | ||
| 108 | color:#FFF; | ||
| 109 | text-decoration:none; | ||
| 110 | text-align:center; | ||
| 111 | } | ||
| 112 | |||
| 113 | #container { | ||
| 114 | width:600px; | ||
| 115 | padding-bottom:15px; | ||
| 116 | background-color:#FFFFFF; | ||
| 117 | } | ||
| 118 | |||
| 119 | .navtop{ | ||
| 120 | height:40px; | ||
| 121 | background-color:#24B2EB; | ||
| 122 | padding:13px; | ||
| 123 | } | ||
| 124 | |||
| 125 | .content { | ||
| 126 | padding:10px 10px 25px; | ||
| 127 | background: #FFFFFF; | ||
| 128 | margin:; | ||
| 129 | color:#333; | ||
| 130 | } | ||
| 131 | |||
| 132 | a.button{ | ||
| 133 | color:white; | ||
| 134 | padding:15px 20px; | ||
| 135 | text-shadow:1px 1px 0 #00A5FF; | ||
| 136 | font-weight:bold; | ||
| 137 | text-align:center; | ||
| 138 | border:1px solid #24B2EB; | ||
| 139 | margin:0px 200px; | ||
| 140 | clear:both; | ||
| 141 | background-color: #24B2EB; | ||
| 142 | border-radius:100px; | ||
| 143 | -moz-border-radius:100px; | ||
| 144 | -webkit-border-radius:100px; | ||
| 145 | } | ||
| 146 | |||
| 147 | a.button:hover{ | ||
| 148 | text-decoration:none; | ||
| 149 | background-color: #24B2EB; | ||
| 150 | } | ||
| 151 | |||
| 152 | </style> | ||
| 153 | </head> | ||
| 154 | <body> | ||
| 155 | <div id="wrapper"> | ||
| 156 | <div id="container"> | ||
| 157 | <div class="navtop"> | ||
| 158 | <h1>{{.Title}}</h1> | ||
| 159 | </div> | ||
| 160 | <div id="content"> | ||
| 161 | {{.Content}} | ||
| 162 | <a href="/" title="Home" class="button">Go Home</a><br /> | ||
| 163 | |||
| 164 | <br>power by beego {{.BeegoVersion}} | ||
| 165 | </div> | ||
| 166 | </div> | ||
| 167 | </div> | ||
| 168 | </body> | ||
| 169 | </html> | ||
| 170 | ` | ||
| 171 | |||
| 172 | var ErrorMaps map[string]http.HandlerFunc | ||
| 173 | |||
| 174 | func init() { | ||
| 175 | ErrorMaps = make(map[string]http.HandlerFunc) | ||
| 176 | } | ||
| 177 | |||
| 178 | //404 | ||
| 179 | func NotFound(rw http.ResponseWriter, r *http.Request) { | ||
| 180 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 181 | data := make(map[string]interface{}) | ||
| 182 | data["Title"] = "Page Not Found" | ||
| 183 | data["Content"] = template.HTML("<br>The Page You have requested flown the coop." + | ||
| 184 | "<br>Perhaps you are here because:" + | ||
| 185 | "<br><br><ul>" + | ||
| 186 | "<br>The page has moved" + | ||
| 187 | "<br>The page no longer exists" + | ||
| 188 | "<br>You were looking for your puppy and got lost" + | ||
| 189 | "<br>You like 404 pages" + | ||
| 190 | "</ul>") | ||
| 191 | data["BeegoVersion"] = VERSION | ||
| 192 | rw.WriteHeader(http.StatusNotFound) | ||
| 193 | t.Execute(rw, data) | ||
| 194 | } | ||
| 195 | |||
| 196 | //401 | ||
| 197 | func Unauthorized(rw http.ResponseWriter, r *http.Request) { | ||
| 198 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 199 | data := make(map[string]interface{}) | ||
| 200 | data["Title"] = "Unauthorized" | ||
| 201 | data["Content"] = template.HTML("<br>The Page You have requested can't authorized." + | ||
| 202 | "<br>Perhaps you are here because:" + | ||
| 203 | "<br><br><ul>" + | ||
| 204 | "<br>Check the credentials that you supplied" + | ||
| 205 | "<br>Check the address for errors" + | ||
| 206 | "</ul>") | ||
| 207 | data["BeegoVersion"] = VERSION | ||
| 208 | rw.WriteHeader(http.StatusUnauthorized) | ||
| 209 | t.Execute(rw, data) | ||
| 210 | } | ||
| 211 | |||
| 212 | //403 | ||
| 213 | func Forbidden(rw http.ResponseWriter, r *http.Request) { | ||
| 214 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 215 | data := make(map[string]interface{}) | ||
| 216 | data["Title"] = "Forbidden" | ||
| 217 | data["Content"] = template.HTML("<br>The Page You have requested forbidden." + | ||
| 218 | "<br>Perhaps you are here because:" + | ||
| 219 | "<br><br><ul>" + | ||
| 220 | "<br>Your address may be blocked" + | ||
| 221 | "<br>The site may be disabled" + | ||
| 222 | "<br>You need to log in" + | ||
| 223 | "</ul>") | ||
| 224 | data["BeegoVersion"] = VERSION | ||
| 225 | rw.WriteHeader(http.StatusForbidden) | ||
| 226 | t.Execute(rw, data) | ||
| 227 | } | ||
| 228 | |||
| 229 | //503 | ||
| 230 | func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { | ||
| 231 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 232 | data := make(map[string]interface{}) | ||
| 233 | data["Title"] = "Service Unavailable" | ||
| 234 | data["Content"] = template.HTML("<br>The Page You have requested unavailable." + | ||
| 235 | "<br>Perhaps you are here because:" + | ||
| 236 | "<br><br><ul>" + | ||
| 237 | "<br><br>The page is overloaded" + | ||
| 238 | "<br>Please try again later." + | ||
| 239 | "</ul>") | ||
| 240 | data["BeegoVersion"] = VERSION | ||
| 241 | rw.WriteHeader(http.StatusServiceUnavailable) | ||
| 242 | t.Execute(rw, data) | ||
| 243 | } | ||
| 244 | |||
| 245 | //500 | ||
| 246 | func InternalServerError(rw http.ResponseWriter, r *http.Request) { | ||
| 247 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 248 | data := make(map[string]interface{}) | ||
| 249 | data["Title"] = "Internal Server Error" | ||
| 250 | data["Content"] = template.HTML("<br>The Page You have requested has down now." + | ||
| 251 | "<br><br><ul>" + | ||
| 252 | "<br>simply try again later" + | ||
| 253 | "<br>you should report the fault to the website administrator" + | ||
| 254 | "</ul>") | ||
| 255 | data["BeegoVersion"] = VERSION | ||
| 256 | rw.WriteHeader(http.StatusInternalServerError) | ||
| 257 | t.Execute(rw, data) | ||
| 258 | } | ||
| 259 | |||
| 260 | func registerErrorHander() { | ||
| 261 | if _, ok := ErrorMaps["404"]; !ok { | ||
| 262 | ErrorMaps["404"] = NotFound | ||
| 263 | } | ||
| 264 | |||
| 265 | if _, ok := ErrorMaps["401"]; !ok { | ||
| 266 | ErrorMaps["401"] = Unauthorized | ||
| 267 | } | ||
| 268 | |||
| 269 | if _, ok := ErrorMaps["403"]; !ok { | ||
| 270 | ErrorMaps["403"] = Forbidden | ||
| 271 | } | ||
| 272 | |||
| 273 | if _, ok := ErrorMaps["503"]; !ok { | ||
| 274 | ErrorMaps["503"] = ServiceUnavailable | ||
| 275 | } | ||
| 276 | |||
| 277 | if _, ok := ErrorMaps["500"]; !ok { | ||
| 278 | ErrorMaps["500"] = InternalServerError | ||
| 279 | } | ||
| 280 | } |
| ... | @@ -5,6 +5,7 @@ import ( | ... | @@ -5,6 +5,7 @@ import ( |
| 5 | "html/template" | 5 | "html/template" |
| 6 | "net/http" | 6 | "net/http" |
| 7 | "runtime" | 7 | "runtime" |
| 8 | "strconv" | ||
| 8 | ) | 9 | ) |
| 9 | 10 | ||
| 10 | var ( | 11 | var ( |
| ... | @@ -286,3 +287,22 @@ func RegisterErrorHander() { | ... | @@ -286,3 +287,22 @@ func RegisterErrorHander() { |
| 286 | ErrorMaps["500"] = InternalServerError | 287 | ErrorMaps["500"] = InternalServerError |
| 287 | } | 288 | } |
| 288 | } | 289 | } |
| 290 | |||
| 291 | func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) { | ||
| 292 | if h, ok := ErrorMaps[errcode]; ok { | ||
| 293 | h(w, r) | ||
| 294 | return | ||
| 295 | } else { | ||
| 296 | isint, err := strconv.Atoi(errcode) | ||
| 297 | if err != nil { | ||
| 298 | isint = 500 | ||
| 299 | } | ||
| 300 | if isint == 400 { | ||
| 301 | msg = "404 page not found" | ||
| 302 | } | ||
| 303 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
| 304 | w.WriteHeader(isint) | ||
| 305 | fmt.Fprintln(w, msg) | ||
| 306 | return | ||
| 307 | } | ||
| 308 | } | ... | ... |
| ... | @@ -3,6 +3,7 @@ package beego | ... | @@ -3,6 +3,7 @@ package beego |
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | beecontext "github.com/astaxie/beego/context" | 5 | beecontext "github.com/astaxie/beego/context" |
| 6 | "github.com/astaxie/beego/middleware" | ||
| 6 | "net/http" | 7 | "net/http" |
| 7 | "net/url" | 8 | "net/url" |
| 8 | "os" | 9 | "os" |
| ... | @@ -222,7 +223,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -222,7 +223,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 222 | defer func() { | 223 | defer func() { |
| 223 | if err := recover(); err != nil { | 224 | if err := recover(); err != nil { |
| 224 | errstr := fmt.Sprint(err) | 225 | errstr := fmt.Sprint(err) |
| 225 | if handler, ok := ErrorMaps[errstr]; ok && ErrorsShow { | 226 | if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow { |
| 226 | handler(rw, r) | 227 | handler(rw, r) |
| 227 | } else { | 228 | } else { |
| 228 | if !RecoverPanic { | 229 | if !RecoverPanic { |
| ... | @@ -242,7 +243,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -242,7 +243,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 242 | } | 243 | } |
| 243 | } | 244 | } |
| 244 | if RunMode == "dev" { | 245 | if RunMode == "dev" { |
| 245 | ShowErr(err, rw, r, stack) | 246 | middleware.ShowErr(err, rw, r, stack) |
| 246 | } | 247 | } |
| 247 | } | 248 | } |
| 248 | } | 249 | } |
| ... | @@ -296,15 +297,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -296,15 +297,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 296 | } | 297 | } |
| 297 | //if the request is dir and DirectoryIndex is false then | 298 | //if the request is dir and DirectoryIndex is false then |
| 298 | if finfo.IsDir() && !DirectoryIndex { | 299 | if finfo.IsDir() && !DirectoryIndex { |
| 299 | if h, ok := ErrorMaps["403"]; ok { | 300 | middleware.Exception("403", rw, r, "403 Forbidden") |
| 300 | h(w, r) | 301 | return |
| 301 | return | ||
| 302 | } else { | ||
| 303 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
| 304 | w.WriteHeader(403) | ||
| 305 | fmt.Fprintln(w, "403 Forbidden") | ||
| 306 | return | ||
| 307 | } | ||
| 308 | } | 302 | } |
| 309 | http.ServeFile(w, r, file) | 303 | http.ServeFile(w, r, file) |
| 310 | w.started = true | 304 | w.started = true |
| ... | @@ -641,12 +635,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -641,12 +635,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 641 | Last: | 635 | Last: |
| 642 | //if no matches to url, throw a not found exception | 636 | //if no matches to url, throw a not found exception |
| 643 | if !findrouter { | 637 | if !findrouter { |
| 644 | if h, ok := ErrorMaps["404"]; ok { | 638 | middleware.Exception("404", rw, r, "") |
| 645 | w.status = 404 | ||
| 646 | h(w, r) | ||
| 647 | } else { | ||
| 648 | http.NotFound(w, r) | ||
| 649 | } | ||
| 650 | } | 639 | } |
| 651 | } | 640 | } |
| 652 | 641 | ... | ... |
-
Please register or sign in to post a comment