-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package httputil | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
// ---------------------------------------------------------- | ||
|
||
// Reply replies a http request with a json response. | ||
func Reply(w http.ResponseWriter, code int, data interface{}) { | ||
msg, err := json.Marshal(data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
h := w.Header() | ||
h.Set("Content-Length", strconv.Itoa(len(msg))) | ||
h.Set("Content-Type", "application/json") | ||
w.WriteHeader(code) | ||
w.Write(msg) | ||
} | ||
|
||
// ReplyWith replies a http request with a bodyType response. | ||
func ReplyWith(w http.ResponseWriter, code int, bodyType string, msg []byte) { | ||
h := w.Header() | ||
h.Set("Content-Length", strconv.Itoa(len(msg))) | ||
h.Set("Content-Type", bodyType) | ||
w.WriteHeader(code) | ||
w.Write(msg) | ||
} | ||
|
||
// ReplyWithStream replies a http request with a streaming response. | ||
func ReplyWithStream(w http.ResponseWriter, code int, bodyType string, body io.Reader, bytes int64) { | ||
h := w.Header() | ||
h.Set("Content-Length", strconv.FormatInt(bytes, 10)) | ||
h.Set("Content-Type", bodyType) | ||
w.WriteHeader(code) | ||
// We don't use io.CopyN: if you need, call io.LimitReader(body, bytes) by yourself | ||
written, err := io.Copy(w, body) | ||
if err != nil || written != bytes { | ||
log.Printf("ReplyWithStream (bytes=%v): written=%v, err=%v\n", bytes, written, err) | ||
} | ||
} | ||
|
||
// ---------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters