add Makefile & update to write out JSON files

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-25 17:18:46 -07:00
parent a698be5fad
commit cdf5f3b428
3 changed files with 69 additions and 29 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
signup

16
Makefile Normal file
View File

@ -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

View File

@ -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")
}