func index(c web.C, w http.ResponseWriter, r *http.Request) { var err error if r.Method == "POST" { err = r.ParseForm() if err != nil { http.Error(w, "No good!", 400) return } p := Paste{} err = param.Parse(r.PostForm, &p) if err != nil { http.Error(w, "You're doing it wrong. In some way at least.", 500) return } pe := PasteEntry{} pe.Paste = p u, err := uuid.NewV4() if err != nil { http.Error(w, "Something went wrong.", 500) } pe.Key = u.String() db.Create(&pe) http.Redirect(w, r, fmt.Sprintf("/paste/%s", pe.Key), http.StatusFound) return } t, err := loadTemplate("index.html") if err != nil { log.Fatal(err) } t.Execute(w, c.Env) }
func CreateShortURL(c web.C, w http.ResponseWriter, r *http.Request) { var url URL r.ParseForm() err := param.Parse(r.Form, &url) logIf(err) log.Printf("Create a short url for: %s User: %s\n", url.Url, url.Username) shortURL, err := models.AddURL(url.Username, url.Url) w.Write([]byte(shortURL)) }
func CreateUser(c web.C, w http.ResponseWriter, r *http.Request) { var user User r.ParseForm() err := param.Parse(r.Form, &user) logIf(err) exists := models.AddUser(user.Username, user.Name) m := map[string]bool{"exists": exists} res, _ := json.Marshal(m) w.Write(res) }
func CreateController(c web.C, w http.ResponseWriter, r *http.Request) { var table Table r.ParseForm() err := param.Parse(r.Form, &table) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } result := Allocate(table.TargetIP, table.TargetPort, table.BoundPort) if result != true { http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprint(w, "Create") }
// NewGreet creates a new greet (POST "/greets"). Creates a greet and redirects // you to the created greet. // // To post a new greet, try this at a shell: // $ now=$(date +'%Y-%m-%mT%H:%M:%SZ') // $ curl -i -d "user=carl&message=Hello+World&time=$now" localhost:8000/greets func NewGreet(w http.ResponseWriter, r *http.Request) { var greet Greet // Parse the POST body into the Greet struct. The format is the same as // is emitted by (e.g.) jQuery.param. r.ParseForm() err := param.Parse(r.Form, &greet) if err != nil || len(greet.Message) > 140 { http.Error(w, err.Error(), http.StatusBadRequest) return } // We make no effort to prevent races against other insertions. Greets = append(Greets, greet) url := fmt.Sprintf("/greets/%d", len(Greets)-1) http.Redirect(w, r, url, http.StatusCreated) }
func insertPost(w http.ResponseWriter, r *http.Request) { db, err := bolt.Open("my.db", 0600, nil) if err != nil { log.Fatal(err) } var p Post r.ParseForm() err = param.Parse(r.Form, &p) if err != nil { log.Fatal(err) } insert(db, p.Contents) w.Header().Set("Content-Type", "application/json") fmt.Fprintf(w, "{\"status\": \"success\"}") defer db.Close() }
// Update is the update endpoint of the Wiki func (w *Wiki) Update(c web.C, rw http.ResponseWriter, r *http.Request) { name := w.getPageName(c.URLParams["name"]) // Parse the POST body r.ParseForm() var fd formData param.Parse(r.Form, &fd) w.DB().Update(func(tx *db.Tx) error { p := db.Page{Tx: tx, Name: name} p.Text = []byte(fd.Text) return p.Save() }) path := "/" + string(name) if path == "/root" { path = "/" } http.Redirect(rw, r, path, 302) }