func updateDocument(user_id string, w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) // var id, _ = strconv.Atoi(vars["id"]) var oldDoc = Document{} DB.Where("uid = ?", vars["id"]).First(&oldDoc) var id = oldDoc.ID var newDoc = Document{} body, _ := ioutil.ReadAll(r.Body) json.Unmarshal(body, &newDoc) var notebook = Notebook{} DB.First(¬ebook, Notebook{DocumentId: uint(id)}) notebook.Body = newDoc.Notebook.Body notebook.Name = newDoc.Notebook.Name notebook.Updates += 1 if notebook.Body != "" { DB.Save(¬ebook) w.Write([]byte("ok")) } else { w.Write([]byte("err")) // TODO http error code } }
func postMessageCradle(user_id string, w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) r.ParseForm() to := r.Form["to"][0] session_id := r.Form["session_id"][0] message := r.Form["message"][0] CRADLE.Post(vars["id"], session_id, to, message) // FIXME w.Write([]byte("{\"ok\":true}")) }
func getDocument(user_id string, w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) var document = Document{} fmt.Printf("GET %s\n", vars["id"]) DB.Where("uid = ?", vars["id"]).First(&document) DB.Model(&document).Related(&document.Notebook) DB.Model(&document).Related(&document.DataFile) json, _ := json.Marshal(document) w.Header().Set("Content-Type", "application/json; charset=utf-8") fmt.Printf("GET %s\n", json) w.Write(json) }
func getCradle(user_id string, w http.ResponseWriter, r *http.Request) { r.ParseForm() vars := mux.Vars(r) session_id := r.Form["session"][0] fmt.Printf("GET INPUT session=%v\n", session_id) session, _ := SESSION.Get(r, "sessionName") oldUserState := "{}" if session.Values["state"] != nil { oldUserState = session.Values["state"].(string) } sessions := CRADLE.Get(vars["id"], user_id, oldUserState, session_id, w.(http.CloseNotifier).CloseNotify()) fmt.Printf("GET OUTPUT session=%v %v\n", session_id, sessions) if sessions != nil { json, _ := json.Marshal(sessions) w.Write(json) } }
func putConfigCradle(user_id string, w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) r.ParseForm() fmt.Printf("Got a PUT action %v\n", r.Form) if r.Form["user"] != nil { userState := r.Form["user"][0] session, _ := SESSION.Get(r, "sessionName") session.Values["state"] = userState session.Save(r, w) CRADLE.UpdateUser(vars["id"], user_id, userState) } if r.Form["state"] != nil { fmt.Printf("UpdateState %v\n", r.Form["state"]) session_id := r.Form["session_id"][0] // SECURITY ISSUE - CAN FORGE MESSAGES - FIXME sessionState := r.Form["state"][0] CRADLE.UpdateState(vars["id"], session_id, sessionState) } w.Write([]byte("{\"ok\":true}")) }
func forkDocument(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] var request = map[string]string{} body, _ := ioutil.ReadAll(r.Body) json.Unmarshal(body, &request) csv, _ := ioutil.ReadFile("./public/forkable/" + id + ".csv") doc, err := ioutil.ReadFile("./public/forkable/" + id + ".json") if err != nil { fmt.Printf("json file missing, falling back to ipynb") doc, _ = ioutil.ReadFile("./public/forkable/" + id + ".ipynb") } document := &Document{Name: "Hello", Uid: randomUID()} DB.Create(&document) notebook := &Notebook{DocumentId: document.ID, Name: "NotebookName", Body: string(doc)} DB.Create(¬ebook) datafile := &DataFile{DocumentId: document.ID, Name: "DataName", Body: string(csv)} DB.Create(&datafile) fmt.Printf("FORKED %s\n", document.Uid) http.Redirect(w, r, fmt.Sprintf("/d/%s", document.Uid), 302) // w.Write([]byte(fmt.Sprintf("/d/%s", document.Uid))) }