1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Improve Response to be more idiomatic

This commit is contained in:
Frédéric Guillot 2017-11-21 18:30:16 -08:00
parent 25cbd65777
commit 02ff7b4bcf
25 changed files with 302 additions and 275 deletions

View file

@ -5,14 +5,15 @@
package core
import (
"log"
"net/http"
"time"
"github.com/miniflux/miniflux2/helper"
"github.com/miniflux/miniflux2/locale"
"github.com/miniflux/miniflux2/server/middleware"
"github.com/miniflux/miniflux2/server/template"
"github.com/miniflux/miniflux2/storage"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
)

View file

@ -5,23 +5,27 @@
package core
import (
"github.com/miniflux/miniflux2/server/template"
"log"
"net/http"
"github.com/miniflux/miniflux2/server/template"
)
type HtmlResponse struct {
// HTMLResponse handles HTML responses.
type HTMLResponse struct {
writer http.ResponseWriter
request *http.Request
template *template.TemplateEngine
}
func (h *HtmlResponse) Render(template string, args map[string]interface{}) {
// Render execute a template and send to the client the generated HTML.
func (h *HTMLResponse) Render(template string, args map[string]interface{}) {
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.template.Execute(h.writer, template, args)
}
func (h *HtmlResponse) ServerError(err error) {
// ServerError sends a 500 error to the browser.
func (h *HTMLResponse) ServerError(err error) {
h.writer.WriteHeader(http.StatusInternalServerError)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
@ -33,7 +37,8 @@ func (h *HtmlResponse) ServerError(err error) {
}
}
func (h *HtmlResponse) BadRequest(err error) {
// BadRequest sends a 400 error to the browser.
func (h *HTMLResponse) BadRequest(err error) {
h.writer.WriteHeader(http.StatusBadRequest)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
@ -45,13 +50,15 @@ func (h *HtmlResponse) BadRequest(err error) {
}
}
func (h *HtmlResponse) NotFound() {
// NotFound sends a 404 error to the browser.
func (h *HTMLResponse) NotFound() {
h.writer.WriteHeader(http.StatusNotFound)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.Write([]byte("Page Not Found"))
}
func (h *HtmlResponse) Forbidden() {
// Forbidden sends a 403 error to the browser.
func (h *HTMLResponse) Forbidden() {
h.writer.WriteHeader(http.StatusForbidden)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.Write([]byte("Access Forbidden"))

View file

@ -11,29 +11,34 @@ import (
"net/http"
)
type JsonResponse struct {
// JSONResponse handles JSON responses.
type JSONResponse struct {
writer http.ResponseWriter
request *http.Request
}
func (j *JsonResponse) Standard(v interface{}) {
// Standard sends a JSON response with the status code 200.
func (j *JSONResponse) Standard(v interface{}) {
j.writer.WriteHeader(http.StatusOK)
j.commonHeaders()
j.writer.Write(j.toJSON(v))
}
func (j *JsonResponse) Created(v interface{}) {
// Created sends a JSON response with the status code 201.
func (j *JSONResponse) Created(v interface{}) {
j.writer.WriteHeader(http.StatusCreated)
j.commonHeaders()
j.writer.Write(j.toJSON(v))
}
func (j *JsonResponse) NoContent() {
// NoContent sends a JSON response with the status code 204.
func (j *JSONResponse) NoContent() {
j.writer.WriteHeader(http.StatusNoContent)
j.commonHeaders()
}
func (j *JsonResponse) BadRequest(err error) {
// BadRequest sends a JSON response with the status code 400.
func (j *JSONResponse) BadRequest(err error) {
log.Println("[API:BadRequest]", err)
j.writer.WriteHeader(http.StatusBadRequest)
j.commonHeaders()
@ -43,14 +48,16 @@ func (j *JsonResponse) BadRequest(err error) {
}
}
func (j *JsonResponse) NotFound(err error) {
// NotFound sends a JSON response with the status code 404.
func (j *JSONResponse) NotFound(err error) {
log.Println("[API:NotFound]", err)
j.writer.WriteHeader(http.StatusNotFound)
j.commonHeaders()
j.writer.Write(j.encodeError(err))
}
func (j *JsonResponse) ServerError(err error) {
// ServerError sends a JSON response with the status code 500.
func (j *JSONResponse) ServerError(err error) {
log.Println("[API:ServerError]", err)
j.writer.WriteHeader(http.StatusInternalServerError)
j.commonHeaders()
@ -60,19 +67,20 @@ func (j *JsonResponse) ServerError(err error) {
}
}
func (j *JsonResponse) Forbidden() {
// Forbidden sends a JSON response with the status code 403.
func (j *JSONResponse) Forbidden() {
log.Println("[API:Forbidden]")
j.writer.WriteHeader(http.StatusForbidden)
j.commonHeaders()
j.writer.Write(j.encodeError(errors.New("Access Forbidden")))
}
func (j *JsonResponse) commonHeaders() {
func (j *JSONResponse) commonHeaders() {
j.writer.Header().Set("Accept", "application/json")
j.writer.Header().Set("Content-Type", "application/json")
}
func (j *JsonResponse) encodeError(err error) []byte {
func (j *JSONResponse) encodeError(err error) []byte {
type errorMsg struct {
ErrorMessage string `json:"error_message"`
}
@ -86,7 +94,7 @@ func (j *JsonResponse) encodeError(err error) []byte {
return data
}
func (j *JsonResponse) toJSON(v interface{}) []byte {
func (j *JSONResponse) toJSON(v interface{}) []byte {
b, err := json.Marshal(v)
if err != nil {
log.Println("Unable to convert interface to JSON:", err)

View file

@ -5,42 +5,50 @@
package core
import (
"github.com/miniflux/miniflux2/server/template"
"net/http"
"time"
"github.com/miniflux/miniflux2/server/template"
)
// Response handles HTTP responses.
type Response struct {
writer http.ResponseWriter
request *http.Request
template *template.TemplateEngine
}
// SetCookie send a cookie to the client.
func (r *Response) SetCookie(cookie *http.Cookie) {
http.SetCookie(r.writer, cookie)
}
func (r *Response) Json() *JsonResponse {
// JSON returns a JSONResponse.
func (r *Response) JSON() *JSONResponse {
r.commonHeaders()
return &JsonResponse{writer: r.writer, request: r.request}
return &JSONResponse{writer: r.writer, request: r.request}
}
func (r *Response) Html() *HtmlResponse {
// HTML returns a HTMLResponse.
func (r *Response) HTML() *HTMLResponse {
r.commonHeaders()
return &HtmlResponse{writer: r.writer, request: r.request, template: r.template}
return &HTMLResponse{writer: r.writer, request: r.request, template: r.template}
}
func (r *Response) Xml() *XmlResponse {
// XML returns a XMLResponse.
func (r *Response) XML() *XMLResponse {
r.commonHeaders()
return &XmlResponse{writer: r.writer, request: r.request}
return &XMLResponse{writer: r.writer, request: r.request}
}
// Redirect redirects the user to another location.
func (r *Response) Redirect(path string) {
http.Redirect(r.writer, r.request, path, http.StatusFound)
}
func (r *Response) Cache(mime_type, etag string, content []byte, duration time.Duration) {
r.writer.Header().Set("Content-Type", mime_type)
// Cache returns a response with caching headers.
func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
r.writer.Header().Set("Content-Type", mimeType)
r.writer.Header().Set("Etag", etag)
r.writer.Header().Set("Cache-Control", "public")
r.writer.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123))
@ -58,6 +66,7 @@ func (r *Response) commonHeaders() {
r.writer.Header().Set("X-Frame-Options", "DENY")
}
// NewResponse returns a new Response.
func NewResponse(w http.ResponseWriter, r *http.Request, template *template.TemplateEngine) *Response {
return &Response{writer: w, request: r, template: template}
}

View file

@ -9,12 +9,14 @@ import (
"net/http"
)
type XmlResponse struct {
// XMLResponse handles XML responses.
type XMLResponse struct {
writer http.ResponseWriter
request *http.Request
}
func (x *XmlResponse) Download(filename, data string) {
// Download force the download of a XML document.
func (x *XMLResponse) Download(filename, data string) {
x.writer.Header().Set("Content-Type", "text/xml")
x.writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
x.writer.Write([]byte(data))