From 02ff7b4bcf2b2c691e55ae61fe97bfd9ae37658e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 21 Nov 2017 18:30:16 -0800 Subject: [PATCH] Improve Response to be more idiomatic --- server/api/controller/category.go | 30 +++++------ server/api/controller/entry.go | 42 +++++++-------- server/api/controller/feed.go | 44 ++++++++-------- server/api/controller/subscription.go | 8 +-- server/api/controller/user.go | 56 ++++++++++---------- server/core/handler.go | 7 +-- server/core/html_response.go | 21 +++++--- server/core/json_response.go | 30 +++++++---- server/core/response.go | 27 ++++++---- server/core/xml_response.go | 6 ++- server/ui/controller/about.go | 4 +- server/ui/controller/category.go | 48 ++++++++--------- server/ui/controller/entry.go | 74 +++++++++++++-------------- server/ui/controller/feed.go | 36 ++++++------- server/ui/controller/history.go | 10 ++-- server/ui/controller/icon.go | 6 +-- server/ui/controller/login.go | 8 +-- server/ui/controller/opml.go | 12 ++--- server/ui/controller/proxy.go | 8 +-- server/ui/controller/session.go | 8 +-- server/ui/controller/settings.go | 12 ++--- server/ui/controller/static.go | 2 +- server/ui/controller/subscription.go | 22 ++++---- server/ui/controller/unread.go | 6 +-- server/ui/controller/user.go | 50 +++++++++--------- 25 files changed, 302 insertions(+), 275 deletions(-) diff --git a/server/api/controller/category.go b/server/api/controller/category.go index 3de03217..deaa273d 100644 --- a/server/api/controller/category.go +++ b/server/api/controller/category.go @@ -14,64 +14,64 @@ import ( func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) { category, err := payload.DecodeCategoryPayload(request.Body()) if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } category.UserID = ctx.GetUserID() if err := category.ValidateCategoryCreation(); err != nil { - response.Json().ServerError(err) + response.JSON().ServerError(err) return } err = c.store.CreateCategory(category) if err != nil { - response.Json().ServerError(errors.New("Unable to create this category")) + response.JSON().ServerError(errors.New("Unable to create this category")) return } - response.Json().Created(category) + response.JSON().Created(category) } // UpdateCategory is the API handler to update a category. func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, response *core.Response) { categoryID, err := request.IntegerParam("categoryID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } category, err := payload.DecodeCategoryPayload(request.Body()) if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } category.UserID = ctx.GetUserID() category.ID = categoryID if err := category.ValidateCategoryModification(); err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } err = c.store.UpdateCategory(category) if err != nil { - response.Json().ServerError(errors.New("Unable to update this category")) + response.JSON().ServerError(errors.New("Unable to update this category")) return } - response.Json().Created(category) + response.JSON().Created(category) } // GetCategories is the API handler to get a list of categories for a given user. func (c *Controller) GetCategories(ctx *core.Context, request *core.Request, response *core.Response) { categories, err := c.store.GetCategories(ctx.GetUserID()) if err != nil { - response.Json().ServerError(errors.New("Unable to fetch categories")) + response.JSON().ServerError(errors.New("Unable to fetch categories")) return } - response.Json().Standard(categories) + response.JSON().Standard(categories) } // RemoveCategory is the API handler to remove a category. @@ -79,19 +79,19 @@ func (c *Controller) RemoveCategory(ctx *core.Context, request *core.Request, re userID := ctx.GetUserID() categoryID, err := request.IntegerParam("categoryID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } if !c.store.CategoryExists(userID, categoryID) { - response.Json().NotFound(errors.New("Category not found")) + response.JSON().NotFound(errors.New("Category not found")) return } if err := c.store.RemoveCategory(userID, categoryID); err != nil { - response.Json().ServerError(errors.New("Unable to remove this category")) + response.JSON().ServerError(errors.New("Unable to remove this category")) return } - response.Json().NoContent() + response.JSON().NoContent() } diff --git a/server/api/controller/entry.go b/server/api/controller/entry.go index 2a5f0bd0..d5f5b186 100644 --- a/server/api/controller/entry.go +++ b/server/api/controller/entry.go @@ -16,13 +16,13 @@ func (c *Controller) GetEntry(ctx *core.Context, request *core.Request, response userID := ctx.GetUserID() feedID, err := request.IntegerParam("feedID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } entryID, err := request.IntegerParam("entryID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } @@ -32,16 +32,16 @@ func (c *Controller) GetEntry(ctx *core.Context, request *core.Request, response entry, err := builder.GetEntry() if err != nil { - response.Json().ServerError(errors.New("Unable to fetch this entry from the database")) + response.JSON().ServerError(errors.New("Unable to fetch this entry from the database")) return } if entry == nil { - response.Json().NotFound(errors.New("Entry not found")) + response.JSON().NotFound(errors.New("Entry not found")) return } - response.Json().Standard(entry) + response.JSON().Standard(entry) } // GetFeedEntries is the API handler to get all feed entries. @@ -49,27 +49,27 @@ func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, re userID := ctx.GetUserID() feedID, err := request.IntegerParam("feedID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } status := request.QueryStringParam("status", "") if status != "" { if err := model.ValidateEntryStatus(status); err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } } order := request.QueryStringParam("order", "id") if err := model.ValidateEntryOrder(order); err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } direction := request.QueryStringParam("direction", "desc") if err := model.ValidateDirection(direction); err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } @@ -86,17 +86,17 @@ func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, re entries, err := builder.GetEntries() if err != nil { - response.Json().ServerError(errors.New("Unable to fetch the list of entries")) + response.JSON().ServerError(errors.New("Unable to fetch the list of entries")) return } count, err := builder.CountEntries() if err != nil { - response.Json().ServerError(errors.New("Unable to count the number of entries")) + response.JSON().ServerError(errors.New("Unable to count the number of entries")) return } - response.Json().Standard(&payload.EntriesResponse{Total: count, Entries: entries}) + response.JSON().Standard(&payload.EntriesResponse{Total: count, Entries: entries}) } // SetEntryStatus is the API handler to change the status of an entry. @@ -105,24 +105,24 @@ func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, re feedID, err := request.IntegerParam("feedID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } entryID, err := request.IntegerParam("entryID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } status, err := payload.DecodeEntryStatusPayload(request.Body()) if err != nil { - response.Json().BadRequest(errors.New("Invalid JSON payload")) + response.JSON().BadRequest(errors.New("Invalid JSON payload")) return } if err := model.ValidateEntryStatus(status); err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } @@ -132,25 +132,25 @@ func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, re entry, err := builder.GetEntry() if err != nil { - response.Json().ServerError(errors.New("Unable to fetch this entry from the database")) + response.JSON().ServerError(errors.New("Unable to fetch this entry from the database")) return } if entry == nil { - response.Json().NotFound(errors.New("Entry not found")) + response.JSON().NotFound(errors.New("Entry not found")) return } if err := c.store.SetEntriesStatus(userID, []int64{entry.ID}, status); err != nil { - response.Json().ServerError(errors.New("Unable to change entry status")) + response.JSON().ServerError(errors.New("Unable to change entry status")) return } entry, err = builder.GetEntry() if err != nil { - response.Json().ServerError(errors.New("Unable to fetch this entry from the database")) + response.JSON().ServerError(errors.New("Unable to fetch this entry from the database")) return } - response.Json().Standard(entry) + response.JSON().Standard(entry) } diff --git a/server/api/controller/feed.go b/server/api/controller/feed.go index c9dc9e21..8ec0e046 100644 --- a/server/api/controller/feed.go +++ b/server/api/controller/feed.go @@ -15,17 +15,17 @@ func (c *Controller) CreateFeed(ctx *core.Context, request *core.Request, respon userID := ctx.GetUserID() feedURL, categoryID, err := payload.DecodeFeedCreationPayload(request.Body()) if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL) if err != nil { - response.Json().ServerError(errors.New("Unable to create this feed")) + response.JSON().ServerError(errors.New("Unable to create this feed")) return } - response.Json().Created(feed) + response.JSON().Created(feed) } // RefreshFeed is the API handler to refresh a feed. @@ -33,17 +33,17 @@ func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, respo userID := ctx.GetUserID() feedID, err := request.IntegerParam("feedID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } err = c.feedHandler.RefreshFeed(userID, feedID) if err != nil { - response.Json().ServerError(errors.New("Unable to refresh this feed")) + response.JSON().ServerError(errors.New("Unable to refresh this feed")) return } - response.Json().NoContent() + response.JSON().NoContent() } // UpdateFeed is the API handler that is used to update a feed. @@ -51,45 +51,45 @@ func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, respon userID := ctx.GetUserID() feedID, err := request.IntegerParam("feedID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } newFeed, err := payload.DecodeFeedModificationPayload(request.Body()) if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } originalFeed, err := c.store.GetFeedById(userID, feedID) if err != nil { - response.Json().NotFound(errors.New("Unable to find this feed")) + response.JSON().NotFound(errors.New("Unable to find this feed")) return } if originalFeed == nil { - response.Json().NotFound(errors.New("Feed not found")) + response.JSON().NotFound(errors.New("Feed not found")) return } originalFeed.Merge(newFeed) if err := c.store.UpdateFeed(originalFeed); err != nil { - response.Json().ServerError(errors.New("Unable to update this feed")) + response.JSON().ServerError(errors.New("Unable to update this feed")) return } - response.Json().Created(originalFeed) + response.JSON().Created(originalFeed) } // GetFeeds is the API handler that get all feeds that belongs to the given user. func (c *Controller) GetFeeds(ctx *core.Context, request *core.Request, response *core.Response) { feeds, err := c.store.GetFeeds(ctx.GetUserID()) if err != nil { - response.Json().ServerError(errors.New("Unable to fetch feeds from the database")) + response.JSON().ServerError(errors.New("Unable to fetch feeds from the database")) return } - response.Json().Standard(feeds) + response.JSON().Standard(feeds) } // GetFeed is the API handler to get a feed. @@ -97,22 +97,22 @@ func (c *Controller) GetFeed(ctx *core.Context, request *core.Request, response userID := ctx.GetUserID() feedID, err := request.IntegerParam("feedID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } feed, err := c.store.GetFeedById(userID, feedID) if err != nil { - response.Json().ServerError(errors.New("Unable to fetch this feed")) + response.JSON().ServerError(errors.New("Unable to fetch this feed")) return } if feed == nil { - response.Json().NotFound(errors.New("Feed not found")) + response.JSON().NotFound(errors.New("Feed not found")) return } - response.Json().Standard(feed) + response.JSON().Standard(feed) } // RemoveFeed is the API handler to remove a feed. @@ -120,19 +120,19 @@ func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, respon userID := ctx.GetUserID() feedID, err := request.IntegerParam("feedID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } if !c.store.FeedExists(userID, feedID) { - response.Json().NotFound(errors.New("Feed not found")) + response.JSON().NotFound(errors.New("Feed not found")) return } if err := c.store.RemoveFeed(userID, feedID); err != nil { - response.Json().ServerError(errors.New("Unable to remove this feed")) + response.JSON().ServerError(errors.New("Unable to remove this feed")) return } - response.Json().NoContent() + response.JSON().NoContent() } diff --git a/server/api/controller/subscription.go b/server/api/controller/subscription.go index 700b39c2..b3d62726 100644 --- a/server/api/controller/subscription.go +++ b/server/api/controller/subscription.go @@ -16,20 +16,20 @@ import ( func (c *Controller) GetSubscriptions(ctx *core.Context, request *core.Request, response *core.Response) { websiteURL, err := payload.DecodeURLPayload(request.Body()) if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } subscriptions, err := subscription.FindSubscriptions(websiteURL) if err != nil { - response.Json().ServerError(errors.New("Unable to discover subscriptions")) + response.JSON().ServerError(errors.New("Unable to discover subscriptions")) return } if subscriptions == nil { - response.Json().NotFound(fmt.Errorf("No subscription found")) + response.JSON().NotFound(fmt.Errorf("No subscription found")) return } - response.Json().Standard(subscriptions) + response.JSON().Standard(subscriptions) } diff --git a/server/api/controller/user.go b/server/api/controller/user.go index 62c8a49f..3ea66508 100644 --- a/server/api/controller/user.go +++ b/server/api/controller/user.go @@ -13,151 +13,151 @@ import ( // CreateUser is the API handler to create a new user. func (c *Controller) CreateUser(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { - response.Json().Forbidden() + response.JSON().Forbidden() return } user, err := payload.DecodeUserPayload(request.Body()) if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } if err := user.ValidateUserCreation(); err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } if c.store.UserExists(user.Username) { - response.Json().BadRequest(errors.New("This user already exists")) + response.JSON().BadRequest(errors.New("This user already exists")) return } err = c.store.CreateUser(user) if err != nil { - response.Json().ServerError(errors.New("Unable to create this user")) + response.JSON().ServerError(errors.New("Unable to create this user")) return } user.Password = "" - response.Json().Created(user) + response.JSON().Created(user) } // UpdateUser is the API handler to update the given user. func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { - response.Json().Forbidden() + response.JSON().Forbidden() return } userID, err := request.IntegerParam("userID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } user, err := payload.DecodeUserPayload(request.Body()) if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } if err := user.ValidateUserModification(); err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } originalUser, err := c.store.GetUserById(userID) if err != nil { - response.Json().BadRequest(errors.New("Unable to fetch this user from the database")) + response.JSON().BadRequest(errors.New("Unable to fetch this user from the database")) return } if originalUser == nil { - response.Json().NotFound(errors.New("User not found")) + response.JSON().NotFound(errors.New("User not found")) return } originalUser.Merge(user) if err = c.store.UpdateUser(originalUser); err != nil { - response.Json().ServerError(errors.New("Unable to update this user")) + response.JSON().ServerError(errors.New("Unable to update this user")) return } - response.Json().Created(originalUser) + response.JSON().Created(originalUser) } // GetUsers is the API handler to get the list of users. func (c *Controller) GetUsers(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { - response.Json().Forbidden() + response.JSON().Forbidden() return } users, err := c.store.GetUsers() if err != nil { - response.Json().ServerError(errors.New("Unable to fetch the list of users")) + response.JSON().ServerError(errors.New("Unable to fetch the list of users")) return } - response.Json().Standard(users) + response.JSON().Standard(users) } // GetUser is the API handler to fetch the given user. func (c *Controller) GetUser(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { - response.Json().Forbidden() + response.JSON().Forbidden() return } userID, err := request.IntegerParam("userID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } user, err := c.store.GetUserById(userID) if err != nil { - response.Json().BadRequest(errors.New("Unable to fetch this user from the database")) + response.JSON().BadRequest(errors.New("Unable to fetch this user from the database")) return } if user == nil { - response.Json().NotFound(errors.New("User not found")) + response.JSON().NotFound(errors.New("User not found")) return } - response.Json().Standard(user) + response.JSON().Standard(user) } // RemoveUser is the API handler to remove an existing user. func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { - response.Json().Forbidden() + response.JSON().Forbidden() return } userID, err := request.IntegerParam("userID") if err != nil { - response.Json().BadRequest(err) + response.JSON().BadRequest(err) return } user, err := c.store.GetUserById(userID) if err != nil { - response.Json().ServerError(errors.New("Unable to fetch this user from the database")) + response.JSON().ServerError(errors.New("Unable to fetch this user from the database")) return } if user == nil { - response.Json().NotFound(errors.New("User not found")) + response.JSON().NotFound(errors.New("User not found")) return } if err := c.store.RemoveUser(user.ID); err != nil { - response.Json().BadRequest(errors.New("Unable to remove this user from the database")) + response.JSON().BadRequest(errors.New("Unable to remove this user from the database")) return } - response.Json().NoContent() + response.JSON().NoContent() } diff --git a/server/core/handler.go b/server/core/handler.go index 43205647..2802f318 100644 --- a/server/core/handler.go +++ b/server/core/handler.go @@ -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" ) diff --git a/server/core/html_response.go b/server/core/html_response.go index 9f493d20..f10fe72f 100644 --- a/server/core/html_response.go +++ b/server/core/html_response.go @@ -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")) diff --git a/server/core/json_response.go b/server/core/json_response.go index c72fd731..3e0b0e8f 100644 --- a/server/core/json_response.go +++ b/server/core/json_response.go @@ -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) diff --git a/server/core/response.go b/server/core/response.go index 4acbe95c..48403dd2 100644 --- a/server/core/response.go +++ b/server/core/response.go @@ -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} } diff --git a/server/core/xml_response.go b/server/core/xml_response.go index 6ffd5c9d..e9a2d3fe 100644 --- a/server/core/xml_response.go +++ b/server/core/xml_response.go @@ -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)) diff --git a/server/ui/controller/about.go b/server/ui/controller/about.go index dcfe0d7a..5d9a2427 100644 --- a/server/ui/controller/about.go +++ b/server/ui/controller/about.go @@ -12,11 +12,11 @@ import ( func (c *Controller) AboutPage(ctx *core.Context, request *core.Request, response *core.Response) { args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("about", args.Merge(tplParams{ + response.HTML().Render("about", args.Merge(tplParams{ "version": version.Version, "build_date": version.BuildDate, "menu": "settings", diff --git a/server/ui/controller/category.go b/server/ui/controller/category.go index 6ac536bf..21e70fed 100644 --- a/server/ui/controller/category.go +++ b/server/ui/controller/category.go @@ -17,18 +17,18 @@ import ( func (c *Controller) ShowCategories(ctx *core.Context, request *core.Request, response *core.Response) { args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } user := ctx.GetLoggedUser() categories, err := c.store.GetCategoriesWithFeedCount(user.ID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("categories", args.Merge(tplParams{ + response.HTML().Render("categories", args.Merge(tplParams{ "categories": categories, "total": len(categories), "menu": "categories", @@ -42,7 +42,7 @@ func (c *Controller) ShowCategoryEntries(ctx *core.Context, request *core.Reques args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -61,17 +61,17 @@ func (c *Controller) ShowCategoryEntries(ctx *core.Context, request *core.Reques entries, err := builder.GetEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } count, err := builder.CountEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("category_entries", args.Merge(tplParams{ + response.HTML().Render("category_entries", args.Merge(tplParams{ "category": category, "entries": entries, "total": count, @@ -84,11 +84,11 @@ func (c *Controller) ShowCategoryEntries(ctx *core.Context, request *core.Reques func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) { args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("create_category", args.Merge(tplParams{ + response.HTML().Render("create_category", args.Merge(tplParams{ "menu": "categories", })) } @@ -98,13 +98,13 @@ func (c *Controller) SaveCategory(ctx *core.Context, request *core.Request, resp user := ctx.GetLoggedUser() args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } categoryForm := form.NewCategoryForm(request.Request()) if err := categoryForm.Validate(); err != nil { - response.Html().Render("create_category", args.Merge(tplParams{ + response.HTML().Render("create_category", args.Merge(tplParams{ "errorMessage": err.Error(), })) return @@ -112,12 +112,12 @@ func (c *Controller) SaveCategory(ctx *core.Context, request *core.Request, resp duplicateCategory, err := c.store.GetCategoryByTitle(user.ID, categoryForm.Title) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if duplicateCategory != nil { - response.Html().Render("create_category", args.Merge(tplParams{ + response.HTML().Render("create_category", args.Merge(tplParams{ "errorMessage": "This category already exists.", })) return @@ -127,7 +127,7 @@ func (c *Controller) SaveCategory(ctx *core.Context, request *core.Request, resp err = c.store.CreateCategory(&category) if err != nil { log.Println(err) - response.Html().Render("create_category", args.Merge(tplParams{ + response.HTML().Render("create_category", args.Merge(tplParams{ "errorMessage": "Unable to create this category.", })) return @@ -148,11 +148,11 @@ func (c *Controller) EditCategory(ctx *core.Context, request *core.Request, resp args, err := c.getCategoryFormTemplateArgs(ctx, user, category, nil) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("edit_category", args) + response.HTML().Render("edit_category", args) } // UpdateCategory validate and update a category. @@ -168,19 +168,19 @@ func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, re categoryForm := form.NewCategoryForm(request.Request()) args, err := c.getCategoryFormTemplateArgs(ctx, user, category, categoryForm) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if err := categoryForm.Validate(); err != nil { - response.Html().Render("edit_category", args.Merge(tplParams{ + response.HTML().Render("edit_category", args.Merge(tplParams{ "errorMessage": err.Error(), })) return } if c.store.AnotherCategoryExists(user.ID, category.ID, categoryForm.Title) { - response.Html().Render("edit_category", args.Merge(tplParams{ + response.HTML().Render("edit_category", args.Merge(tplParams{ "errorMessage": "This category already exists.", })) return @@ -189,7 +189,7 @@ func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, re err = c.store.UpdateCategory(categoryForm.Merge(category)) if err != nil { log.Println(err) - response.Html().Render("edit_category", args.Merge(tplParams{ + response.HTML().Render("edit_category", args.Merge(tplParams{ "errorMessage": "Unable to update this category.", })) return @@ -208,7 +208,7 @@ func (c *Controller) RemoveCategory(ctx *core.Context, request *core.Request, re } if err := c.store.RemoveCategory(user.ID, category.ID); err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -218,19 +218,19 @@ func (c *Controller) RemoveCategory(ctx *core.Context, request *core.Request, re func (c *Controller) getCategoryFromURL(ctx *core.Context, request *core.Request, response *core.Response) (*model.Category, error) { categoryID, err := request.IntegerParam("categoryID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return nil, err } user := ctx.GetLoggedUser() category, err := c.store.GetCategory(user.ID, categoryID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return nil, err } if category == nil { - response.Html().NotFound() + response.HTML().NotFound() return nil, errors.New("Category not found") } diff --git a/server/ui/controller/entry.go b/server/ui/controller/entry.go index 17e73c02..1a55a1df 100644 --- a/server/ui/controller/entry.go +++ b/server/ui/controller/entry.go @@ -20,13 +20,13 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res entryID, err := request.IntegerParam("entryID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } feedID, err := request.IntegerParam("feedID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } @@ -37,18 +37,18 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res entry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if entry == nil { - response.Html().NotFound() + response.HTML().NotFound() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -61,7 +61,7 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res builder.WithDirection(model.DefaultSortingDirection) nextEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -74,7 +74,7 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res builder.WithDirection(model.GetOppositeDirection(sortingDirection)) prevEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -91,12 +91,12 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res if entry.Status == model.EntryStatusUnread { err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } } - response.Html().Render("entry", args.Merge(tplParams{ + response.HTML().Render("entry", args.Merge(tplParams{ "entry": entry, "prevEntry": prevEntry, "nextEntry": nextEntry, @@ -113,13 +113,13 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request, categoryID, err := request.IntegerParam("categoryID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } entryID, err := request.IntegerParam("entryID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } @@ -130,18 +130,18 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request, entry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if entry == nil { - response.Html().NotFound() + response.HTML().NotFound() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -154,7 +154,7 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request, builder.WithDirection(sortingDirection) nextEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -167,7 +167,7 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request, builder.WithDirection(model.GetOppositeDirection(sortingDirection)) prevEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -185,12 +185,12 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request, err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead) if err != nil { log.Println(err) - response.Html().ServerError(nil) + response.HTML().ServerError(nil) return } } - response.Html().Render("entry", args.Merge(tplParams{ + response.HTML().Render("entry", args.Merge(tplParams{ "entry": entry, "prevEntry": prevEntry, "nextEntry": nextEntry, @@ -207,7 +207,7 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r entryID, err := request.IntegerParam("entryID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } @@ -217,18 +217,18 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r entry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if entry == nil { - response.Html().NotFound() + response.HTML().NotFound() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -241,7 +241,7 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r builder.WithDirection(sortingDirection) nextEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -254,7 +254,7 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r builder.WithDirection(model.GetOppositeDirection(sortingDirection)) prevEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -272,12 +272,12 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead) if err != nil { log.Println(err) - response.Html().ServerError(nil) + response.HTML().ServerError(nil) return } } - response.Html().Render("entry", args.Merge(tplParams{ + response.HTML().Render("entry", args.Merge(tplParams{ "entry": entry, "prevEntry": prevEntry, "nextEntry": nextEntry, @@ -294,7 +294,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res entryID, err := request.IntegerParam("entryID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } @@ -304,18 +304,18 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res entry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if entry == nil { - response.Html().NotFound() + response.HTML().NotFound() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -328,7 +328,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res builder.WithDirection(sortingDirection) nextEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -341,7 +341,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res builder.WithDirection(model.GetOppositeDirection(sortingDirection)) prevEntry, err := builder.GetEntry() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -355,7 +355,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res prevEntryRoute = ctx.GetRoute("readEntry", "entryID", prevEntry.ID) } - response.Html().Render("entry", args.Merge(tplParams{ + response.HTML().Render("entry", args.Merge(tplParams{ "entry": entry, "prevEntry": prevEntry, "nextEntry": nextEntry, @@ -372,21 +372,21 @@ func (c *Controller) UpdateEntriesStatus(ctx *core.Context, request *core.Reques entryIDs, status, err := payload.DecodeEntryStatusPayload(request.Body()) if err != nil { log.Println(err) - response.Json().BadRequest(nil) + response.JSON().BadRequest(nil) return } if len(entryIDs) == 0 { - response.Json().BadRequest(errors.New("The list of entryID is empty")) + response.JSON().BadRequest(errors.New("The list of entryID is empty")) return } err = c.store.SetEntriesStatus(user.ID, entryIDs, status) if err != nil { log.Println(err) - response.Json().ServerError(nil) + response.JSON().ServerError(nil) return } - response.Json().Standard("OK") + response.JSON().Standard("OK") } diff --git a/server/ui/controller/feed.go b/server/ui/controller/feed.go index ac850fd8..044c7166 100644 --- a/server/ui/controller/feed.go +++ b/server/ui/controller/feed.go @@ -19,17 +19,17 @@ func (c *Controller) ShowFeedsPage(ctx *core.Context, request *core.Request, res args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } feeds, err := c.store.GetFeeds(user.ID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("feeds", args.Merge(tplParams{ + response.HTML().Render("feeds", args.Merge(tplParams{ "feeds": feeds, "total": len(feeds), "menu": "feeds", @@ -43,7 +43,7 @@ func (c *Controller) ShowFeedEntries(ctx *core.Context, request *core.Request, r args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -62,17 +62,17 @@ func (c *Controller) ShowFeedEntries(ctx *core.Context, request *core.Request, r entries, err := builder.GetEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } count, err := builder.CountEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("feed_entries", args.Merge(tplParams{ + response.HTML().Render("feed_entries", args.Merge(tplParams{ "feed": feed, "entries": entries, "total": count, @@ -92,11 +92,11 @@ func (c *Controller) EditFeed(ctx *core.Context, request *core.Request, response args, err := c.getFeedFormTemplateArgs(ctx, user, feed, nil) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("edit_feed", args) + response.HTML().Render("edit_feed", args) } // UpdateFeed update a subscription and redirect to the feed entries page. @@ -111,12 +111,12 @@ func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, respon feedForm := form.NewFeedForm(request.Request()) args, err := c.getFeedFormTemplateArgs(ctx, user, feed, feedForm) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if err := feedForm.ValidateModification(); err != nil { - response.Html().Render("edit_feed", args.Merge(tplParams{ + response.HTML().Render("edit_feed", args.Merge(tplParams{ "errorMessage": err.Error(), })) return @@ -125,7 +125,7 @@ func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, respon err = c.store.UpdateFeed(feedForm.Merge(feed)) if err != nil { log.Println(err) - response.Html().Render("edit_feed", args.Merge(tplParams{ + response.HTML().Render("edit_feed", args.Merge(tplParams{ "errorMessage": "Unable to update this feed.", })) return @@ -138,13 +138,13 @@ func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, respon func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, response *core.Response) { feedID, err := request.IntegerParam("feedID") if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } user := ctx.GetLoggedUser() if err := c.store.RemoveFeed(user.ID, feedID); err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -155,7 +155,7 @@ func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, respon func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, response *core.Response) { feedID, err := request.IntegerParam("feedID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } @@ -170,18 +170,18 @@ func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, respo func (c *Controller) getFeedFromURL(request *core.Request, response *core.Response, user *model.User) (*model.Feed, error) { feedID, err := request.IntegerParam("feedID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return nil, err } feed, err := c.store.GetFeedById(user.ID, feedID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return nil, err } if feed == nil { - response.Html().NotFound() + response.HTML().NotFound() return nil, errors.New("Feed not found") } diff --git a/server/ui/controller/history.go b/server/ui/controller/history.go index 8f130745..dff78be1 100644 --- a/server/ui/controller/history.go +++ b/server/ui/controller/history.go @@ -16,7 +16,7 @@ func (c *Controller) ShowHistoryPage(ctx *core.Context, request *core.Request, r args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -29,17 +29,17 @@ func (c *Controller) ShowHistoryPage(ctx *core.Context, request *core.Request, r entries, err := builder.GetEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } count, err := builder.CountEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("history", args.Merge(tplParams{ + response.HTML().Render("history", args.Merge(tplParams{ "entries": entries, "total": count, "pagination": c.getPagination(ctx.GetRoute("history"), count, offset), @@ -53,7 +53,7 @@ func (c *Controller) FlushHistory(ctx *core.Context, request *core.Request, resp err := c.store.FlushHistory(user.ID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } diff --git a/server/ui/controller/icon.go b/server/ui/controller/icon.go index 36e08b28..0fd85caf 100644 --- a/server/ui/controller/icon.go +++ b/server/ui/controller/icon.go @@ -12,18 +12,18 @@ import ( func (c *Controller) ShowIcon(ctx *core.Context, request *core.Request, response *core.Response) { iconID, err := request.IntegerParam("iconID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } icon, err := c.store.GetIconByID(iconID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if icon == nil { - response.Html().NotFound() + response.HTML().NotFound() return } diff --git a/server/ui/controller/login.go b/server/ui/controller/login.go index 2cb0c500..3bdb3209 100644 --- a/server/ui/controller/login.go +++ b/server/ui/controller/login.go @@ -20,7 +20,7 @@ func (c *Controller) ShowLoginPage(ctx *core.Context, request *core.Request, res return } - response.Html().Render("login", tplParams{ + response.HTML().Render("login", tplParams{ "csrf": ctx.GetCsrfToken(), }) } @@ -34,13 +34,13 @@ func (c *Controller) CheckLogin(ctx *core.Context, request *core.Request, respon if err := authForm.Validate(); err != nil { log.Println(err) - response.Html().Render("login", tplParams) + response.HTML().Render("login", tplParams) return } if err := c.store.CheckPassword(authForm.Username, authForm.Password); err != nil { log.Println(err) - response.Html().Render("login", tplParams) + response.HTML().Render("login", tplParams) return } @@ -50,7 +50,7 @@ func (c *Controller) CheckLogin(ctx *core.Context, request *core.Request, respon realip.RealIP(request.Request()), ) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } diff --git a/server/ui/controller/opml.go b/server/ui/controller/opml.go index 9801ec19..60e27679 100644 --- a/server/ui/controller/opml.go +++ b/server/ui/controller/opml.go @@ -14,21 +14,21 @@ func (c *Controller) Export(ctx *core.Context, request *core.Request, response * user := ctx.GetLoggedUser() opml, err := c.opmlHandler.Export(user.ID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Xml().Download("feeds.opml", opml) + response.XML().Download("feeds.opml", opml) } func (c *Controller) Import(ctx *core.Context, request *core.Request, response *core.Response) { args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("import", args.Merge(tplParams{ + response.HTML().Render("import", args.Merge(tplParams{ "menu": "feeds", })) } @@ -48,11 +48,11 @@ func (c *Controller) UploadOPML(ctx *core.Context, request *core.Request, respon if impErr := c.opmlHandler.Import(user.ID, file); impErr != nil { args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("import", args.Merge(tplParams{ + response.HTML().Render("import", args.Merge(tplParams{ "errorMessage": impErr, "menu": "feeds", })) diff --git a/server/ui/controller/proxy.go b/server/ui/controller/proxy.go index 95e039a4..19a64366 100644 --- a/server/ui/controller/proxy.go +++ b/server/ui/controller/proxy.go @@ -18,26 +18,26 @@ import ( func (c *Controller) ImageProxy(ctx *core.Context, request *core.Request, response *core.Response) { encodedURL := request.StringParam("encodedURL", "") if encodedURL == "" { - response.Html().BadRequest(errors.New("No URL provided")) + response.HTML().BadRequest(errors.New("No URL provided")) return } decodedURL, err := base64.StdEncoding.DecodeString(encodedURL) if err != nil { - response.Html().BadRequest(errors.New("Unable to decode this URL")) + response.HTML().BadRequest(errors.New("Unable to decode this URL")) return } resp, err := http.Get(string(decodedURL)) if err != nil { log.Println(err) - response.Html().NotFound() + response.HTML().NotFound() return } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - response.Html().NotFound() + response.HTML().NotFound() return } diff --git a/server/ui/controller/session.go b/server/ui/controller/session.go index 385ee36d..9ec35066 100644 --- a/server/ui/controller/session.go +++ b/server/ui/controller/session.go @@ -13,18 +13,18 @@ func (c *Controller) ShowSessions(ctx *core.Context, request *core.Request, resp user := ctx.GetLoggedUser() args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } sessions, err := c.store.GetSessions(user.ID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } sessionCookie := request.Cookie("sessionID") - response.Html().Render("sessions", args.Merge(tplParams{ + response.HTML().Render("sessions", args.Merge(tplParams{ "sessions": sessions, "currentSessionToken": sessionCookie, "menu": "settings", @@ -36,7 +36,7 @@ func (c *Controller) RemoveSession(ctx *core.Context, request *core.Request, res sessionID, err := request.IntegerParam("sessionID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return } diff --git a/server/ui/controller/settings.go b/server/ui/controller/settings.go index 66c992c2..3e3b6b38 100644 --- a/server/ui/controller/settings.go +++ b/server/ui/controller/settings.go @@ -17,11 +17,11 @@ func (c *Controller) ShowSettings(ctx *core.Context, request *core.Request, resp args, err := c.getSettingsFormTemplateArgs(ctx, user, nil) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("settings", args) + response.HTML().Render("settings", args) } func (c *Controller) UpdateSettings(ctx *core.Context, request *core.Request, response *core.Response) { @@ -30,12 +30,12 @@ func (c *Controller) UpdateSettings(ctx *core.Context, request *core.Request, re settingsForm := form.NewSettingsForm(request.Request()) args, err := c.getSettingsFormTemplateArgs(ctx, user, settingsForm) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } if err := settingsForm.Validate(); err != nil { - response.Html().Render("settings", args.Merge(tplParams{ + response.HTML().Render("settings", args.Merge(tplParams{ "form": settingsForm, "errorMessage": err.Error(), })) @@ -43,7 +43,7 @@ func (c *Controller) UpdateSettings(ctx *core.Context, request *core.Request, re } if c.store.AnotherUserExists(user.ID, settingsForm.Username) { - response.Html().Render("settings", args.Merge(tplParams{ + response.HTML().Render("settings", args.Merge(tplParams{ "form": settingsForm, "errorMessage": "This user already exists.", })) @@ -53,7 +53,7 @@ func (c *Controller) UpdateSettings(ctx *core.Context, request *core.Request, re err = c.store.UpdateUser(settingsForm.Merge(user)) if err != nil { log.Println(err) - response.Html().Render("settings", args.Merge(tplParams{ + response.HTML().Render("settings", args.Merge(tplParams{ "form": settingsForm, "errorMessage": "Unable to update this user.", })) diff --git a/server/ui/controller/static.go b/server/ui/controller/static.go index a8513501..822118a3 100644 --- a/server/ui/controller/static.go +++ b/server/ui/controller/static.go @@ -33,7 +33,7 @@ func (c *Controller) Favicon(ctx *core.Context, request *core.Request, response blob, err := base64.StdEncoding.DecodeString(static.Binaries["favicon.ico"]) if err != nil { log.Println(err) - response.Html().NotFound() + response.HTML().NotFound() return } diff --git a/server/ui/controller/subscription.go b/server/ui/controller/subscription.go index 88fa077a..a9473bdc 100644 --- a/server/ui/controller/subscription.go +++ b/server/ui/controller/subscription.go @@ -17,11 +17,11 @@ func (c *Controller) AddSubscription(ctx *core.Context, request *core.Request, r args, err := c.getSubscriptionFormTemplateArgs(ctx, user) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("add_subscription", args) + response.HTML().Render("add_subscription", args) } func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request, response *core.Response) { @@ -29,13 +29,13 @@ func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request args, err := c.getSubscriptionFormTemplateArgs(ctx, user) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } subscriptionForm := form.NewSubscriptionForm(request.Request()) if err := subscriptionForm.Validate(); err != nil { - response.Html().Render("add_subscription", args.Merge(tplParams{ + response.HTML().Render("add_subscription", args.Merge(tplParams{ "form": subscriptionForm, "errorMessage": err.Error(), })) @@ -45,7 +45,7 @@ func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request subscriptions, err := subscription.FindSubscriptions(subscriptionForm.URL) if err != nil { log.Println(err) - response.Html().Render("add_subscription", args.Merge(tplParams{ + response.HTML().Render("add_subscription", args.Merge(tplParams{ "form": subscriptionForm, "errorMessage": err, })) @@ -57,14 +57,14 @@ func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request n := len(subscriptions) switch { case n == 0: - response.Html().Render("add_subscription", args.Merge(tplParams{ + response.HTML().Render("add_subscription", args.Merge(tplParams{ "form": subscriptionForm, "errorMessage": "Unable to find any subscription.", })) case n == 1: feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptions[0].URL) if err != nil { - response.Html().Render("add_subscription", args.Merge(tplParams{ + response.HTML().Render("add_subscription", args.Merge(tplParams{ "form": subscriptionForm, "errorMessage": err, })) @@ -73,7 +73,7 @@ func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request response.Redirect(ctx.GetRoute("feedEntries", "feedID", feed.ID)) case n > 1: - response.Html().Render("choose_subscription", args.Merge(tplParams{ + response.HTML().Render("choose_subscription", args.Merge(tplParams{ "categoryID": subscriptionForm.CategoryID, "subscriptions": subscriptions, })) @@ -85,13 +85,13 @@ func (c *Controller) ChooseSubscription(ctx *core.Context, request *core.Request args, err := c.getSubscriptionFormTemplateArgs(ctx, user) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } subscriptionForm := form.NewSubscriptionForm(request.Request()) if err := subscriptionForm.Validate(); err != nil { - response.Html().Render("add_subscription", args.Merge(tplParams{ + response.HTML().Render("add_subscription", args.Merge(tplParams{ "form": subscriptionForm, "errorMessage": err.Error(), })) @@ -100,7 +100,7 @@ func (c *Controller) ChooseSubscription(ctx *core.Context, request *core.Request feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptionForm.URL) if err != nil { - response.Html().Render("add_subscription", args.Merge(tplParams{ + response.HTML().Render("add_subscription", args.Merge(tplParams{ "form": subscriptionForm, "errorMessage": err, })) diff --git a/server/ui/controller/unread.go b/server/ui/controller/unread.go index 59bd9dd5..8457cf44 100644 --- a/server/ui/controller/unread.go +++ b/server/ui/controller/unread.go @@ -23,17 +23,17 @@ func (c *Controller) ShowUnreadPage(ctx *core.Context, request *core.Request, re entries, err := builder.GetEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } countUnread, err := builder.CountEntries() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("unread", tplParams{ + response.HTML().Render("unread", tplParams{ "user": user, "countUnread": countUnread, "entries": entries, diff --git a/server/ui/controller/user.go b/server/ui/controller/user.go index ad6f68f9..01c786ff 100644 --- a/server/ui/controller/user.go +++ b/server/ui/controller/user.go @@ -16,23 +16,23 @@ func (c *Controller) ShowUsers(ctx *core.Context, request *core.Request, respons user := ctx.GetLoggedUser() if !user.IsAdmin { - response.Html().Forbidden() + response.HTML().Forbidden() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } users, err := c.store.GetUsers() if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("users", args.Merge(tplParams{ + response.HTML().Render("users", args.Merge(tplParams{ "users": users, "menu": "settings", })) @@ -42,17 +42,17 @@ func (c *Controller) CreateUser(ctx *core.Context, request *core.Request, respon user := ctx.GetLoggedUser() if !user.IsAdmin { - response.Html().Forbidden() + response.HTML().Forbidden() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } - response.Html().Render("create_user", args.Merge(tplParams{ + response.HTML().Render("create_user", args.Merge(tplParams{ "menu": "settings", "form": &form.UserForm{}, })) @@ -62,19 +62,19 @@ func (c *Controller) SaveUser(ctx *core.Context, request *core.Request, response user := ctx.GetLoggedUser() if !user.IsAdmin { - response.Html().Forbidden() + response.HTML().Forbidden() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } userForm := form.NewUserForm(request.Request()) if err := userForm.ValidateCreation(); err != nil { - response.Html().Render("create_user", args.Merge(tplParams{ + response.HTML().Render("create_user", args.Merge(tplParams{ "menu": "settings", "form": userForm, "errorMessage": err.Error(), @@ -83,7 +83,7 @@ func (c *Controller) SaveUser(ctx *core.Context, request *core.Request, response } if c.store.UserExists(userForm.Username) { - response.Html().Render("create_user", args.Merge(tplParams{ + response.HTML().Render("create_user", args.Merge(tplParams{ "menu": "settings", "form": userForm, "errorMessage": "This user already exists.", @@ -94,7 +94,7 @@ func (c *Controller) SaveUser(ctx *core.Context, request *core.Request, response newUser := userForm.ToUser() if err := c.store.CreateUser(newUser); err != nil { log.Println(err) - response.Html().Render("edit_user", args.Merge(tplParams{ + response.HTML().Render("edit_user", args.Merge(tplParams{ "menu": "settings", "form": userForm, "errorMessage": "Unable to create this user.", @@ -109,13 +109,13 @@ func (c *Controller) EditUser(ctx *core.Context, request *core.Request, response user := ctx.GetLoggedUser() if !user.IsAdmin { - response.Html().Forbidden() + response.HTML().Forbidden() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -124,7 +124,7 @@ func (c *Controller) EditUser(ctx *core.Context, request *core.Request, response return } - response.Html().Render("edit_user", args.Merge(tplParams{ + response.HTML().Render("edit_user", args.Merge(tplParams{ "menu": "settings", "selected_user": selectedUser, "form": &form.UserForm{ @@ -138,13 +138,13 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon user := ctx.GetLoggedUser() if !user.IsAdmin { - response.Html().Forbidden() + response.HTML().Forbidden() return } args, err := c.getCommonTemplateArgs(ctx) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -155,7 +155,7 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon userForm := form.NewUserForm(request.Request()) if err := userForm.ValidateModification(); err != nil { - response.Html().Render("edit_user", args.Merge(tplParams{ + response.HTML().Render("edit_user", args.Merge(tplParams{ "menu": "settings", "selected_user": selectedUser, "form": userForm, @@ -165,7 +165,7 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon } if c.store.AnotherUserExists(selectedUser.ID, userForm.Username) { - response.Html().Render("edit_user", args.Merge(tplParams{ + response.HTML().Render("edit_user", args.Merge(tplParams{ "menu": "settings", "selected_user": selectedUser, "form": userForm, @@ -177,7 +177,7 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon userForm.Merge(selectedUser) if err := c.store.UpdateUser(selectedUser); err != nil { log.Println(err) - response.Html().Render("edit_user", args.Merge(tplParams{ + response.HTML().Render("edit_user", args.Merge(tplParams{ "menu": "settings", "selected_user": selectedUser, "form": userForm, @@ -192,7 +192,7 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) { user := ctx.GetLoggedUser() if !user.IsAdmin { - response.Html().Forbidden() + response.HTML().Forbidden() return } @@ -202,7 +202,7 @@ func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, respon } if err := c.store.RemoveUser(selectedUser.ID); err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return } @@ -212,18 +212,18 @@ func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, respon func (c *Controller) getUserFromURL(ctx *core.Context, request *core.Request, response *core.Response) (*model.User, error) { userID, err := request.IntegerParam("userID") if err != nil { - response.Html().BadRequest(err) + response.HTML().BadRequest(err) return nil, err } user, err := c.store.GetUserById(userID) if err != nil { - response.Html().ServerError(err) + response.HTML().ServerError(err) return nil, err } if user == nil { - response.Html().NotFound() + response.HTML().NotFound() return nil, errors.New("User not found") }