-
Notifications
You must be signed in to change notification settings - Fork 50
/
helper.go
38 lines (33 loc) · 1.29 KB
/
helper.go
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
// Copyright (c) 2017-2024 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"
"net/http"
"time"
)
// 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).Header().Set("X-Content-Type-Options", "nosniff")
(*writer).WriteHeader(code)
(*writer).Write(*respJSON)
}
// writeJSONErrorResponse convenience func for writing json error responses
func writeJSONErrorResponse(writer *http.ResponseWriter, 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(http.StatusInternalServerError)
(*writer).Write(errorJSON)
}
func getUnixTime(year int, month time.Month, day int) int64 {
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC).Unix()
}