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) {
|
2019-05-25 20:04:50 -05:00
|
|
|
// NOTE:
|
|
|
|
// NOTE: If you are having to enable 'CORS' then
|
|
|
|
// NOTE: something is really wrong with the design
|
|
|
|
// NOTE: and general architecture. Never allow this.
|
|
|
|
// NOTE: fix your design the right way.
|
|
|
|
// NOTE:
|
|
|
|
//
|
|
|
|
// TODO: remove all ability to accept any CORS
|
|
|
|
// on any WIT backend and infrastructre anything
|
|
|
|
//
|
2019-05-25 19:18:46 -05:00
|
|
|
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
|
2019-05-25 19:36:35 -05:00
|
|
|
var account pb.Account
|
|
|
|
account = JSONtoPB(string(body))
|
2019-05-25 19:18:46 -05:00
|
|
|
account.Token = "testing a token"
|
|
|
|
log.Println("account =", account)
|
|
|
|
|
2019-05-25 19:36:35 -05:00
|
|
|
// Add all the HTTP headers to the Account protobuf
|
|
|
|
for name, headers := range r.Header {
|
|
|
|
log.Println("handler2() name =", name)
|
|
|
|
log.Println("handler2() headers =", headers)
|
2019-05-25 19:55:26 -05:00
|
|
|
name = string(name)
|
2019-05-25 19:36:35 -05:00
|
|
|
for _, h := range headers {
|
|
|
|
log.Println("handler2() h =", h)
|
|
|
|
|
|
|
|
// create a new Account_httpHeader
|
|
|
|
var request pb.Account_HttpHeader
|
|
|
|
request.Name = name
|
|
|
|
request.Value = h
|
|
|
|
account.HttpHeaders = append(account.HttpHeaders, &request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 19:18:46 -05:00
|
|
|
// change it back to JSON
|
|
|
|
marshaler := &jsonpb.Marshaler{}
|
|
|
|
stuff, _ := marshaler.MarshalToString(&account)
|
|
|
|
log.Println(stuff)
|
|
|
|
|
|
|
|
current := time.Now()
|
2019-05-25 19:24:13 -05:00
|
|
|
filename := "/signup/" + current.Format("2006-01-02-15-04-05")
|
2019-05-25 19:18:46 -05:00
|
|
|
log.Println("filename =", filename)
|
|
|
|
// write to file
|
|
|
|
writeToFile(filename, stuff + "\n")
|
|
|
|
}
|