Browse Source

Set up JSON-serving route

Edward Bramanti 10 years ago
parent
commit
491f765541
1 changed files with 48 additions and 0 deletions
  1. 48 0
      frameworks/Go/go-mongodb/src/hello/hello.go

+ 48 - 0
frameworks/Go/go-mongodb/src/hello/hello.go

@@ -0,0 +1,48 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"gopkg.in/mgo.v2"
+	"log"
+	"net/http"
+	"runtime"
+)
+
+const (
+	connectionString = "localhost"
+	helloWorldString = "Hello, world!"
+)
+
+type Message struct {
+	Message string `json:"message"`
+}
+
+type World struct {
+	Id           uint16 `json:"id"`
+	RandomNumber uint16 `json:"randomNumber"`
+}
+
+type Fortune struct {
+	Id      uint16 `json:"id"`
+	Message string `json:"message"`
+}
+
+func main() {
+	runtime.GOMAXPROCS(runtime.NumCPU())
+	session, err := mgo.Dial(connectionString)
+	if err != nil {
+		log.Fatalf("Error opening database: %v", err)
+	}
+	fmt.Println("Test")
+	defer session.Close()
+	session.SetPoolLimit(5)
+	http.HandleFunc("/json", jsonHandler)
+	http.ListenAndServe(":8228", nil)
+}
+
+// Test 1: JSON serialization
+func jsonHandler(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("Content-Type", "application/javascript")
+	json.NewEncoder(w).Encode(&Message{helloWorldString})
+}