mirror of
https://github.com/elyby/chrly.git
synced 2024-11-06 08:11:11 +05:30
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package http
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
func NotFound(response http.ResponseWriter, _ *http.Request) {
|
|
data, _ := json.Marshal(map[string]string{
|
|
"status": "404",
|
|
"message": "Not Found",
|
|
})
|
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
response.WriteHeader(http.StatusNotFound)
|
|
_, _ = response.Write(data)
|
|
}
|
|
|
|
func waitForSignal() os.Signal {
|
|
ch := make(chan os.Signal)
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return <-ch
|
|
}
|
|
|
|
func apiBadRequest(resp http.ResponseWriter, errorsPerField map[string][]string) {
|
|
resp.WriteHeader(http.StatusBadRequest)
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
result, _ := json.Marshal(map[string]interface{}{
|
|
"errors": errorsPerField,
|
|
})
|
|
_, _ = resp.Write(result)
|
|
}
|
|
|
|
func apiForbidden(resp http.ResponseWriter, reason string) {
|
|
resp.WriteHeader(http.StatusForbidden)
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
result, _ := json.Marshal(map[string]interface{}{
|
|
"error": reason,
|
|
})
|
|
_, _ = resp.Write(result)
|
|
}
|
|
|
|
func apiNotFound(resp http.ResponseWriter, reason string) {
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
result, _ := json.Marshal([]interface{}{
|
|
reason,
|
|
})
|
|
_, _ = resp.Write(result)
|
|
}
|
|
|
|
func apiServerError(resp http.ResponseWriter) {
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
}
|