39 lines
781 B
Go
39 lines
781 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"fmt"
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
)
|
||
|
|
||
|
var messages []*string
|
||
|
|
||
|
func main() {
|
||
|
//listener, err = net.Listen("socket", "/tmp/
|
||
|
http.HandleFunc("/", get)
|
||
|
http.HandleFunc("/add", post)
|
||
|
log.Fatal(http.ListenAndServe(":12345", nil))
|
||
|
}
|
||
|
|
||
|
type BlogPost struct {
|
||
|
Message string `"message"`
|
||
|
Title string `"title"`
|
||
|
Secret string `"secret"`
|
||
|
}
|
||
|
|
||
|
func get(w http.ResponseWriter, r *http.Request) {
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
w.Write([]byte("response:"))
|
||
|
for _, m := range messages {
|
||
|
w.Write([]byte(*m))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func post(w http.ResponseWriter, r *http.Request) {
|
||
|
var post BlogPost
|
||
|
json.NewDecoder(r.Body).Decode(&post)
|
||
|
fmt.Println(post.Message)
|
||
|
messages = append(messages, &post.Message)
|
||
|
}
|