Created
October 2, 2014 02:32
-
-
Save hourback/821ac82feca4bbcfee6c 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" | |
) | |
type Notification struct { | |
Id string | |
Status string | |
DescriptionOfProblem string | |
} | |
type Response struct { | |
Data struct { | |
Children []struct { | |
Data 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") | |
io.WriteString(w, "This will list all of the notifications in the database.\n") | |
response := Response{ Data: { Children: {notifications} } } | |
log.Println(response) | |
b, err := json.Marshal(response) | |
if err != nil { | |
log.Println(err) | |
} else { | |
log.Println("string(b) is:", string(b)) | |
} | |
} | |
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?"}, | |
) | |
log.Println(notifications) | |
http.HandleFunc("/hello", HelloServer) | |
http.HandleFunc("/api/notifications", AllNotifications) | |
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