Quellcode durchsuchen

add JSON supports

tags/v0.9.0
jimzhan vor 9 Jahren
Ursprung
Commit
bbfed3727c
1 geänderte Dateien mit 18 neuen und 0 gelöschten Zeilen
  1. +18
    -0
      server.go

+ 18
- 0
server.go Datei anzeigen

@@ -1,6 +1,8 @@
package rex

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
@@ -182,6 +184,22 @@ func (self *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
self.build().ServeHTTP(w, r)
}

// Sends the HTTP response in JSON.
func (self *server) Send(w http.ResponseWriter, v interface{}) {
var buffer = new(bytes.Buffer)

w.Header().Set("Content-type", "application/json; charset=utf-8")
if err := json.NewEncoder(buffer).Encode(v); err == nil {
w.Write(buffer.Bytes())
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

// Renders a view (Pongo) and sends the rendered HTML string to the client.
// Optional parameters: value, local variables for the view.
// func (self *server) Render(filename string, v ...interface{}) {}

// Run starts the application server to serve incoming requests at the given address.
func (self *server) Run() {
runtime.GOMAXPROCS(maxprocs)

Laden…
Abbrechen
Speichern