Created
October 2, 2014 21:42
-
-
Save hourback/4a0b3b4929023173911d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
_ "github.com/lib/pq" | |
//"database/sql" | |
//"github.com/jmoiron/sqlx" | |
"log" | |
"io" | |
"net/http" | |
"encoding/json" | |
"github.com/gorilla/mux" | |
//"github.com/steveyen/gkvlite" | |
"fmt" | |
) | |
type Notification struct { | |
Id string `json:"id"` | |
Status string `json:"status"` | |
DescriptionOfProblem string `json:"description_of_problem"` | |
} | |
type Response struct { | |
Notifications []Notification `json:"notification"` | |
} | |
var notifications = []Notification{} | |
func HelloServer(w http.ResponseWriter, req *http.Request) { | |
io.WriteString(w, "hello, world!\n") | |
} | |
func AllNotifications(w http.ResponseWriter, req *http.Request) { | |
log.Println("Entering AllNotifications") | |
res := Response{ notifications } | |
log.Println("res is: ", res) | |
b, err := json.Marshal(res) | |
if err != nil { | |
log.Println(err) | |
} else { | |
w.Header().Set("Content-Type", "application/json") | |
w.Write(b) | |
log.Println("string(b) is:", string(b)) | |
} | |
} | |
func AddDefaultHeaders(fn http.HandlerFunc) http.HandlerFunc { | |
log.Println("Inside AddDefaultHeaders") | |
return func(w http.ResponseWriter, r *http.Request) { | |
// Put this in later, just to be safe | |
/*if origin := r.Header.Get("Origin"); origin != "" { | |
w.Header().Set("Access-Control-Allow-Origin", origin) | |
}*/ | |
w.Header().Set("Access-Control-Allow-Origin", "*") | |
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") | |
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token") | |
w.Header().Set("Access-Control-Allow-Credentials", "true") | |
fn(w, r) | |
} | |
} | |
func GetNotification(w http.ResponseWriter, req *http.Request) { | |
vars := mux.Vars(req) | |
notificationId := vars["notificationId"] | |
log.Println("notificationId is: ", notificationId) | |
for i, v := range notifications { | |
if notificationId == v.Id { | |
log.Println(i, v) | |
} | |
} | |
} | |
func PostNotification(w http.ResponseWriter, req *http.Request) { | |
log.Println("Entering PostNotification....") | |
log.Println(req.Body) | |
decoder := json.NewDecoder(req.Body) | |
for { | |
var n Response | |
if err := decoder.Decode(&n); err == io.EOF { | |
break | |
} else if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%s: %s\n", n.Status, n.DescriptionOfProblem) | |
notifications = append(notifications, n) | |
} | |
} | |
func main() { | |
log.Println("Starting server") | |
log.Println("Creating records") | |
notifications = append(notifications, | |
Notification{"1", "OPEN", "What do you think?"}, | |
Notification{"2", "CLOSED", "What do you think?"}, | |
Notification{"3", "", "What do you think?"}, | |
) | |
gmux := mux.NewRouter() | |
gmux.HandleFunc("/hello", AddDefaultHeaders(HelloServer)) | |
gmux.HandleFunc("/api/notifications", AddDefaultHeaders(AllNotifications)).Methods("GET") | |
gmux.HandleFunc("/api/notifications", AddDefaultHeaders(PostNotification)).Methods("POST", "OPTIONS") | |
gmux.HandleFunc("/api/notifications/{notificationId}", AddDefaultHeaders(GetNotification)) | |
http.Handle("/", gmux) | |
err := http.ListenAndServe(":8080", nil) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
} else { | |
log.Println("Listening on 8080") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment