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

@ -14,64 +14,64 @@ import (
func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
category, err := payload.DecodeCategoryPayload(request.Body()) category, err := payload.DecodeCategoryPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
category.UserID = ctx.GetUserID() category.UserID = ctx.GetUserID()
if err := category.ValidateCategoryCreation(); err != nil { if err := category.ValidateCategoryCreation(); err != nil {
response.Json().ServerError(err) response.JSON().ServerError(err)
return return
} }
err = c.store.CreateCategory(category) err = c.store.CreateCategory(category)
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to create this category")) response.JSON().ServerError(errors.New("Unable to create this category"))
return return
} }
response.Json().Created(category) response.JSON().Created(category)
} }
// UpdateCategory is the API handler to update a category. // UpdateCategory is the API handler to update a category.
func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
categoryID, err := request.IntegerParam("categoryID") categoryID, err := request.IntegerParam("categoryID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
category, err := payload.DecodeCategoryPayload(request.Body()) category, err := payload.DecodeCategoryPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
category.UserID = ctx.GetUserID() category.UserID = ctx.GetUserID()
category.ID = categoryID category.ID = categoryID
if err := category.ValidateCategoryModification(); err != nil { if err := category.ValidateCategoryModification(); err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
err = c.store.UpdateCategory(category) err = c.store.UpdateCategory(category)
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to update this category")) response.JSON().ServerError(errors.New("Unable to update this category"))
return return
} }
response.Json().Created(category) response.JSON().Created(category)
} }
// GetCategories is the API handler to get a list of categories for a given user. // 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) { func (c *Controller) GetCategories(ctx *core.Context, request *core.Request, response *core.Response) {
categories, err := c.store.GetCategories(ctx.GetUserID()) categories, err := c.store.GetCategories(ctx.GetUserID())
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to fetch categories")) response.JSON().ServerError(errors.New("Unable to fetch categories"))
return return
} }
response.Json().Standard(categories) response.JSON().Standard(categories)
} }
// RemoveCategory is the API handler to remove a category. // 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() userID := ctx.GetUserID()
categoryID, err := request.IntegerParam("categoryID") categoryID, err := request.IntegerParam("categoryID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
if !c.store.CategoryExists(userID, categoryID) { if !c.store.CategoryExists(userID, categoryID) {
response.Json().NotFound(errors.New("Category not found")) response.JSON().NotFound(errors.New("Category not found"))
return return
} }
if err := c.store.RemoveCategory(userID, categoryID); err != nil { 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 return
} }
response.Json().NoContent() response.JSON().NoContent()
} }

View file

@ -16,13 +16,13 @@ func (c *Controller) GetEntry(ctx *core.Context, request *core.Request, response
userID := ctx.GetUserID() userID := ctx.GetUserID()
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
entryID, err := request.IntegerParam("entryID") entryID, err := request.IntegerParam("entryID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
@ -32,16 +32,16 @@ func (c *Controller) GetEntry(ctx *core.Context, request *core.Request, response
entry, err := builder.GetEntry() entry, err := builder.GetEntry()
if err != nil { 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 return
} }
if entry == nil { if entry == nil {
response.Json().NotFound(errors.New("Entry not found")) response.JSON().NotFound(errors.New("Entry not found"))
return return
} }
response.Json().Standard(entry) response.JSON().Standard(entry)
} }
// GetFeedEntries is the API handler to get all feed entries. // 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() userID := ctx.GetUserID()
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
status := request.QueryStringParam("status", "") status := request.QueryStringParam("status", "")
if status != "" { if status != "" {
if err := model.ValidateEntryStatus(status); err != nil { if err := model.ValidateEntryStatus(status); err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
} }
order := request.QueryStringParam("order", "id") order := request.QueryStringParam("order", "id")
if err := model.ValidateEntryOrder(order); err != nil { if err := model.ValidateEntryOrder(order); err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
direction := request.QueryStringParam("direction", "desc") direction := request.QueryStringParam("direction", "desc")
if err := model.ValidateDirection(direction); err != nil { if err := model.ValidateDirection(direction); err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
@ -86,17 +86,17 @@ func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, re
entries, err := builder.GetEntries() entries, err := builder.GetEntries()
if err != nil { 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 return
} }
count, err := builder.CountEntries() count, err := builder.CountEntries()
if err != nil { 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 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. // 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") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
entryID, err := request.IntegerParam("entryID") entryID, err := request.IntegerParam("entryID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
status, err := payload.DecodeEntryStatusPayload(request.Body()) status, err := payload.DecodeEntryStatusPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(errors.New("Invalid JSON payload")) response.JSON().BadRequest(errors.New("Invalid JSON payload"))
return return
} }
if err := model.ValidateEntryStatus(status); err != nil { if err := model.ValidateEntryStatus(status); err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
@ -132,25 +132,25 @@ func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, re
entry, err := builder.GetEntry() entry, err := builder.GetEntry()
if err != nil { 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 return
} }
if entry == nil { if entry == nil {
response.Json().NotFound(errors.New("Entry not found")) response.JSON().NotFound(errors.New("Entry not found"))
return return
} }
if err := c.store.SetEntriesStatus(userID, []int64{entry.ID}, status); err != nil { 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 return
} }
entry, err = builder.GetEntry() entry, err = builder.GetEntry()
if err != nil { 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 return
} }
response.Json().Standard(entry) response.JSON().Standard(entry)
} }

View file

@ -15,17 +15,17 @@ func (c *Controller) CreateFeed(ctx *core.Context, request *core.Request, respon
userID := ctx.GetUserID() userID := ctx.GetUserID()
feedURL, categoryID, err := payload.DecodeFeedCreationPayload(request.Body()) feedURL, categoryID, err := payload.DecodeFeedCreationPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL) feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL)
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to create this feed")) response.JSON().ServerError(errors.New("Unable to create this feed"))
return return
} }
response.Json().Created(feed) response.JSON().Created(feed)
} }
// RefreshFeed is the API handler to refresh a 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() userID := ctx.GetUserID()
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
err = c.feedHandler.RefreshFeed(userID, feedID) err = c.feedHandler.RefreshFeed(userID, feedID)
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to refresh this feed")) response.JSON().ServerError(errors.New("Unable to refresh this feed"))
return return
} }
response.Json().NoContent() response.JSON().NoContent()
} }
// UpdateFeed is the API handler that is used to update a feed. // 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() userID := ctx.GetUserID()
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
newFeed, err := payload.DecodeFeedModificationPayload(request.Body()) newFeed, err := payload.DecodeFeedModificationPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
originalFeed, err := c.store.GetFeedById(userID, feedID) originalFeed, err := c.store.GetFeedById(userID, feedID)
if err != nil { if err != nil {
response.Json().NotFound(errors.New("Unable to find this feed")) response.JSON().NotFound(errors.New("Unable to find this feed"))
return return
} }
if originalFeed == nil { if originalFeed == nil {
response.Json().NotFound(errors.New("Feed not found")) response.JSON().NotFound(errors.New("Feed not found"))
return return
} }
originalFeed.Merge(newFeed) originalFeed.Merge(newFeed)
if err := c.store.UpdateFeed(originalFeed); err != nil { 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 return
} }
response.Json().Created(originalFeed) response.JSON().Created(originalFeed)
} }
// GetFeeds is the API handler that get all feeds that belongs to the given user. // 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) { func (c *Controller) GetFeeds(ctx *core.Context, request *core.Request, response *core.Response) {
feeds, err := c.store.GetFeeds(ctx.GetUserID()) feeds, err := c.store.GetFeeds(ctx.GetUserID())
if err != nil { 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 return
} }
response.Json().Standard(feeds) response.JSON().Standard(feeds)
} }
// GetFeed is the API handler to get a feed. // 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() userID := ctx.GetUserID()
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
feed, err := c.store.GetFeedById(userID, feedID) feed, err := c.store.GetFeedById(userID, feedID)
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to fetch this feed")) response.JSON().ServerError(errors.New("Unable to fetch this feed"))
return return
} }
if feed == nil { if feed == nil {
response.Json().NotFound(errors.New("Feed not found")) response.JSON().NotFound(errors.New("Feed not found"))
return return
} }
response.Json().Standard(feed) response.JSON().Standard(feed)
} }
// RemoveFeed is the API handler to remove a 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() userID := ctx.GetUserID()
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
if !c.store.FeedExists(userID, feedID) { if !c.store.FeedExists(userID, feedID) {
response.Json().NotFound(errors.New("Feed not found")) response.JSON().NotFound(errors.New("Feed not found"))
return return
} }
if err := c.store.RemoveFeed(userID, feedID); err != nil { 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 return
} }
response.Json().NoContent() response.JSON().NoContent()
} }

View file

@ -16,20 +16,20 @@ import (
func (c *Controller) GetSubscriptions(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) GetSubscriptions(ctx *core.Context, request *core.Request, response *core.Response) {
websiteURL, err := payload.DecodeURLPayload(request.Body()) websiteURL, err := payload.DecodeURLPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
subscriptions, err := subscription.FindSubscriptions(websiteURL) subscriptions, err := subscription.FindSubscriptions(websiteURL)
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to discover subscriptions")) response.JSON().ServerError(errors.New("Unable to discover subscriptions"))
return return
} }
if subscriptions == nil { if subscriptions == nil {
response.Json().NotFound(fmt.Errorf("No subscription found")) response.JSON().NotFound(fmt.Errorf("No subscription found"))
return return
} }
response.Json().Standard(subscriptions) response.JSON().Standard(subscriptions)
} }

View file

@ -13,151 +13,151 @@ import (
// CreateUser is the API handler to create a new user. // CreateUser is the API handler to create a new user.
func (c *Controller) CreateUser(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) CreateUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() { if !ctx.IsAdminUser() {
response.Json().Forbidden() response.JSON().Forbidden()
return return
} }
user, err := payload.DecodeUserPayload(request.Body()) user, err := payload.DecodeUserPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
if err := user.ValidateUserCreation(); err != nil { if err := user.ValidateUserCreation(); err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
if c.store.UserExists(user.Username) { 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 return
} }
err = c.store.CreateUser(user) err = c.store.CreateUser(user)
if err != nil { if err != nil {
response.Json().ServerError(errors.New("Unable to create this user")) response.JSON().ServerError(errors.New("Unable to create this user"))
return return
} }
user.Password = "" user.Password = ""
response.Json().Created(user) response.JSON().Created(user)
} }
// UpdateUser is the API handler to update the given user. // UpdateUser is the API handler to update the given user.
func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() { if !ctx.IsAdminUser() {
response.Json().Forbidden() response.JSON().Forbidden()
return return
} }
userID, err := request.IntegerParam("userID") userID, err := request.IntegerParam("userID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
user, err := payload.DecodeUserPayload(request.Body()) user, err := payload.DecodeUserPayload(request.Body())
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
if err := user.ValidateUserModification(); err != nil { if err := user.ValidateUserModification(); err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
originalUser, err := c.store.GetUserById(userID) originalUser, err := c.store.GetUserById(userID)
if err != nil { 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 return
} }
if originalUser == nil { if originalUser == nil {
response.Json().NotFound(errors.New("User not found")) response.JSON().NotFound(errors.New("User not found"))
return return
} }
originalUser.Merge(user) originalUser.Merge(user)
if err = c.store.UpdateUser(originalUser); err != nil { 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 return
} }
response.Json().Created(originalUser) response.JSON().Created(originalUser)
} }
// GetUsers is the API handler to get the list of users. // GetUsers is the API handler to get the list of users.
func (c *Controller) GetUsers(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) GetUsers(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() { if !ctx.IsAdminUser() {
response.Json().Forbidden() response.JSON().Forbidden()
return return
} }
users, err := c.store.GetUsers() users, err := c.store.GetUsers()
if err != nil { 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 return
} }
response.Json().Standard(users) response.JSON().Standard(users)
} }
// GetUser is the API handler to fetch the given user. // GetUser is the API handler to fetch the given user.
func (c *Controller) GetUser(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) GetUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() { if !ctx.IsAdminUser() {
response.Json().Forbidden() response.JSON().Forbidden()
return return
} }
userID, err := request.IntegerParam("userID") userID, err := request.IntegerParam("userID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
user, err := c.store.GetUserById(userID) user, err := c.store.GetUserById(userID)
if err != nil { 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 return
} }
if user == nil { if user == nil {
response.Json().NotFound(errors.New("User not found")) response.JSON().NotFound(errors.New("User not found"))
return return
} }
response.Json().Standard(user) response.JSON().Standard(user)
} }
// RemoveUser is the API handler to remove an existing user. // RemoveUser is the API handler to remove an existing user.
func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) {
if !ctx.IsAdminUser() { if !ctx.IsAdminUser() {
response.Json().Forbidden() response.JSON().Forbidden()
return return
} }
userID, err := request.IntegerParam("userID") userID, err := request.IntegerParam("userID")
if err != nil { if err != nil {
response.Json().BadRequest(err) response.JSON().BadRequest(err)
return return
} }
user, err := c.store.GetUserById(userID) user, err := c.store.GetUserById(userID)
if err != nil { 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 return
} }
if user == nil { if user == nil {
response.Json().NotFound(errors.New("User not found")) response.JSON().NotFound(errors.New("User not found"))
return return
} }
if err := c.store.RemoveUser(user.ID); err != nil { 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 return
} }
response.Json().NoContent() response.JSON().NoContent()
} }

View file

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

View file

@ -5,23 +5,27 @@
package core package core
import ( import (
"github.com/miniflux/miniflux2/server/template"
"log" "log"
"net/http" "net/http"
"github.com/miniflux/miniflux2/server/template"
) )
type HtmlResponse struct { // HTMLResponse handles HTML responses.
type HTMLResponse struct {
writer http.ResponseWriter writer http.ResponseWriter
request *http.Request request *http.Request
template *template.TemplateEngine 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.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.template.Execute(h.writer, template, args) 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.WriteHeader(http.StatusInternalServerError)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") 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.WriteHeader(http.StatusBadRequest)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") 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.WriteHeader(http.StatusNotFound)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.Write([]byte("Page Not Found")) 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.WriteHeader(http.StatusForbidden)
h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") h.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
h.writer.Write([]byte("Access Forbidden")) h.writer.Write([]byte("Access Forbidden"))

View file

@ -11,29 +11,34 @@ import (
"net/http" "net/http"
) )
type JsonResponse struct { // JSONResponse handles JSON responses.
type JSONResponse struct {
writer http.ResponseWriter writer http.ResponseWriter
request *http.Request 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.writer.WriteHeader(http.StatusOK)
j.commonHeaders() j.commonHeaders()
j.writer.Write(j.toJSON(v)) 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.writer.WriteHeader(http.StatusCreated)
j.commonHeaders() j.commonHeaders()
j.writer.Write(j.toJSON(v)) 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.writer.WriteHeader(http.StatusNoContent)
j.commonHeaders() 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) log.Println("[API:BadRequest]", err)
j.writer.WriteHeader(http.StatusBadRequest) j.writer.WriteHeader(http.StatusBadRequest)
j.commonHeaders() 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) log.Println("[API:NotFound]", err)
j.writer.WriteHeader(http.StatusNotFound) j.writer.WriteHeader(http.StatusNotFound)
j.commonHeaders() j.commonHeaders()
j.writer.Write(j.encodeError(err)) 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) log.Println("[API:ServerError]", err)
j.writer.WriteHeader(http.StatusInternalServerError) j.writer.WriteHeader(http.StatusInternalServerError)
j.commonHeaders() 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]") log.Println("[API:Forbidden]")
j.writer.WriteHeader(http.StatusForbidden) j.writer.WriteHeader(http.StatusForbidden)
j.commonHeaders() j.commonHeaders()
j.writer.Write(j.encodeError(errors.New("Access Forbidden"))) 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("Accept", "application/json")
j.writer.Header().Set("Content-Type", "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 { type errorMsg struct {
ErrorMessage string `json:"error_message"` ErrorMessage string `json:"error_message"`
} }
@ -86,7 +94,7 @@ func (j *JsonResponse) encodeError(err error) []byte {
return data return data
} }
func (j *JsonResponse) toJSON(v interface{}) []byte { func (j *JSONResponse) toJSON(v interface{}) []byte {
b, err := json.Marshal(v) b, err := json.Marshal(v)
if err != nil { if err != nil {
log.Println("Unable to convert interface to JSON:", err) log.Println("Unable to convert interface to JSON:", err)

View file

@ -5,42 +5,50 @@
package core package core
import ( import (
"github.com/miniflux/miniflux2/server/template"
"net/http" "net/http"
"time" "time"
"github.com/miniflux/miniflux2/server/template"
) )
// Response handles HTTP responses.
type Response struct { type Response struct {
writer http.ResponseWriter writer http.ResponseWriter
request *http.Request request *http.Request
template *template.TemplateEngine template *template.TemplateEngine
} }
// SetCookie send a cookie to the client.
func (r *Response) SetCookie(cookie *http.Cookie) { func (r *Response) SetCookie(cookie *http.Cookie) {
http.SetCookie(r.writer, cookie) http.SetCookie(r.writer, cookie)
} }
func (r *Response) Json() *JsonResponse { // JSON returns a JSONResponse.
func (r *Response) JSON() *JSONResponse {
r.commonHeaders() 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() 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() 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) { func (r *Response) Redirect(path string) {
http.Redirect(r.writer, r.request, path, http.StatusFound) http.Redirect(r.writer, r.request, path, http.StatusFound)
} }
func (r *Response) Cache(mime_type, etag string, content []byte, duration time.Duration) { // Cache returns a response with caching headers.
r.writer.Header().Set("Content-Type", mime_type) 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("Etag", etag)
r.writer.Header().Set("Cache-Control", "public") r.writer.Header().Set("Cache-Control", "public")
r.writer.Header().Set("Expires", time.Now().Add(duration).Format(time.RFC1123)) 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") 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 { func NewResponse(w http.ResponseWriter, r *http.Request, template *template.TemplateEngine) *Response {
return &Response{writer: w, request: r, template: template} return &Response{writer: w, request: r, template: template}
} }

View file

@ -9,12 +9,14 @@ import (
"net/http" "net/http"
) )
type XmlResponse struct { // XMLResponse handles XML responses.
type XMLResponse struct {
writer http.ResponseWriter writer http.ResponseWriter
request *http.Request 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-Type", "text/xml")
x.writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename)) x.writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
x.writer.Write([]byte(data)) x.writer.Write([]byte(data))

View file

@ -12,11 +12,11 @@ import (
func (c *Controller) AboutPage(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) AboutPage(ctx *core.Context, request *core.Request, response *core.Response) {
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("about", args.Merge(tplParams{ response.HTML().Render("about", args.Merge(tplParams{
"version": version.Version, "version": version.Version,
"build_date": version.BuildDate, "build_date": version.BuildDate,
"menu": "settings", "menu": "settings",

View file

@ -17,18 +17,18 @@ import (
func (c *Controller) ShowCategories(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) ShowCategories(ctx *core.Context, request *core.Request, response *core.Response) {
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
categories, err := c.store.GetCategoriesWithFeedCount(user.ID) categories, err := c.store.GetCategoriesWithFeedCount(user.ID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("categories", args.Merge(tplParams{ response.HTML().Render("categories", args.Merge(tplParams{
"categories": categories, "categories": categories,
"total": len(categories), "total": len(categories),
"menu": "categories", "menu": "categories",
@ -42,7 +42,7 @@ func (c *Controller) ShowCategoryEntries(ctx *core.Context, request *core.Reques
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -61,17 +61,17 @@ func (c *Controller) ShowCategoryEntries(ctx *core.Context, request *core.Reques
entries, err := builder.GetEntries() entries, err := builder.GetEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
count, err := builder.CountEntries() count, err := builder.CountEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("category_entries", args.Merge(tplParams{ response.HTML().Render("category_entries", args.Merge(tplParams{
"category": category, "category": category,
"entries": entries, "entries": entries,
"total": count, "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) { func (c *Controller) CreateCategory(ctx *core.Context, request *core.Request, response *core.Response) {
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("create_category", args.Merge(tplParams{ response.HTML().Render("create_category", args.Merge(tplParams{
"menu": "categories", "menu": "categories",
})) }))
} }
@ -98,13 +98,13 @@ func (c *Controller) SaveCategory(ctx *core.Context, request *core.Request, resp
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
categoryForm := form.NewCategoryForm(request.Request()) categoryForm := form.NewCategoryForm(request.Request())
if err := categoryForm.Validate(); err != nil { 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(), "errorMessage": err.Error(),
})) }))
return 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) duplicateCategory, err := c.store.GetCategoryByTitle(user.ID, categoryForm.Title)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if duplicateCategory != nil { if duplicateCategory != nil {
response.Html().Render("create_category", args.Merge(tplParams{ response.HTML().Render("create_category", args.Merge(tplParams{
"errorMessage": "This category already exists.", "errorMessage": "This category already exists.",
})) }))
return return
@ -127,7 +127,7 @@ func (c *Controller) SaveCategory(ctx *core.Context, request *core.Request, resp
err = c.store.CreateCategory(&category) err = c.store.CreateCategory(&category)
if err != nil { if err != nil {
log.Println(err) 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.", "errorMessage": "Unable to create this category.",
})) }))
return return
@ -148,11 +148,11 @@ func (c *Controller) EditCategory(ctx *core.Context, request *core.Request, resp
args, err := c.getCategoryFormTemplateArgs(ctx, user, category, nil) args, err := c.getCategoryFormTemplateArgs(ctx, user, category, nil)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("edit_category", args) response.HTML().Render("edit_category", args)
} }
// UpdateCategory validate and update a category. // 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()) categoryForm := form.NewCategoryForm(request.Request())
args, err := c.getCategoryFormTemplateArgs(ctx, user, category, categoryForm) args, err := c.getCategoryFormTemplateArgs(ctx, user, category, categoryForm)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if err := categoryForm.Validate(); err != nil { 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(), "errorMessage": err.Error(),
})) }))
return return
} }
if c.store.AnotherCategoryExists(user.ID, category.ID, categoryForm.Title) { 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.", "errorMessage": "This category already exists.",
})) }))
return return
@ -189,7 +189,7 @@ func (c *Controller) UpdateCategory(ctx *core.Context, request *core.Request, re
err = c.store.UpdateCategory(categoryForm.Merge(category)) err = c.store.UpdateCategory(categoryForm.Merge(category))
if err != nil { if err != nil {
log.Println(err) 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.", "errorMessage": "Unable to update this category.",
})) }))
return 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 { if err := c.store.RemoveCategory(user.ID, category.ID); err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return 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) { func (c *Controller) getCategoryFromURL(ctx *core.Context, request *core.Request, response *core.Response) (*model.Category, error) {
categoryID, err := request.IntegerParam("categoryID") categoryID, err := request.IntegerParam("categoryID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return nil, err return nil, err
} }
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
category, err := c.store.GetCategory(user.ID, categoryID) category, err := c.store.GetCategory(user.ID, categoryID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return nil, err return nil, err
} }
if category == nil { if category == nil {
response.Html().NotFound() response.HTML().NotFound()
return nil, errors.New("Category not found") return nil, errors.New("Category not found")
} }

View file

@ -20,13 +20,13 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res
entryID, err := request.IntegerParam("entryID") entryID, err := request.IntegerParam("entryID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }
@ -37,18 +37,18 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res
entry, err := builder.GetEntry() entry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if entry == nil { if entry == nil {
response.Html().NotFound() response.HTML().NotFound()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -61,7 +61,7 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res
builder.WithDirection(model.DefaultSortingDirection) builder.WithDirection(model.DefaultSortingDirection)
nextEntry, err := builder.GetEntry() nextEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -74,7 +74,7 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res
builder.WithDirection(model.GetOppositeDirection(sortingDirection)) builder.WithDirection(model.GetOppositeDirection(sortingDirection))
prevEntry, err := builder.GetEntry() prevEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -91,12 +91,12 @@ func (c *Controller) ShowFeedEntry(ctx *core.Context, request *core.Request, res
if entry.Status == model.EntryStatusUnread { if entry.Status == model.EntryStatusUnread {
err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead) err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
} }
response.Html().Render("entry", args.Merge(tplParams{ response.HTML().Render("entry", args.Merge(tplParams{
"entry": entry, "entry": entry,
"prevEntry": prevEntry, "prevEntry": prevEntry,
"nextEntry": nextEntry, "nextEntry": nextEntry,
@ -113,13 +113,13 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request,
categoryID, err := request.IntegerParam("categoryID") categoryID, err := request.IntegerParam("categoryID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }
entryID, err := request.IntegerParam("entryID") entryID, err := request.IntegerParam("entryID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }
@ -130,18 +130,18 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request,
entry, err := builder.GetEntry() entry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if entry == nil { if entry == nil {
response.Html().NotFound() response.HTML().NotFound()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -154,7 +154,7 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request,
builder.WithDirection(sortingDirection) builder.WithDirection(sortingDirection)
nextEntry, err := builder.GetEntry() nextEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -167,7 +167,7 @@ func (c *Controller) ShowCategoryEntry(ctx *core.Context, request *core.Request,
builder.WithDirection(model.GetOppositeDirection(sortingDirection)) builder.WithDirection(model.GetOppositeDirection(sortingDirection))
prevEntry, err := builder.GetEntry() prevEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return 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) err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Html().ServerError(nil) response.HTML().ServerError(nil)
return return
} }
} }
response.Html().Render("entry", args.Merge(tplParams{ response.HTML().Render("entry", args.Merge(tplParams{
"entry": entry, "entry": entry,
"prevEntry": prevEntry, "prevEntry": prevEntry,
"nextEntry": nextEntry, "nextEntry": nextEntry,
@ -207,7 +207,7 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r
entryID, err := request.IntegerParam("entryID") entryID, err := request.IntegerParam("entryID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }
@ -217,18 +217,18 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r
entry, err := builder.GetEntry() entry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if entry == nil { if entry == nil {
response.Html().NotFound() response.HTML().NotFound()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -241,7 +241,7 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r
builder.WithDirection(sortingDirection) builder.WithDirection(sortingDirection)
nextEntry, err := builder.GetEntry() nextEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -254,7 +254,7 @@ func (c *Controller) ShowUnreadEntry(ctx *core.Context, request *core.Request, r
builder.WithDirection(model.GetOppositeDirection(sortingDirection)) builder.WithDirection(model.GetOppositeDirection(sortingDirection))
prevEntry, err := builder.GetEntry() prevEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return 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) err = c.store.SetEntriesStatus(user.ID, []int64{entry.ID}, model.EntryStatusRead)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Html().ServerError(nil) response.HTML().ServerError(nil)
return return
} }
} }
response.Html().Render("entry", args.Merge(tplParams{ response.HTML().Render("entry", args.Merge(tplParams{
"entry": entry, "entry": entry,
"prevEntry": prevEntry, "prevEntry": prevEntry,
"nextEntry": nextEntry, "nextEntry": nextEntry,
@ -294,7 +294,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res
entryID, err := request.IntegerParam("entryID") entryID, err := request.IntegerParam("entryID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }
@ -304,18 +304,18 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res
entry, err := builder.GetEntry() entry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if entry == nil { if entry == nil {
response.Html().NotFound() response.HTML().NotFound()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -328,7 +328,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res
builder.WithDirection(sortingDirection) builder.WithDirection(sortingDirection)
nextEntry, err := builder.GetEntry() nextEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -341,7 +341,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res
builder.WithDirection(model.GetOppositeDirection(sortingDirection)) builder.WithDirection(model.GetOppositeDirection(sortingDirection))
prevEntry, err := builder.GetEntry() prevEntry, err := builder.GetEntry()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -355,7 +355,7 @@ func (c *Controller) ShowReadEntry(ctx *core.Context, request *core.Request, res
prevEntryRoute = ctx.GetRoute("readEntry", "entryID", prevEntry.ID) prevEntryRoute = ctx.GetRoute("readEntry", "entryID", prevEntry.ID)
} }
response.Html().Render("entry", args.Merge(tplParams{ response.HTML().Render("entry", args.Merge(tplParams{
"entry": entry, "entry": entry,
"prevEntry": prevEntry, "prevEntry": prevEntry,
"nextEntry": nextEntry, "nextEntry": nextEntry,
@ -372,21 +372,21 @@ func (c *Controller) UpdateEntriesStatus(ctx *core.Context, request *core.Reques
entryIDs, status, err := payload.DecodeEntryStatusPayload(request.Body()) entryIDs, status, err := payload.DecodeEntryStatusPayload(request.Body())
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Json().BadRequest(nil) response.JSON().BadRequest(nil)
return return
} }
if len(entryIDs) == 0 { 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 return
} }
err = c.store.SetEntriesStatus(user.ID, entryIDs, status) err = c.store.SetEntriesStatus(user.ID, entryIDs, status)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Json().ServerError(nil) response.JSON().ServerError(nil)
return return
} }
response.Json().Standard("OK") response.JSON().Standard("OK")
} }

View file

@ -19,17 +19,17 @@ func (c *Controller) ShowFeedsPage(ctx *core.Context, request *core.Request, res
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
feeds, err := c.store.GetFeeds(user.ID) feeds, err := c.store.GetFeeds(user.ID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("feeds", args.Merge(tplParams{ response.HTML().Render("feeds", args.Merge(tplParams{
"feeds": feeds, "feeds": feeds,
"total": len(feeds), "total": len(feeds),
"menu": "feeds", "menu": "feeds",
@ -43,7 +43,7 @@ func (c *Controller) ShowFeedEntries(ctx *core.Context, request *core.Request, r
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -62,17 +62,17 @@ func (c *Controller) ShowFeedEntries(ctx *core.Context, request *core.Request, r
entries, err := builder.GetEntries() entries, err := builder.GetEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
count, err := builder.CountEntries() count, err := builder.CountEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("feed_entries", args.Merge(tplParams{ response.HTML().Render("feed_entries", args.Merge(tplParams{
"feed": feed, "feed": feed,
"entries": entries, "entries": entries,
"total": count, "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) args, err := c.getFeedFormTemplateArgs(ctx, user, feed, nil)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("edit_feed", args) response.HTML().Render("edit_feed", args)
} }
// UpdateFeed update a subscription and redirect to the feed entries page. // 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()) feedForm := form.NewFeedForm(request.Request())
args, err := c.getFeedFormTemplateArgs(ctx, user, feed, feedForm) args, err := c.getFeedFormTemplateArgs(ctx, user, feed, feedForm)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if err := feedForm.ValidateModification(); err != nil { 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(), "errorMessage": err.Error(),
})) }))
return return
@ -125,7 +125,7 @@ func (c *Controller) UpdateFeed(ctx *core.Context, request *core.Request, respon
err = c.store.UpdateFeed(feedForm.Merge(feed)) err = c.store.UpdateFeed(feedForm.Merge(feed))
if err != nil { if err != nil {
log.Println(err) 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.", "errorMessage": "Unable to update this feed.",
})) }))
return 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) { func (c *Controller) RemoveFeed(ctx *core.Context, request *core.Request, response *core.Response) {
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
if err := c.store.RemoveFeed(user.ID, feedID); err != nil { if err := c.store.RemoveFeed(user.ID, feedID); err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return 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) { func (c *Controller) RefreshFeed(ctx *core.Context, request *core.Request, response *core.Response) {
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return 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) { func (c *Controller) getFeedFromURL(request *core.Request, response *core.Response, user *model.User) (*model.Feed, error) {
feedID, err := request.IntegerParam("feedID") feedID, err := request.IntegerParam("feedID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return nil, err return nil, err
} }
feed, err := c.store.GetFeedById(user.ID, feedID) feed, err := c.store.GetFeedById(user.ID, feedID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return nil, err return nil, err
} }
if feed == nil { if feed == nil {
response.Html().NotFound() response.HTML().NotFound()
return nil, errors.New("Feed not found") return nil, errors.New("Feed not found")
} }

View file

@ -16,7 +16,7 @@ func (c *Controller) ShowHistoryPage(ctx *core.Context, request *core.Request, r
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -29,17 +29,17 @@ func (c *Controller) ShowHistoryPage(ctx *core.Context, request *core.Request, r
entries, err := builder.GetEntries() entries, err := builder.GetEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
count, err := builder.CountEntries() count, err := builder.CountEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("history", args.Merge(tplParams{ response.HTML().Render("history", args.Merge(tplParams{
"entries": entries, "entries": entries,
"total": count, "total": count,
"pagination": c.getPagination(ctx.GetRoute("history"), count, offset), "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) err := c.store.FlushHistory(user.ID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }

View file

@ -12,18 +12,18 @@ import (
func (c *Controller) ShowIcon(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) ShowIcon(ctx *core.Context, request *core.Request, response *core.Response) {
iconID, err := request.IntegerParam("iconID") iconID, err := request.IntegerParam("iconID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }
icon, err := c.store.GetIconByID(iconID) icon, err := c.store.GetIconByID(iconID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if icon == nil { if icon == nil {
response.Html().NotFound() response.HTML().NotFound()
return return
} }

View file

@ -20,7 +20,7 @@ func (c *Controller) ShowLoginPage(ctx *core.Context, request *core.Request, res
return return
} }
response.Html().Render("login", tplParams{ response.HTML().Render("login", tplParams{
"csrf": ctx.GetCsrfToken(), "csrf": ctx.GetCsrfToken(),
}) })
} }
@ -34,13 +34,13 @@ func (c *Controller) CheckLogin(ctx *core.Context, request *core.Request, respon
if err := authForm.Validate(); err != nil { if err := authForm.Validate(); err != nil {
log.Println(err) log.Println(err)
response.Html().Render("login", tplParams) response.HTML().Render("login", tplParams)
return return
} }
if err := c.store.CheckPassword(authForm.Username, authForm.Password); err != nil { if err := c.store.CheckPassword(authForm.Username, authForm.Password); err != nil {
log.Println(err) log.Println(err)
response.Html().Render("login", tplParams) response.HTML().Render("login", tplParams)
return return
} }
@ -50,7 +50,7 @@ func (c *Controller) CheckLogin(ctx *core.Context, request *core.Request, respon
realip.RealIP(request.Request()), realip.RealIP(request.Request()),
) )
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }

View file

@ -14,21 +14,21 @@ func (c *Controller) Export(ctx *core.Context, request *core.Request, response *
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
opml, err := c.opmlHandler.Export(user.ID) opml, err := c.opmlHandler.Export(user.ID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return 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) { func (c *Controller) Import(ctx *core.Context, request *core.Request, response *core.Response) {
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("import", args.Merge(tplParams{ response.HTML().Render("import", args.Merge(tplParams{
"menu": "feeds", "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 { if impErr := c.opmlHandler.Import(user.ID, file); impErr != nil {
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("import", args.Merge(tplParams{ response.HTML().Render("import", args.Merge(tplParams{
"errorMessage": impErr, "errorMessage": impErr,
"menu": "feeds", "menu": "feeds",
})) }))

View file

@ -18,26 +18,26 @@ import (
func (c *Controller) ImageProxy(ctx *core.Context, request *core.Request, response *core.Response) { func (c *Controller) ImageProxy(ctx *core.Context, request *core.Request, response *core.Response) {
encodedURL := request.StringParam("encodedURL", "") encodedURL := request.StringParam("encodedURL", "")
if encodedURL == "" { if encodedURL == "" {
response.Html().BadRequest(errors.New("No URL provided")) response.HTML().BadRequest(errors.New("No URL provided"))
return return
} }
decodedURL, err := base64.StdEncoding.DecodeString(encodedURL) decodedURL, err := base64.StdEncoding.DecodeString(encodedURL)
if err != nil { if err != nil {
response.Html().BadRequest(errors.New("Unable to decode this URL")) response.HTML().BadRequest(errors.New("Unable to decode this URL"))
return return
} }
resp, err := http.Get(string(decodedURL)) resp, err := http.Get(string(decodedURL))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Html().NotFound() response.HTML().NotFound()
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
response.Html().NotFound() response.HTML().NotFound()
return return
} }

View file

@ -13,18 +13,18 @@ func (c *Controller) ShowSessions(ctx *core.Context, request *core.Request, resp
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
sessions, err := c.store.GetSessions(user.ID) sessions, err := c.store.GetSessions(user.ID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
sessionCookie := request.Cookie("sessionID") sessionCookie := request.Cookie("sessionID")
response.Html().Render("sessions", args.Merge(tplParams{ response.HTML().Render("sessions", args.Merge(tplParams{
"sessions": sessions, "sessions": sessions,
"currentSessionToken": sessionCookie, "currentSessionToken": sessionCookie,
"menu": "settings", "menu": "settings",
@ -36,7 +36,7 @@ func (c *Controller) RemoveSession(ctx *core.Context, request *core.Request, res
sessionID, err := request.IntegerParam("sessionID") sessionID, err := request.IntegerParam("sessionID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return return
} }

View file

@ -17,11 +17,11 @@ func (c *Controller) ShowSettings(ctx *core.Context, request *core.Request, resp
args, err := c.getSettingsFormTemplateArgs(ctx, user, nil) args, err := c.getSettingsFormTemplateArgs(ctx, user, nil)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("settings", args) response.HTML().Render("settings", args)
} }
func (c *Controller) UpdateSettings(ctx *core.Context, request *core.Request, response *core.Response) { 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()) settingsForm := form.NewSettingsForm(request.Request())
args, err := c.getSettingsFormTemplateArgs(ctx, user, settingsForm) args, err := c.getSettingsFormTemplateArgs(ctx, user, settingsForm)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
if err := settingsForm.Validate(); err != nil { if err := settingsForm.Validate(); err != nil {
response.Html().Render("settings", args.Merge(tplParams{ response.HTML().Render("settings", args.Merge(tplParams{
"form": settingsForm, "form": settingsForm,
"errorMessage": err.Error(), "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) { if c.store.AnotherUserExists(user.ID, settingsForm.Username) {
response.Html().Render("settings", args.Merge(tplParams{ response.HTML().Render("settings", args.Merge(tplParams{
"form": settingsForm, "form": settingsForm,
"errorMessage": "This user already exists.", "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)) err = c.store.UpdateUser(settingsForm.Merge(user))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Html().Render("settings", args.Merge(tplParams{ response.HTML().Render("settings", args.Merge(tplParams{
"form": settingsForm, "form": settingsForm,
"errorMessage": "Unable to update this user.", "errorMessage": "Unable to update this user.",
})) }))

View file

@ -33,7 +33,7 @@ func (c *Controller) Favicon(ctx *core.Context, request *core.Request, response
blob, err := base64.StdEncoding.DecodeString(static.Binaries["favicon.ico"]) blob, err := base64.StdEncoding.DecodeString(static.Binaries["favicon.ico"])
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Html().NotFound() response.HTML().NotFound()
return return
} }

View file

@ -17,11 +17,11 @@ func (c *Controller) AddSubscription(ctx *core.Context, request *core.Request, r
args, err := c.getSubscriptionFormTemplateArgs(ctx, user) args, err := c.getSubscriptionFormTemplateArgs(ctx, user)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return 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) { 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) args, err := c.getSubscriptionFormTemplateArgs(ctx, user)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
subscriptionForm := form.NewSubscriptionForm(request.Request()) subscriptionForm := form.NewSubscriptionForm(request.Request())
if err := subscriptionForm.Validate(); err != nil { if err := subscriptionForm.Validate(); err != nil {
response.Html().Render("add_subscription", args.Merge(tplParams{ response.HTML().Render("add_subscription", args.Merge(tplParams{
"form": subscriptionForm, "form": subscriptionForm,
"errorMessage": err.Error(), "errorMessage": err.Error(),
})) }))
@ -45,7 +45,7 @@ func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request
subscriptions, err := subscription.FindSubscriptions(subscriptionForm.URL) subscriptions, err := subscription.FindSubscriptions(subscriptionForm.URL)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
response.Html().Render("add_subscription", args.Merge(tplParams{ response.HTML().Render("add_subscription", args.Merge(tplParams{
"form": subscriptionForm, "form": subscriptionForm,
"errorMessage": err, "errorMessage": err,
})) }))
@ -57,14 +57,14 @@ func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request
n := len(subscriptions) n := len(subscriptions)
switch { switch {
case n == 0: case n == 0:
response.Html().Render("add_subscription", args.Merge(tplParams{ response.HTML().Render("add_subscription", args.Merge(tplParams{
"form": subscriptionForm, "form": subscriptionForm,
"errorMessage": "Unable to find any subscription.", "errorMessage": "Unable to find any subscription.",
})) }))
case n == 1: case n == 1:
feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptions[0].URL) feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptions[0].URL)
if err != nil { if err != nil {
response.Html().Render("add_subscription", args.Merge(tplParams{ response.HTML().Render("add_subscription", args.Merge(tplParams{
"form": subscriptionForm, "form": subscriptionForm,
"errorMessage": err, "errorMessage": err,
})) }))
@ -73,7 +73,7 @@ func (c *Controller) SubmitSubscription(ctx *core.Context, request *core.Request
response.Redirect(ctx.GetRoute("feedEntries", "feedID", feed.ID)) response.Redirect(ctx.GetRoute("feedEntries", "feedID", feed.ID))
case n > 1: case n > 1:
response.Html().Render("choose_subscription", args.Merge(tplParams{ response.HTML().Render("choose_subscription", args.Merge(tplParams{
"categoryID": subscriptionForm.CategoryID, "categoryID": subscriptionForm.CategoryID,
"subscriptions": subscriptions, "subscriptions": subscriptions,
})) }))
@ -85,13 +85,13 @@ func (c *Controller) ChooseSubscription(ctx *core.Context, request *core.Request
args, err := c.getSubscriptionFormTemplateArgs(ctx, user) args, err := c.getSubscriptionFormTemplateArgs(ctx, user)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
subscriptionForm := form.NewSubscriptionForm(request.Request()) subscriptionForm := form.NewSubscriptionForm(request.Request())
if err := subscriptionForm.Validate(); err != nil { if err := subscriptionForm.Validate(); err != nil {
response.Html().Render("add_subscription", args.Merge(tplParams{ response.HTML().Render("add_subscription", args.Merge(tplParams{
"form": subscriptionForm, "form": subscriptionForm,
"errorMessage": err.Error(), "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) feed, err := c.feedHandler.CreateFeed(user.ID, subscriptionForm.CategoryID, subscriptionForm.URL)
if err != nil { if err != nil {
response.Html().Render("add_subscription", args.Merge(tplParams{ response.HTML().Render("add_subscription", args.Merge(tplParams{
"form": subscriptionForm, "form": subscriptionForm,
"errorMessage": err, "errorMessage": err,
})) }))

View file

@ -23,17 +23,17 @@ func (c *Controller) ShowUnreadPage(ctx *core.Context, request *core.Request, re
entries, err := builder.GetEntries() entries, err := builder.GetEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
countUnread, err := builder.CountEntries() countUnread, err := builder.CountEntries()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("unread", tplParams{ response.HTML().Render("unread", tplParams{
"user": user, "user": user,
"countUnread": countUnread, "countUnread": countUnread,
"entries": entries, "entries": entries,

View file

@ -16,23 +16,23 @@ func (c *Controller) ShowUsers(ctx *core.Context, request *core.Request, respons
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
if !user.IsAdmin { if !user.IsAdmin {
response.Html().Forbidden() response.HTML().Forbidden()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
users, err := c.store.GetUsers() users, err := c.store.GetUsers()
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("users", args.Merge(tplParams{ response.HTML().Render("users", args.Merge(tplParams{
"users": users, "users": users,
"menu": "settings", "menu": "settings",
})) }))
@ -42,17 +42,17 @@ func (c *Controller) CreateUser(ctx *core.Context, request *core.Request, respon
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
if !user.IsAdmin { if !user.IsAdmin {
response.Html().Forbidden() response.HTML().Forbidden()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
response.Html().Render("create_user", args.Merge(tplParams{ response.HTML().Render("create_user", args.Merge(tplParams{
"menu": "settings", "menu": "settings",
"form": &form.UserForm{}, "form": &form.UserForm{},
})) }))
@ -62,19 +62,19 @@ func (c *Controller) SaveUser(ctx *core.Context, request *core.Request, response
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
if !user.IsAdmin { if !user.IsAdmin {
response.Html().Forbidden() response.HTML().Forbidden()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
userForm := form.NewUserForm(request.Request()) userForm := form.NewUserForm(request.Request())
if err := userForm.ValidateCreation(); err != nil { if err := userForm.ValidateCreation(); err != nil {
response.Html().Render("create_user", args.Merge(tplParams{ response.HTML().Render("create_user", args.Merge(tplParams{
"menu": "settings", "menu": "settings",
"form": userForm, "form": userForm,
"errorMessage": err.Error(), "errorMessage": err.Error(),
@ -83,7 +83,7 @@ func (c *Controller) SaveUser(ctx *core.Context, request *core.Request, response
} }
if c.store.UserExists(userForm.Username) { if c.store.UserExists(userForm.Username) {
response.Html().Render("create_user", args.Merge(tplParams{ response.HTML().Render("create_user", args.Merge(tplParams{
"menu": "settings", "menu": "settings",
"form": userForm, "form": userForm,
"errorMessage": "This user already exists.", "errorMessage": "This user already exists.",
@ -94,7 +94,7 @@ func (c *Controller) SaveUser(ctx *core.Context, request *core.Request, response
newUser := userForm.ToUser() newUser := userForm.ToUser()
if err := c.store.CreateUser(newUser); err != nil { if err := c.store.CreateUser(newUser); err != nil {
log.Println(err) log.Println(err)
response.Html().Render("edit_user", args.Merge(tplParams{ response.HTML().Render("edit_user", args.Merge(tplParams{
"menu": "settings", "menu": "settings",
"form": userForm, "form": userForm,
"errorMessage": "Unable to create this user.", "errorMessage": "Unable to create this user.",
@ -109,13 +109,13 @@ func (c *Controller) EditUser(ctx *core.Context, request *core.Request, response
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
if !user.IsAdmin { if !user.IsAdmin {
response.Html().Forbidden() response.HTML().Forbidden()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -124,7 +124,7 @@ func (c *Controller) EditUser(ctx *core.Context, request *core.Request, response
return return
} }
response.Html().Render("edit_user", args.Merge(tplParams{ response.HTML().Render("edit_user", args.Merge(tplParams{
"menu": "settings", "menu": "settings",
"selected_user": selectedUser, "selected_user": selectedUser,
"form": &form.UserForm{ "form": &form.UserForm{
@ -138,13 +138,13 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
if !user.IsAdmin { if !user.IsAdmin {
response.Html().Forbidden() response.HTML().Forbidden()
return return
} }
args, err := c.getCommonTemplateArgs(ctx) args, err := c.getCommonTemplateArgs(ctx)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return return
} }
@ -155,7 +155,7 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon
userForm := form.NewUserForm(request.Request()) userForm := form.NewUserForm(request.Request())
if err := userForm.ValidateModification(); err != nil { if err := userForm.ValidateModification(); err != nil {
response.Html().Render("edit_user", args.Merge(tplParams{ response.HTML().Render("edit_user", args.Merge(tplParams{
"menu": "settings", "menu": "settings",
"selected_user": selectedUser, "selected_user": selectedUser,
"form": userForm, "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) { 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", "menu": "settings",
"selected_user": selectedUser, "selected_user": selectedUser,
"form": userForm, "form": userForm,
@ -177,7 +177,7 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon
userForm.Merge(selectedUser) userForm.Merge(selectedUser)
if err := c.store.UpdateUser(selectedUser); err != nil { if err := c.store.UpdateUser(selectedUser); err != nil {
log.Println(err) log.Println(err)
response.Html().Render("edit_user", args.Merge(tplParams{ response.HTML().Render("edit_user", args.Merge(tplParams{
"menu": "settings", "menu": "settings",
"selected_user": selectedUser, "selected_user": selectedUser,
"form": userForm, "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) { func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) {
user := ctx.GetLoggedUser() user := ctx.GetLoggedUser()
if !user.IsAdmin { if !user.IsAdmin {
response.Html().Forbidden() response.HTML().Forbidden()
return 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 { if err := c.store.RemoveUser(selectedUser.ID); err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return 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) { func (c *Controller) getUserFromURL(ctx *core.Context, request *core.Request, response *core.Response) (*model.User, error) {
userID, err := request.IntegerParam("userID") userID, err := request.IntegerParam("userID")
if err != nil { if err != nil {
response.Html().BadRequest(err) response.HTML().BadRequest(err)
return nil, err return nil, err
} }
user, err := c.store.GetUserById(userID) user, err := c.store.GetUserById(userID)
if err != nil { if err != nil {
response.Html().ServerError(err) response.HTML().ServerError(err)
return nil, err return nil, err
} }
if user == nil { if user == nil {
response.Html().NotFound() response.HTML().NotFound()
return nil, errors.New("User not found") return nil, errors.New("User not found")
} }