Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F12634081
response.go
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
response.go
View Options
package
impart
import
(
"encoding/json"
"net/http"
"strconv"
)
type
(
// Envelope contains metadata and optional data for a response object.
Envelope
struct
{
Code
int
`json:"code"`
ErrorType
string
`json:"error_type,omitempty"`
ErrorMessage
string
`json:"error_msg,omitempty"`
Data
interface
{}
`json:"data,omitempty"`
}
)
func
writeBody
(
w
http
.
ResponseWriter
,
body
[]
byte
,
status
int
,
contentType
string
)
error
{
w
.
Header
().
Set
(
"Content-Type"
,
contentType
+
"; charset=UTF-8"
)
w
.
Header
().
Set
(
"Content-Length"
,
strconv
.
Itoa
(
len
(
body
)))
w
.
WriteHeader
(
status
)
_
,
err
:=
w
.
Write
(
body
)
return
err
}
func
renderJSON
(
w
http
.
ResponseWriter
,
value
interface
{},
status
int
)
error
{
body
,
err
:=
json
.
Marshal
(
value
)
if
err
!=
nil
{
return
err
}
return
writeBody
(
w
,
body
,
status
,
"application/json"
)
}
func
renderString
(
w
http
.
ResponseWriter
,
status
int
,
msg
string
)
error
{
return
writeBody
(
w
,
[]
byte
(
msg
),
status
,
"text/plain"
)
}
func
WriteSuccess
(
w
http
.
ResponseWriter
,
data
interface
{},
status
int
)
error
{
env
:=
&
Envelope
{
Code
:
status
,
Data
:
data
,
}
return
renderJSON
(
w
,
env
,
status
)
}
func
WriteError
(
w
http
.
ResponseWriter
,
e
HTTPError
)
error
{
env
:=
&
Envelope
{
Code
:
e
.
Status
,
ErrorMessage
:
e
.
Message
,
}
return
renderJSON
(
w
,
env
,
e
.
Status
)
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Dec 4, 10:25 PM (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3524817
Attached To
rI impart
Event Timeline
Log In to Comment