Add support for limit and offset parameters for GET / endpoint

Related #3372
This commit is contained in:
Junegunn Choi
2023-09-18 00:51:40 +09:00
parent edfdcc8cee
commit 901939bd96
2 changed files with 46 additions and 12 deletions

View File

@@ -8,11 +8,23 @@ import (
"fmt"
"net"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var getRegex *regexp.Regexp
func init() {
getRegex = regexp.MustCompile(`^GET /(?:\?([a-z0-9=&]+))? HTTP`)
}
type getParams struct {
limit int
offset int
}
const (
crlf = "\r\n"
httpOk = "HTTP/1.1 200 OK" + crlf
@@ -117,9 +129,9 @@ func (server *httpServer) handleHttpRequest(conn net.Conn) string {
text := scanner.Text()
switch section {
case 0:
// TODO: Parameter support e.g. "GET /?limit=100 HTTP"
if strings.HasPrefix(text, "GET / HTTP") {
server.actionChannel <- []*action{{t: actResponse}}
getMatch := getRegex.FindStringSubmatch(text)
if len(getMatch) > 0 {
server.actionChannel <- []*action{{t: actResponse, a: getMatch[1]}}
response := <-server.responseChannel
return good(response)
} else if !strings.HasPrefix(text, "POST / HTTP") {
@@ -175,3 +187,25 @@ func (server *httpServer) handleHttpRequest(conn net.Conn) string {
server.actionChannel <- actions
return httpOk
}
func parseGetParams(query string) getParams {
params := getParams{limit: 100, offset: 0}
for _, pair := range strings.Split(query, "&") {
parts := strings.SplitN(pair, "=", 2)
if len(parts) == 2 {
switch parts[0] {
case "limit":
val, err := strconv.Atoi(parts[1])
if err == nil {
params.limit = val
}
case "offset":
val, err := strconv.Atoi(parts[1])
if err == nil {
params.offset = val
}
}
}
}
return params
}