m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-16 15:23:48 -05:00

Rewrite HTTP server without net/http

This cuts down the binary size from 5.7MB to 3.3MB.
This commit is contained in:
Junegunn Choi
2022-12-20 16:28:36 +09:00
parent 1ba7484d60
commit 4b055bf260
2 changed files with 113 additions and 36 deletions

View File

@@ -3,10 +3,8 @@ package fzf
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"os/signal"
"regexp"
@@ -626,39 +624,6 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
return &t
}
func (t *Terminal) startServer() {
if t.listenPort == 0 {
return
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
response := ""
actions := parseSingleActionList(string(body), func(message string) {
response = message
})
if len(response) > 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, response)
return
}
t.serverChan <- actions
})
go func() {
http.ListenAndServe(fmt.Sprintf(":%d", t.listenPort), nil)
}()
}
func borderLines(shape tui.BorderShape) int {
switch shape {
case tui.BorderHorizontal, tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderDouble:
@@ -2535,7 +2500,7 @@ func (t *Terminal) Loop() {
looping := true
_, startEvent := t.keymap[tui.Start.AsEvent()]
t.startServer()
startHttpServer(t.listenPort, t.serverChan)
eventChan := make(chan tui.Event)
needBarrier := true
barrier := make(chan bool)