aboutsummaryrefslogtreecommitdiffhomepage
path: root/http/response/html/html.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-04-29 16:35:04 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-04-29 16:35:04 -0700
commitf49b42f70f902d4da1e0fa4080e99164b331b716 (patch)
treec6bdd19f11d100c44b0d30344ec37038f649e988 /http/response/html/html.go
parent1eba1730d1af50ed545f4fde78b22d6fb62ca11e (diff)
Use vanilla HTTP handlers (refactoring)
Diffstat (limited to 'http/response/html/html.go')
-rw-r--r--http/response/html/html.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/http/response/html/html.go b/http/response/html/html.go
new file mode 100644
index 0000000..4f605e8
--- /dev/null
+++ b/http/response/html/html.go
@@ -0,0 +1,57 @@
+// Copyright 2018 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 html
+
+import (
+ "net/http"
+
+ "github.com/miniflux/miniflux/logger"
+)
+
+// OK writes a standard HTML response.
+func OK(w http.ResponseWriter, b []byte) {
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ w.Write(b)
+}
+
+// ServerError sends a 500 error to the browser.
+func ServerError(w http.ResponseWriter, err error) {
+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ w.WriteHeader(http.StatusInternalServerError)
+
+ if err != nil {
+ logger.Error("[Internal Server Error] %v", err)
+ w.Write([]byte("Internal Server Error: " + err.Error()))
+ } else {
+ w.Write([]byte("Internal Server Error"))
+ }
+}
+
+// BadRequest sends a 400 error to the browser.
+func BadRequest(w http.ResponseWriter, err error) {
+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ w.WriteHeader(http.StatusBadRequest)
+
+ if err != nil {
+ logger.Error("[Bad Request] %v", err)
+ w.Write([]byte("Bad Request: " + err.Error()))
+ } else {
+ w.Write([]byte("Bad Request"))
+ }
+}
+
+// NotFound sends a 404 error to the browser.
+func NotFound(w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ w.WriteHeader(http.StatusNotFound)
+ w.Write([]byte("Page Not Found"))
+}
+
+// Forbidden sends a 403 error to the browser.
+func Forbidden(w http.ResponseWriter) {
+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ w.WriteHeader(http.StatusForbidden)
+ w.Write([]byte("Access Forbidden"))
+}