Inititial commit

This commit is contained in:
Christian Barth
2018-04-29 01:25:38 +02:00
commit b4e2e4a52a
7 changed files with 295 additions and 0 deletions

32
core/communications.go Normal file
View File

@@ -0,0 +1,32 @@
package core
import (
"encoding/json"
"fmt"
"net/http"
)
// SendObject sends a object through the writer
func SendObject(writer *http.ResponseWriter, data interface{}) {
js := ToJSON(data)
if js == "" {
SendJSON(writer, "{\"server_message\":\"internal_sending_error\"")
return
}
SendJSON(writer, js)
}
// SendJSON sends JSON string through the writer
func SendJSON(writer *http.ResponseWriter, data string) {
(*writer).Header().Set("Content-Type", "application/json")
fmt.Fprintf((*writer), data)
}
// ToJSON converts an object to a JSON string
func ToJSON(in interface{}) string {
data, err := json.Marshal(in)
if err != nil {
return ""
}
return string(data)
}