Esempio n. 1
0
// EditHandler is a dingo.Handler that edits/saves a given template.
func EditHandler(ctx dingo.Context) {
	ctx.ParseForm()
	if !CanEdit(ctx) {
		ctx.HttpError(401)
		return
	}

	var v View
	d := editCtxData(ctx)
	if n, ok := ctx.Form["name"]; !ok {
		editTempl.Execute(ctx.Response, d)
		return
	} else if v, ok = editableViews[n[0]]; !ok {
		d.Error = errors.New(fmt.Sprintf("Template name: `%s` does not exist.", n[0]))
		editTempl.Execute(ctx.Response, d)
		return
	}

	if ctx.Method == "POST" {
		d.IsAction = true
		c := []byte(ctx.FormValue("content"))
		if err := v.Save(ctx, c); err != nil {
			d.Error = err
			d.Content = c
		} else {
			d.WasSaved = true
			d.Content = []byte(v.Data(ctx))
		}
	} else {
		d.Content = []byte(v.Data(ctx))
	}

	editTempl.Execute(ctx.Response, d)
}
Esempio n. 2
0
File: view.go Progetto: juztin/dingo
// Execute invokes a view by key.
func Execute(ctx dingo.Context, key string, data interface{}) {
	if v, ok := viewCol[key]; !ok {
		ctx.HttpError(404)
	} else if err := v.Execute(ctx, data); err != nil {
		// TODO log this somewhere
		log.Println("dingo: template execution error, ", err)
		/* This will cause a warning to be logged from `net/http/server.go`.
		 * The headers have, most likely, been written to the stream. The error is
		 * occuring midway through template processing, which is writing to the response stream.
		 * Server.go logs this; if we don't call the error handler below, then the stream is cut-off
		 * with no other warning to the client, with this they at-least get the 500 template.
		 */
		ctx.HttpError(500)
	}
}
Esempio n. 3
0
File: rest.go Progetto: juztin/dingo
func JSONHandler(fn Handler, ctx dingo.Context) {
	var (
		enc      encoder
		callback string
	)

	ct := ctx.Header.Get("Accept")
	ct, _, _ = mime.ParseMediaType(ct)
	switch ct {
	default:
		ct = "application/json"
		callback = ctx.URL.Query().Get("callback")
		if len(callback) > 0 {
			ct = "application/javascript"
		}
		enc = json.Marshal
	case "application/json":
		enc = json.Marshal
	case "application/xml":
		enc = xml.Marshal
	}

	ctx.Response.Header().Set("Content-Type", ct)
	status, o := fn(ctx)
	ctx.Response.WriteHeader(status)
	if o == nil && len(callback) < 1 {
		return
	}

	data, err := enc(o)
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError)
		return
	}

	if len(callback) > 0 {
		data = pad(data, callback)
	}
	ctx.Response.Write(data)
}