Пример #1
0
func main() {
	args := flag.Args()

	path := "."
	if len(args) != 0 {
		path = args[0]
	}
	ext = "." + ext

	wg := sync.WaitGroup{}
	errors.Fatal(
		filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}

			if !info.IsDir() && filepath.Ext(info.Name()) == ext {
				wg.Add(1)
				go process(path, &wg)
			}

			return nil
		}),
	)

	wg.Wait()

	errors.CondDo(len(as.categories) == 0,
		errors.Err("No files contains api in this dir or file"),
		errors.FatalAnyln)
	errors.CondDo(outputType != "md",
		errors.Err("Sorry, currently only support markdown format"),
		errors.FatalAnyln)

	orders := slices.Strings(strings2.SplitAndTrim(order, orderSep)).Clear("")
	if fname != "" {
		errors.Fatalln(file.OpenOrCreate(fname, overwrite, func(fd *os.File) error {
			as.WriteMarkDown(fd, orders)

			return nil
		}))
	} else {
		as.WriteMarkDown(os.Stdout, orders)
	}
}
Пример #2
0
func (c *CORS) preflight(req zerver.Request, resp zerver.Response, method, headers string) {
	origin := "*"
	if !c.allowAll {
		origin = req.GetHeader(_CORS_ORIGIN)
		if !c.allow(origin) {
			resp.StatusCode(http.StatusOK)
			return
		}
	}

	respHeaders := resp.Headers()
	respHeaders.Set(_CORS_ALLOWORIGIN, origin)
	upperMethod := strings.ToUpper(method)

	for _, m := range c.Methods {
		if m == upperMethod {
			respHeaders.Add(_CORS_ALLOWMETHODS, method)
			break
		}
	}

	for _, h := range strings2.SplitAndTrim(headers, ",") {
		for _, ch := range c.Headers {
			if strings.ToLower(h) == ch { // c.Headers already ToLowered when Init
				respHeaders.Add(_CORS_ALLOWHEADERS, ch)
				break
			}
		}
	}

	respHeaders.Set(_CORS_ALLOWCREDENTIALS, c.allowCredentials)
	if c.exposeHeaders != "" {
		respHeaders.Set(_CORS_EXPOSEHEADERS, c.exposeHeaders)
	}

	if c.preflightMaxage != "" {
		respHeaders.Set(_CORS_MAXAGE, c.preflightMaxage)
	}

	resp.StatusCode(http.StatusOK)
}
Пример #3
0
func (c *CORS) preflight(req zerver.Request, resp zerver.Response, method, headers string) {
	origin := "*"
	if !c.allowAll {
		origin = req.Header(_CORS_ORIGIN)
		if !c.allow(origin) {
			goto END
		}
	}

	resp.SetHeader(_CORS_ALLOWORIGIN, origin)
	method = strings.ToUpper(method)

	for _, m := range c.Methods {
		if m == method {
			resp.AddHeader(_CORS_ALLOWMETHODS, m)
			break
		}
	}

	for _, h := range strings2.SplitAndTrim(headers, ",") {
		for _, ch := range c.Headers {
			if strings.ToLower(h) == ch { // c.Headers already ToLowered when Init
				resp.AddHeader(_CORS_ALLOWHEADERS, ch)
				break
			}
		}
	}

	resp.SetHeader(_CORS_ALLOWCREDENTIALS, c.allowCredentials)
	if c.exposeHeaders != "" {
		resp.SetHeader(_CORS_EXPOSEHEADERS, c.exposeHeaders)
	}

	if c.preflightMaxage != "" {
		resp.SetHeader(_CORS_MAXAGE, c.preflightMaxage)
	}

END:
	resp.ReportOK()
}
Пример #4
0
func process(path string, wg *sync.WaitGroup) {
	sectionState := PARSE_INIT
	dataState := PARSE_INIT
	var a *API
	var sec *section
	var category string = "global"

	dataStart := 0

	err := file.Filter(path, func(linum int, line []byte) error {
		line = bytes.Replace(line, tab, tabreplace, -1)
		originLine := string(line)

		if !bytes.HasPrefix(line, unsafe2.Bytes(comment)) {
			sectionState = PARSE_INIT
			return nil
		}

		line = bytes.TrimSpace(line[len(comment):])
		line = bytes.Replace(line, []byte{'\t'}, []byte("    "), -1)
		if len(line) == 0 {
			return nil
		}

		linestr := string(line)
		switch tag := matchTag(linestr); tag {
		case TAG_CATEGORY:
			category = strings.TrimSpace(linestr[tag.Strlen():])

		case TAG_API:
			a = &API{}
			sectionState = PARSE_API
			name := strings.TrimSpace(linestr[tag.Strlen():])
			names := strings2.SplitAndTrim(name, TAG_AT_CATEGORY)
			a.name = names[0]

			if len(names) > 1 {
				as.AddAPI(names[1], a)
			} else {
				as.AddAPI(category, a)
			}
		case TAG_SUBAPI:
			a = &API{}
			sectionState = PARSE_API
			a.name = strings.TrimSpace(linestr[tag.Strlen():])

			as.AddSubAPI(a.name, a)
		case TAG_APIINCL:
			if a != nil {
				a.subapis = append(a.subapis, strings2.SplitAndTrim(linestr[tag.Strlen():], ",")...)
			}
		case TAG_ENDAPI:
			sectionState = PARSE_INIT

		case TAG_HEADER:
			sec = &section{}
			sectionState = PARSE_BODY
			dataState = PARSE_HEADER

			as.AddSubHeader(strings.TrimSpace(linestr[tag.Strlen():]), sec)
		case TAG_HEADERINCL:
			if sec != nil {
				sec.subheaders = append(sec.subheaders, strings2.SplitAndTrim(linestr[tag.Strlen():], ",")...)
			}

		case TAG_RESP:
			if sectionState != PARSE_INIT {
				sectionState = PARSE_BODY
				dataState = PARSE_STATUS
				sec = &section{}
				a.resps = append(a.resps, sec)
			}
		case TAG_SUBRESP:
			name := strings.TrimSpace(linestr[tag.Strlen():])
			sectionState = PARSE_BODY
			dataState = PARSE_STATUS
			sec = &section{}
			as.AddSubResp(name, sec)
		case TAG_RESPINCL:
			if a != nil {
				a.subresps = append(a.subresps, strings2.SplitAndTrim(linestr[tag.Strlen():], ",")...)
			}

		case TAG_REQ:
			if sectionState != PARSE_INIT {
				sectionState = PARSE_BODY
				sec = &section{}
				a.req = sec
				dataState = PARSE_STATUS
			}

		default:
			if sectionState == PARSE_INIT {
				break
			} else if sectionState == PARSE_API {
				a.desc = append(a.desc, linestr)

				break
			}

			switch dataState {
			case PARSE_STATUS:
				sec.headerLine = linestr
				dataState = PARSE_HEADER

			case PARSE_HEADER:
				if !strings.HasPrefix(linestr, TAG_DATA.String()) {
					sec.headers = append(sec.headers, linestr)

					break
				}

				dataStart = strings.Index(originLine, TAG_DATA.String()) + TAG_DATA.Strlen()
				dataState = PARSE_DATA
				fallthrough

			case PARSE_DATA:
				if len(originLine) >= dataStart {
					sec.datas = append(sec.datas, originLine[dataStart:])
				} else {
					reportErrorln("skipped data line: %s:%d:%s", path, linum, originLine)
				}
			}
		}

		return nil
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	wg.Done()
}