2019-05-25 17:30:40 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
import "log"
|
|
|
|
import "net/http"
|
2019-05-25 19:18:46 -05:00
|
|
|
import "io/ioutil"
|
|
|
|
import "strings"
|
|
|
|
import "os"
|
|
|
|
import "time"
|
|
|
|
|
2019-05-25 17:30:40 -05:00
|
|
|
import "github.com/davecgh/go-spew/spew"
|
|
|
|
|
2019-05-25 19:18:46 -05:00
|
|
|
import "github.com/golang/protobuf/jsonpb"
|
|
|
|
import pb "git.wit.com/wit/witProtobuf"
|
|
|
|
|
2019-05-25 17:30:40 -05:00
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// spew.Dump(r)
|
|
|
|
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
|
2019-05-25 19:18:46 -05:00
|
|
|
log.Printf("handler: fucking unknown path")
|
2019-05-25 17:30:40 -05:00
|
|
|
}
|
|
|
|
|
2019-05-25 19:18:46 -05:00
|
|
|
func JSONtoPB(a string) pb.Account {
|
|
|
|
sets := pb.Account{}
|
2019-05-25 17:30:40 -05:00
|
|
|
|
2019-05-25 19:18:46 -05:00
|
|
|
err := jsonpb.Unmarshal(strings.NewReader(string(a)), &sets)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("jsonpb.Unmarshal() ERROR =", err)
|
2019-05-25 17:30:40 -05:00
|
|
|
}
|
2019-05-25 19:18:46 -05:00
|
|
|
spew.Dump(sets)
|
|
|
|
return sets
|
2019-05-25 17:30:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.Println("listen on :9000")
|
|
|
|
|
|
|
|
http.HandleFunc("/", handler)
|
|
|
|
http.HandleFunc("/email", handler2)
|
|
|
|
|
|
|
|
http.ListenAndServe(":9000", nil)
|
|
|
|
}
|
2019-05-25 19:18:46 -05:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|