From 53c9620b8a0e318a24ca678b69b0b640eb3fdad9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 25 May 2019 17:11:32 -0700 Subject: [PATCH] write to files Signed-off-by: Jeff Carr --- emaild/alfonso.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/emaild/alfonso.go b/emaild/alfonso.go index 8c5e219..f55b712 100644 --- a/emaild/alfonso.go +++ b/emaild/alfonso.go @@ -5,6 +5,8 @@ import "log" import "net/http" import "io/ioutil" import "strings" +import "os" +import "time" import "github.com/davecgh/go-spew/spew" @@ -37,6 +39,12 @@ func main() { http.ListenAndServe(":9000", nil) } +func writeToFile(filename string, a string) { + f, _ := os.Create(filename) + f.WriteString(a) + f.Close() +} + func handler2(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") @@ -50,8 +58,19 @@ func handler2(w http.ResponseWriter, r *http.Request) { log.Println("body =", j) log.Println("err =", err) + // change the JSON int a protobuf account := JSONtoPB(string(body)) account.Token = "testing a token" - log.Println("account =", account) + + // change it back to JSON + marshaler := &jsonpb.Marshaler{} + stuff, _ := marshaler.MarshalToString(&account) + log.Println(stuff) + + current := time.Now() + filename := "/tmp/" + current.Format("2006-01-02-15-04-05") + log.Println("filename =", filename) + // write to file + writeToFile(filename, stuff + "\n") }