Beispiel #1
0
// Execute invokes the editor for the wrapped view.
func (e *EditableView) Execute(ctx dingo.Context, data interface{}) error {
	ctx.ParseForm()
	if _, ok := ctx.Form["edit"]; !ok || !CanEdit(ctx) {
		return e.View.Execute(ctx, data)
	}

	if ctx.Method == "GET" {
		return e.tmpl.Execute(ctx.Response, editViewData(ctx, e.View))
	} else if ctx.Method != "POST" {
		http.Error(ctx.Response, "Invalid Method", http.StatusMethodNotAllowed)
		return nil
	}

	c := []byte(ctx.FormValue("content"))
	d := new(EditTemplateData)
	d.DingoVer = dingo.VERSION
	d.IsAction = true
	d.DoneURL = ctx.URL.Path
	d.Stylesheets = codeMirrorCSS()
	d.Scripts = codeMirrorJS()
	if err := e.View.Save(ctx, c); err != nil {
		d.Error = err
		d.Content = c
	} else {
		d.WasSaved = true
		d.Content = []byte(e.View.Data(ctx))
	}

	return e.tmpl.Execute(ctx.Response, d)
	// TODO check if this has been updated
	// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/7Ks1iq2s7FA
	// len(edit) == 0 vs edit == ""
}
Beispiel #2
0
// 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)
	}
}
Beispiel #3
0
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)
}
Beispiel #4
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)
}