From cdf5f3b4280d2f692da61e0180282fa0e5ffb835 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 25 May 2019 17:18:46 -0700 Subject: [PATCH] add Makefile & update to write out JSON files Signed-off-by: Jeff Carr --- .gitignore | 1 + Makefile | 16 +++++++++++ signup.go | 81 +++++++++++++++++++++++++++++++++++------------------- 3 files changed, 69 insertions(+), 29 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e2085d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +signup diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..37c8196 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +# do this + +all: + go build + ./signup + +# this is the raw socket server. it will +# show all socket connects recieved +raw-socket: + go run server.go + +push: + git pull + git add --all + -git commit -a -s + git push diff --git a/signup.go b/signup.go index 3c94c93..f55b712 100644 --- a/signup.go +++ b/signup.go @@ -3,44 +3,31 @@ package main import "fmt" import "log" import "net/http" +import "io/ioutil" +import "strings" +import "os" +import "time" + import "github.com/davecgh/go-spew/spew" +import "github.com/golang/protobuf/jsonpb" +import pb "git.wit.com/wit/witProtobuf" + func handler(w http.ResponseWriter, r *http.Request) { // spew.Dump(r) fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) - log.Printf("handler: unknown path") + log.Printf("handler: fucking unknown path") } -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") - w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") +func JSONtoPB(a string) pb.Account { + sets := pb.Account{} - spew.Dump(r) - fmt.Fprintf(w, "handler2 %s!", r.URL.Path[1:]) - - log.Printf("handler: method switch statement") - switch r.Method { - case http.MethodGet: - // Serve the resource. - log.Printf("handler2 GET") - case http.MethodPost: - // Create a new record. - log.Printf("handler2 POST") - case http.MethodPut: - // Update an existing record. - log.Printf("handler2 PUT") - case http.MethodDelete: - // Remove the record. - log.Printf("handler2 DELETE") - case http.MethodOptions: - // Remove the record. - log.Printf("handler2 OPTIONS") - default: - // Give an error message. - log.Printf("handler2 DEFAULT Method=", r.Method) - spew.Dump(r.Method) + err := jsonpb.Unmarshal(strings.NewReader(string(a)), &sets) + if err != nil { + log.Println("jsonpb.Unmarshal() ERROR =", err) } + spew.Dump(sets) + return sets } func main() { @@ -51,3 +38,39 @@ 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") + w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") + + // spew.Dump(r) + fmt.Fprintf(w, "Hi there, handler2 %s!", r.URL.Path[1:]) + + body, err := ioutil.ReadAll(r.Body) + j := string(body) + 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") +}