diff options
author | 2018-01-02 22:04:48 -0800 | |
---|---|---|
committer | 2018-01-02 22:04:48 -0800 | |
commit | 320d1b016747ba4501da9417d9ce5f99368a5768 (patch) | |
tree | 1054d96afde6022951b76cc4a09b78e1e3f05058 /http/handler/html_response.go | |
parent | c39f2e1a8d2de6d412bcc673d29eb0f7a2d1f5f7 (diff) | |
download | v2-320d1b016747ba4501da9417d9ce5f99368a5768.tar.gz v2-320d1b016747ba4501da9417d9ce5f99368a5768.tar.zst v2-320d1b016747ba4501da9417d9ce5f99368a5768.zip |
Refactor packages to have more idiomatic code base
Diffstat (limited to 'http/handler/html_response.go')
-rw-r--r-- | http/handler/html_response.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/http/handler/html_response.go b/http/handler/html_response.go new file mode 100644 index 00000000..26e52708 --- /dev/null +++ b/http/handler/html_response.go @@ -0,0 +1,65 @@ +// Copyright 2017 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package handler + +import ( + "net/http" + + "github.com/miniflux/miniflux/logger" + "github.com/miniflux/miniflux/template" +) + +// HTMLResponse handles HTML responses. +type HTMLResponse struct { + writer http.ResponseWriter + request *http.Request + template *template.Engine +} + +// 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) +} + +// ServerError sends a 500 error to the browser. +func (h *HTMLResponse) ServerError(err error) { + h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") + h.writer.WriteHeader(http.StatusInternalServerError) + + if err != nil { + logger.Error("[Internal Server Error] %v", err) + h.writer.Write([]byte("Internal Server Error: " + err.Error())) + } else { + h.writer.Write([]byte("Internal Server Error")) + } +} + +// BadRequest sends a 400 error to the browser. +func (h *HTMLResponse) BadRequest(err error) { + h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") + h.writer.WriteHeader(http.StatusBadRequest) + + if err != nil { + logger.Error("[Bad Request] %v", err) + h.writer.Write([]byte("Bad Request: " + err.Error())) + } else { + h.writer.Write([]byte("Bad Request")) + } +} + +// NotFound sends a 404 error to the browser. +func (h *HTMLResponse) NotFound() { + h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") + h.writer.WriteHeader(http.StatusNotFound) + h.writer.Write([]byte("Page Not Found")) +} + +// Forbidden sends a 403 error to the browser. +func (h *HTMLResponse) Forbidden() { + h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") + h.writer.WriteHeader(http.StatusForbidden) + h.writer.Write([]byte("Access Forbidden")) +} |