func Answer(req *http.Request, r render.Render) { returnData := make(map[string]interface{}) if req.Method == "GET" { blankList, choiceList, judgeList := appService.GetTitleId(1, 1, 1) returnData["blankList"] = blankList returnData["choiceList"] = choiceList returnData["judgeList"] = judgeList r.HTML(200, "answer", returnData) return } req.ParseForm() values := req.Form records := values.Get("records") if records == "" { r.Data(500, []byte("error")) } name, recordsList, err := appService.CheckRecords(records) if err != nil { r.Data(500, []byte("appService.CheckRecords error")) } returnData["name"] = name returnData["list"] = recordsList r.HTML(200, "record", returnData) return }
func Index(session sessions.Session, r render.Render) { id, ok := session.Get("user_id").(int64) if !ok { r.Data(200, []byte("error")) return } list, err := ga.GetPermissionByUserId(id) if err != nil { r.Data(200, []byte("get error")) return } r.HTML(200, "list", map[string]interface{}{"list": list}) return }
// Markdown renders a POST request into HTML. func (ap ApiRender) Markdown(r render.Render, req *http.Request) { if err := req.ParseForm(); err != nil { r.Data(500, []byte(err.Error())) return } if _, ok := req.Form["raw"]; !ok { r.Data(500, []byte("No Data")) return } raw := req.Form["raw"][0] data := util.Markdown([]byte(raw)) r.JSON(200, apiRenderResponse{Data: string(data)}) }
func getCertificates(req *http.Request, params martini.Params, r render.Render) { format := req.URL.Query().Get("format") newCA, err := ca.NewCA(config["root-dir"]) if err != nil { panic(err) } id := params["id"] idInt, err := strconv.Atoi(id) if err != nil { r.JSON(401, map[string]interface{}{ "result": "wrong id", }) return } cert, err := newCA.GetCertificate(int64(idInt)) if err != nil { r.JSON(401, map[string]interface{}{ "result": "cannot get cert", }) return } pem, err := cert.ToPEM() if err != nil { r.JSON(401, map[string]interface{}{ "result": "cannot get cert", }) return } if format == "file" { r.Data(200, pem) } else { r.JSON(200, map[string]interface{}{ "certificate": map[string]interface{}{ "id": cert.GetSerialNumber().Int64(), "crt": string(pem), //"csr": csr, }, }) } }
// Create allows for the creation of new posts. It returns a 204 // response on creation or a 500 response on error. func (ap ApiPosts) Create(r render.Render, req *http.Request) { var ( post models.Post err error t = "urlencoded" p interface{} ) root := util.Config().App.PostsDir contentType := req.Header.Get("content-type") if strings.Contains(contentType, "application/json") { t = "json" } switch t { case "json": dec := json.NewDecoder(req.Body) err = dec.Decode(&post) p = post default: err = req.ParseForm() p = req.Form } if err != nil { r.Data(500, []byte(err.Error())) return } if err = models.SavePost(root, p); err == nil { r.Data(204, []byte("Created")) } else { r.Data(500, []byte(err.Error())) } }
func postCertificates(req *http.Request, params martini.Params, r render.Render) { csrString := req.PostFormValue("csr") format := req.URL.Query().Get("format") csr, err := pkix.NewCertificateRequestFromPEM([]byte(csrString)) if err != nil { panic(err) } newCA, err := ca.NewCA(config["root-dir"]) if err != nil { panic(err) } cert, err := newCA.IssueCertificate(csr) if err != nil { panic(err) } certPem, err := cert.ToPEM() if err != nil { panic(err) } if format == "file" { r.Data(200, certPem) } else { r.JSON(200, map[string]interface{}{ "certificate": map[string]interface{}{ "id": cert.GetSerialNumber().Int64(), "crt": string(certPem), //"csr": csr, }, }) } }
func getCA(req *http.Request, params martini.Params, r render.Render) { format := req.URL.Query().Get("format") newCA, err := ca.NewCA(config["root-dir"]) if err != nil { panic(err) } pem, err := newCA.Certificate.ToPEM() if err != nil { panic(err) } if format == "file" { r.Data(200, pem) } else { r.JSON(200, map[string]interface{}{ "ca": map[string]interface{}{ "crt": string(pem), }, }) } }
func routeGetAdAsset(r render.Render, res http.ResponseWriter, req *http.Request, params martini.Params) { slot := params["slot"] id := params["id"] ad := getAd(req, slot, id) if ad == nil { r.JSON(404, map[string]string{"error": "not_found"}) return } content_type := "application/octet-stream" if ad.Type != "" { content_type = ad.Type } res.Header().Set("Content-Type", content_type) data, _ := rd.Get(assetKey(slot, id)).Result() range_str := req.Header.Get("Range") if range_str == "" { r.Data(200, []byte(data)) return } re := regexp.MustCompile("^bytes=(\\d*)-(\\d*)$") m := re.FindAllStringSubmatch(range_str, -1) if m == nil { r.Status(416) return } head_str := m[0][1] tail_str := m[0][2] if head_str == "" && tail_str == "" { r.Status(416) return } head := 0 tail := 0 if head_str != "" { head, _ = strconv.Atoi(head_str) } if tail_str != "" { tail, _ = strconv.Atoi(tail_str) } else { tail = len(data) - 1 } if head < 0 || head >= len(data) || tail < 0 { r.Status(416) return } range_data := data[head:(tail + 1)] content_range := fmt.Sprintf("bytes %d-%d/%d", head, tail, len(data)) res.Header().Set("Content-Range", content_range) res.Header().Set("Content-Length", strconv.Itoa(len(range_data))) r.Data(206, []byte(range_data)) }
func downloadHandler(render render.Render, params martini.Params) { dat, _ := ioutil.ReadFile("./upload/" + params["id"]) render.Data(200, dat) }