func GenerateHtml(apiCall *models.ApiCall) { shouldAddPathSpec := true for k, apiSpec := range spec.ApiSpecs { if apiSpec.Path == apiCall.CurrentPath && apiSpec.HttpVerb == apiCall.MethodType { shouldAddPathSpec = false apiCall.Id = count count += 1 deleteCommonHeaders(apiCall) avoid := false for _, currentApiCall := range spec.ApiSpecs[k].Calls { if apiCall.RequestBody == currentApiCall.RequestBody && apiCall.ResponseCode == currentApiCall.ResponseCode && apiCall.ResponseBody == currentApiCall.ResponseBody { avoid = true } } if !avoid { spec.ApiSpecs[k].Calls = append(apiSpec.Calls, *apiCall) } } } if shouldAddPathSpec { apiSpec := models.ApiSpec{ HttpVerb: apiCall.MethodType, Path: apiCall.CurrentPath, } apiCall.Id = count count += 1 deleteCommonHeaders(apiCall) apiSpec.Calls = append(apiSpec.Calls, *apiCall) spec.ApiSpecs = append(spec.ApiSpecs, apiSpec) } filePath, err := filepath.Abs(config.DocPath) dataFile, err := os.Create(filePath + ".json") if err != nil { log.Println(err) return } defer dataFile.Close() data, err := json.Marshal(spec) if err != nil { log.Println(err) return } _, err = dataFile.Write(data) if err != nil { log.Println(err) return } generateHtml() }
func After(apiCall *models.ApiCall, writer *httptest.ResponseRecorder, w http.ResponseWriter, r *http.Request) { if strings.Contains(r.RequestURI, ".ico") { fmt.Fprintf(w, writer.Body.String()) return } if writer.Code != 404 { apiCall.MethodType = r.Method apiCall.CurrentPath = strings.Split(r.RequestURI, "?")[0] apiCall.ResponseBody = writer.Body.String() apiCall.ResponseCode = writer.Code apiCall.ResponseHeader, apiCall.ResponseAnnotations = ReadHeadersFromResponse(writer) go yaag.GenerateHtml(apiCall) } for key, value := range apiCall.ResponseHeader { w.Header().Add(key, value) } w.WriteHeader(writer.Code) w.Write(writer.Body.Bytes()) }
func Before(apiCall *models.ApiCall, req *http.Request) { apiCall.RequestHeader = ReadHeaders(req) apiCall.RequestUrlParams = ReadQueryParams(req) val, ok := apiCall.RequestHeader["Content-Type"] log.Println(val) if ok { ct := strings.TrimSpace(apiCall.RequestHeader["Content-Type"]) switch ct { case "application/x-www-form-urlencoded": fallthrough case "application/json, application/x-www-form-urlencoded": log.Println("Reading form") apiCall.PostForm = ReadPostForm(req) case "application/json": log.Println("Reading body") apiCall.RequestBody = *ReadBody(req) default: if strings.Contains(ct, "multipart/form-data") { handleMultipart(apiCall, req) } } } }
func Document() gin.HandlerFunc { return func(c *gin.Context) { if !yaag.IsOn() { return } writer := httptest.NewRecorder() apiCall := models.ApiCall{} middleware.Before(&apiCall, c.Request) c.Next() if writer.Code != 404 { apiCall.MethodType = c.Request.Method apiCall.CurrentPath = strings.Split(c.Request.RequestURI, "?")[0] apiCall.ResponseBody = "" apiCall.ResponseCode = c.Writer.Status() headers := map[string]string{} for k, v := range c.Writer.Header() { log.Println(k, v) headers[k] = strings.Join(v, " ") } apiCall.ResponseHeader = headers go yaag.GenerateHtml(&apiCall) } } }
func handleMultipart(apiCall *models.ApiCall, req *http.Request) { apiCall.RequestHeader["Content-Type"] = "multipart/form-data" req.ParseMultipartForm(MaxInMemoryMultipartSize) apiCall.PostForm = ReadMultiPostForm(req.MultipartForm) }
func FilterForApiDoc(c *revel.Controller, fc []revel.Filter) { if record, _ := revel.Config.Bool("yaag.record"); !record { fc[0](c, fc[1:]) return } w := httptest.NewRecorder() c.Response = revel.NewResponse(w) httpVerb := c.Request.Method customParams := make(map[string]interface{}) headers := make(map[string]string) hasJson := false hasXml := false body := middleware.ReadBody(c.Request.Request) if c.Request.ContentType == "application/json" { if httpVerb == "POST" || httpVerb == "PUT" || httpVerb == "PATCH" { err := json.Unmarshal([]byte(*body), &customParams) if err != nil { log.Println("Json Error ! ", err) } else { hasJson = true } } else { err := json.Unmarshal([]byte(c.Request.URL.RawQuery), &customParams) if err != nil { log.Println("Json Error ! ", err) } else { hasJson = true } } } else if c.Request.ContentType == "application/xml" { if httpVerb == "POST" || httpVerb == "PUT" || httpVerb == "PATCH" { err := xml.Unmarshal([]byte(*body), &customParams) if err != nil { log.Println("Xml Error ! ", err) } else { hasXml = true } } else { err := xml.Unmarshal([]byte(c.Request.URL.RawQuery), &customParams) if err != nil { log.Println("Json Error ! ", err) } else { hasXml = true } } } log.Println(hasJson, hasXml) // call remaiing filters fc[0](c, fc[1:]) c.Result.Apply(c.Request, c.Response) htmlValues := models.ApiCall{} htmlValues.CommonRequestHeaders = make(map[string]string) // get headers for k, v := range c.Request.Header { isCommon := false for _, hk := range yaag.CommonHeaders { if k == hk { isCommon = true htmlValues.CommonRequestHeaders[k] = strings.Join(v, " ") break } } if !isCommon { headers[k] = strings.Join(v, " ") } } htmlValues.MethodType = httpVerb htmlValues.CurrentPath = c.Request.URL.Path htmlValues.PostForm = make(map[string]string) for k, v := range c.Params.Form { htmlValues.PostForm[k] = strings.Join(v, " ") } htmlValues.RequestBody = *body htmlValues.RequestHeader = headers htmlValues.RequestUrlParams = make(map[string]string) for k, v := range c.Request.URL.Query() { htmlValues.RequestUrlParams[k] = strings.Join(v, " ") } htmlValues.ResponseHeader = make(map[string]string) htmlValues.ResponseBody = w.Body.String() for k, v := range w.Header() { htmlValues.ResponseHeader[k] = strings.Join(v, " ") } htmlValues.ResponseCode = w.Code go yaag.GenerateHtml(&htmlValues) }