diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2023-09-03 16:30:35 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2023-09-03 16:30:35 +0900 |
commit | 0f50dc848e58b47408713c2c31f2be73287c030a (patch) | |
tree | 6bf96117303bbde31fc71092302e185bdfb37429 /src/server.go | |
parent | c5e4b83de3a201e496f356e826529a45165138ea (diff) |
Add 'GET /' endpoint for getting the program state (experimental)
Related #3372
Diffstat (limited to 'src/server.go')
-rw-r--r-- | src/server.go | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/server.go b/src/server.go index c583400f..36980157 100644 --- a/src/server.go +++ b/src/server.go @@ -23,11 +23,12 @@ const ( ) type httpServer struct { - apiKey []byte - channel chan []*action + apiKey []byte + actionChannel chan []*action + responseChannel chan string } -func startHttpServer(port int, channel chan []*action) (error, int) { +func startHttpServer(port int, actionChannel chan []*action, responseChannel chan string) (error, int) { if port < 0 { return nil, port } @@ -50,8 +51,9 @@ func startHttpServer(port int, channel chan []*action) (error, int) { } server := httpServer{ - apiKey: []byte(os.Getenv("FZF_API_KEY")), - channel: channel, + apiKey: []byte(os.Getenv("FZF_API_KEY")), + actionChannel: actionChannel, + responseChannel: responseChannel, } go func() { @@ -83,13 +85,18 @@ func (server *httpServer) handleHttpRequest(conn net.Conn) string { contentLength := 0 apiKey := "" body := "" - unauthorized := func(message string) string { + answer := func(code string, message string) string { message += "\n" - return httpUnauthorized + fmt.Sprintf("Content-Length: %d%s", len(message), crlf+crlf+message) + return code + fmt.Sprintf("Content-Length: %d%s", len(message), crlf+crlf+message) + } + unauthorized := func(message string) string { + return answer(httpUnauthorized, message) } bad := func(message string) string { - message += "\n" - return httpBadRequest + fmt.Sprintf("Content-Length: %d%s", len(message), crlf+crlf+message) + return answer(httpBadRequest, message) + } + good := func(message string) string { + return answer(httpOk+"Content-Type: application/json"+crlf, message) } conn.SetReadDeadline(time.Now().Add(httpReadTimeout)) scanner := bufio.NewScanner(conn) @@ -110,7 +117,12 @@ func (server *httpServer) handleHttpRequest(conn net.Conn) string { text := scanner.Text() switch section { case 0: - if !strings.HasPrefix(text, "POST / HTTP") { + // TODO: Parameter support e.g. "GET /?limit=100 HTTP" + if strings.HasPrefix(text, "GET / HTTP") { + server.actionChannel <- []*action{{t: actResponse}} + response := <-server.responseChannel + return good(response) + } else if !strings.HasPrefix(text, "POST / HTTP") { return bad("invalid request method") } section++ @@ -160,6 +172,6 @@ func (server *httpServer) handleHttpRequest(conn net.Conn) string { return bad("no action specified") } - server.channel <- actions + server.actionChannel <- actions return httpOk } |