forked from decred/dcrwebapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
50 lines (44 loc) · 1.62 KB
/
helper.go
File metadata and controls
50 lines (44 loc) · 1.62 KB
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
// Copyright (c) 2017-2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"math"
"net/http"
)
// writeJSONResponse convenience func for writing json responses
func writeJSONResponse(writer *http.ResponseWriter, code int,
respJSON *[]byte) {
(*writer).Header().Set("Content-Type", "application/json")
(*writer).Header().Set("Strict-Transport-Security", "max-age=15552001")
(*writer).Header().Set("Vary", "Accept-Encoding")
(*writer).WriteHeader(code)
(*writer).Write(*respJSON)
}
// writeSVGResponse convenience func for writing svg responses
func writeSVGResponse(writer *http.ResponseWriter, code int,
svg *string) {
(*writer).Header().Set("Strict-Transport-Security", "max-age=15552001")
(*writer).Header().Set("Vary", "Accept-Encoding")
(*writer).Header().Set("Content-Type", "image/svg+xml")
(*writer).WriteHeader(code)
fmt.Fprint(*writer, *svg)
}
// writeJSONErrorResponse convenience func for writing json error responses
func writeJSONErrorResponse(writer *http.ResponseWriter, code int, err error) {
errorBody := map[string]interface{}{}
errorBody["error"] = err.Error()
errorJSON, _ := json.Marshal(errorBody)
(*writer).Header().Set("Content-Type", "application/json")
(*writer).Header().Set("Strict-Transport-Security", "max-age=15552001")
(*writer).Header().Set("Vary", "Accept-Encoding")
(*writer).WriteHeader(code)
(*writer).Write(errorJSON)
}
// round rounding func
func round(f float64, places uint) float64 {
shift := math.Pow(10, float64(places))
return math.Floor(f*shift+.5) / shift
}