Esempio n. 1
0
func (p *PanicHandler) Err405(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	renderer := render.FromContext(ctx)

	renderer.HTML(rw, 405, "errors/405", map[string]interface{}{
		"Title":         "Method Not Allowed",
		"Nav":           -1,
		"environment":   runMode,
		"authenticated": cas.Username(r),
		"copyright":     time.Now().Year(),
	})
}
Esempio n. 2
0
func (p *PanicHandler) Err403(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	renderer := render.FromContext(ctx)

	data := make(map[string]interface{})
	data["Title"] = "bzzzt..."
	data["Nav"] = -1
	data["environment"] = runMode
	data["authenticated"] = cas.Username(r)
	data["copyright"] = time.Now().Year()

	renderer.HTML(rw, 403, "errors/403", data)
}
Esempio n. 3
0
func (p *PanicHandler) Err550(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	renderer := render.FromContext(ctx)

	data := make(map[string]interface{})
	data["Title"] = "Oh No!"
	//data["Permission"] = r.Form.Get("permission") // FIXME
	data["Nav"] = -1
	data["environment"] = runMode
	data["authenticated"] = cas.Username(r)
	data["copyright"] = time.Now().Year()

	renderer.HTML(rw, 550, "errors/550", data)
}
Esempio n. 4
0
func (p *PanicHandler) Err500(ex interface{}, ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	renderer := render.FromContext(ctx)

	data := make(map[string]interface{})
	data["Title"] = "eek fire FIRE"
	data["Nav"] = -1
	data["environment"] = runMode
	data["authenticated"] = cas.Username(r)
	data["copyright"] = time.Now().Year()
	data["error"] = fmt.Sprintf("%v", ex)

	stackTrace := errors.Wrap(ex, 4).ErrorStack()

	klog.Critical("err  (%v): Internal Server Error: %v", r.RemoteAddr, ex)
	klog.Critical("err  (%v): Stacktrace: %v", r.RemoteAddr, stackTrace)

	if mode := conf.GetDefault("runMode", "dev").(string); mode == "dev" {
		// dump the stacktrace out on the page too
		data["stacktrace"] = stackTrace
	}

	renderer.HTML(rw, 500, "errors/500", data)
}
Esempio n. 5
0
func (p *PanicHandler) Err404(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	renderer := render.FromContext(ctx)

	result := make(map[string]interface{})
	result["Title"] = "i have no idea what i'm doing"
	result["Nav"] = -1
	result["environment"] = runMode
	result["authenticated"] = cas.Username(r)
	result["copyright"] = time.Now().Year()

	if xImg, found := p.Cache.Get("404-xkcd-img"); found {
		result["xkcd_today"] = xImg
		xAlt, _ := p.Cache.Get("404-xkcd-alt")
		result["xkcd_today_alt"] = xAlt
	} else {
		if resp, err := http.Get("https://www.xkcd.com/info.0.json"); err == nil {
			defer resp.Body.Close()

			bte, err := ioutil.ReadAll(resp.Body)

			if err == nil {
				var v map[string]interface{}

				if json.Unmarshal(bte, &v) == nil {
					p.Cache.Set("404-xkcd-img", v["img"].(string))
					p.Cache.Set("404-xkcd-alt", v["alt"].(string))

					result["xkcd_today"] = v["img"]
					result["xkcd_today_title"] = v["alt"]
				}
			}
		}
	}

	renderer.HTML(rw, 404, "errors/404", result)
}
Esempio n. 6
0
func RenderAfterware(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	ret := FromContext(ctx)
	renderer := render.FromContext(ctx)

	// Set the CSRF token
	rw.Header().Set("X-CSRF-Token", csrf.Token(ctx, r))

	switch ret.Type {
	case DataNoRender:
		break
	case DataHTML:
		if ret.Template == "" {
			// guess we're not rendering anything
			break
		}
		if m, ok := ret.Data.(map[string]interface{}); ok {
			// Set the copyright on all pages
			m["copyright"] = time.Now().Year()

			// Add xsrf tokens
			m["xsrf_token"] = csrf.Token(ctx, r)
			m["xsrf_data"] = csrf.TemplateField(ctx, r)

			// Add environment declaration
			m["environment"] = runMode

			// Add Nav info if it doesn't already exist
			if _, ok := m["Nav"]; !ok {
				m["Nav"] = -1
			}

			// Add authentication information
			m["authenticated"] = cas.Username(r)

			// Add session flash stuff
			if f, has := sessionmw.Get(ctx, FlashErr); has {
				m["flash_err"] = f
				sessionmw.Delete(ctx, FlashErr)
			}
			if f, has := sessionmw.Get(ctx, FlashWarn); has {
				m["flash_warn"] = f
				sessionmw.Delete(ctx, FlashWarn)
			}
			if f, has := sessionmw.Get(ctx, FlashInfo); has {
				m["flash_info"] = f
				sessionmw.Delete(ctx, FlashInfo)
			}
		}
		renderer.HTML(rw, ret.Status, ret.Template, ret.Data)
	case DataJSON:
		renderer.JSON(rw, ret.Status, ret.Data)
	case DataBinary:
		renderer.Data(rw, ret.Status, ret.Data.([]byte))
	case DataText:
		renderer.Text(rw, ret.Status, ret.Data.(string))
	case DataJSONP:
		renderer.JSONP(rw, ret.Status, ret.Callback, ret.Data)
	case DataXML:
		renderer.XML(rw, ret.Status, ret.Data)
	default:
		panic("no such data type")
	}
}