refactor
This commit is contained in:
9
client/httputil/helloworld.go
Normal file
9
client/httputil/helloworld.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package httputil
|
||||
|
||||
import "net/http"
|
||||
|
||||
func HelloWorld(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello world"))
|
||||
})
|
||||
}
|
||||
17
client/httputil/response.go
Normal file
17
client/httputil/response.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func WriteJSON(w http.ResponseWriter, status int, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(jsonBytes)
|
||||
}
|
||||
Reference in New Issue
Block a user