Пример #1
0
func (h Handler) PostArticles(ctx wombat.Context) {
	title := ctx.FormValue("title")
	if request.IsApplicationJson(ctx.Request) {
		ctx.Response.Header().Set("Content-Type", "application/json")
		defer ctx.Body.Close()
		if b, err := ioutil.ReadAll(ctx.Body); err != nil {
			m := make(map[string]interface{})
			json.Unmarshal(b, &m)
			title, _ = m["title"].(string)
		}
	}

	if title == "" {
		// Missing title
		ctx.HttpError(http.StatusBadRequest, GetErrorStr(ctx.Request, "Missing title"))
		return
	}

	t, err := CreateArticle(ctx, title)
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
		return
	}

	// When not a JSON request issue redirect to new article
	if !request.IsApplicationJson(ctx.Request) {
		ctx.Redirect(fmt.Sprintf("%s/%s?view=edit", h.BasePath, t))
	}
}
Пример #2
0
func (h Handler) PutArticle(ctx wombat.Context, titlePath string) {
	ctx.Response.Header().Set("Content-Type", "application/json")

	o, ok := h.Article(titlePath, true)
	if !ok {
		ctx.HttpError(http.StatusNotFound)
		return
	}
	a := h.ItoArticle(o)

	// JSON message
	if request.IsApplicationJson(ctx.Request) {
		// Get the bytes for JSON processing
		defer ctx.Body.Close()
		if data, err := ioutil.ReadAll(ctx.Body); err != nil {
			ctx.HttpError(http.StatusBadRequest, GetError(ctx.Request, err))
		} else {
			JSONHandler(ctx, a, h.ImagePath, data)
		}
	} else if IsImageRequest(ctx) {
		ImagesHandler(ctx, a, h.ImagePath)
	} else {
		// Nothing could be done for the given request
		ctx.HttpError(http.StatusBadRequest)
	}
}
Пример #3
0
func articleResponse(ctx wombat.Context, h *Handler, o interface{}, tmpl, title string) {
	if request.IsApplicationJson(ctx.Request) {
		// JSON
		ctx.Response.Header().Set("Content-Type", "application/json")
		jd, _ := json.Marshal(o)
		ctx.Response.Write(jd)
	} else {
		// HTTP
		data := h.Data(ctx, o, title)
		views.Execute(ctx.Context, h.Templates[tmpl], data)
	}
}
Пример #4
0
func RequireTitleAdmin(fn func(wombat.Context, string)) interface{} {
	return func(ctx wombat.Context, titlePath string) {
		if !ctx.User.IsAdmin() {
			if request.IsApplicationJson(ctx.Request) {
				ctx.Response.Header().Set("Content-Type", "application/json")
			}
			ctx.HttpError(http.StatusUnauthorized)
			return
		}

		fn(ctx, titlePath)
	}
}
Пример #5
0
func RequireAdmin(fn wombat.Handler) wombat.Handler {
	return func(ctx wombat.Context) {
		if !ctx.User.IsAdmin() {
			if request.IsApplicationJson(ctx.Request) {
				ctx.Response.Header().Set("Content-Type", "application/json")
			}
			ctx.HttpError(http.StatusUnauthorized)
			return
		}

		fn(ctx)
	}
}
Пример #6
0
func GetErrorStr(r *http.Request, msg string) string {
	if request.IsApplicationJson(r) {
		msg = fmt.Sprintf(`{"error":"%s"}`, msg)
	}
	return msg
}