aboutsummaryrefslogtreecommitdiffhomepage
path: root/http/response/html/html.go
blob: 65a46490ddc2a308696a1caf09effdf55cad6855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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 "miniflux.app/http/response/html"

import (
	"net/http"

	"miniflux.app/http/response"
	"miniflux.app/logger"
)

// OK writes a standard HTML response.
func OK(w http.ResponseWriter, r *http.Request, b []byte) {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	response.Compress(w, r, 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"))
}