wit-signup/signup.go

104 lines
2.6 KiB
Go
Raw Normal View History

2019-05-25 17:30:40 -05:00
package main
import "fmt"
import "log"
import "net/http"
import "io/ioutil"
import "strings"
import "os"
import "time"
2019-05-25 17:30:40 -05:00
import "github.com/davecgh/go-spew/spew"
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:])
log.Printf("handler: fucking unknown path")
2019-05-25 17:30:40 -05:00
}
func JSONtoPB(a string) pb.Account {
sets := pb.Account{}
2019-05-25 17:30:40 -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
}
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)
}
func writeToFile(filename string, a string) {
f, _ := os.Create(filename)
f.WriteString(a)
f.Close()
}
func handler2(w http.ResponseWriter, r *http.Request) {
// 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
//
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
var account pb.Account
account = JSONtoPB(string(body))
account.Token = "testing a token"
log.Println("account =", account)
// 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)
name = string(name)
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)
}
}
// 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")
log.Println("filename =", filename)
// write to file
writeToFile(filename, stuff + "\n")
}