commit 6a2a2abbad8350b21dd05bb976fd6eed4b1a2c99
parent 73e870f522af0be4749e711c698941a7bafccad5
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 22 Jul 2024 08:50:45 +0900
cleanup
Diffstat:
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/main.go b/main.go
@@ -219,12 +219,14 @@ func main() {
}
intc := make(chan os.Signal)
done := make(chan struct{})
+ listenErrChan := make(chan struct{})
signal.Notify(intc, os.Interrupt)
- go writer(intc, done, bufc, f)
- http.HandleFunc("/", handler)
+ go writer(intc, listenErrChan, done, bufc, f)
go func() {
- log.Printf("listenAndServe: %v\n",
- http.ListenAndServe("localhost:8000", nil))
+ http.HandleFunc("/", handler)
+ err := http.ListenAndServe("localhost:8000", nil)
+ log.Printf("listenAndServe: %v\n", err)
+ close(listenErrChan)
}()
<-done
os.Exit(0)
@@ -243,13 +245,11 @@ func handler(w http.ResponseWriter, r *http.Request) {
log.Printf("makeAns: %v\n", err)
return
}
- log.Printf("makeAns: %q\n", a)
A, err := json.MarshalIndent(a, "", " ")
if err != nil {
log.Printf("marchalIndent: %v\n", err)
return
}
- log.Printf("A: %v\n", string(A))
bufc <- A
} else {
fmt.Fprint(w, header+Q.HTML()+footer)
@@ -258,7 +258,8 @@ func handler(w http.ResponseWriter, r *http.Request) {
// Writer reads json data from c and writes them to f.
// This function closes f after c is closed.
-func writer(intc <-chan os.Signal, done chan<- struct{}, c <-chan []byte, f *os.File) {
+func writer(intc <-chan os.Signal, listenErrChan <-chan struct{},
+ done chan<- struct{}, c <-chan []byte, f *os.File) {
defer close(done)
const bufSize = 4096
var buf bytes.Buffer
@@ -270,7 +271,6 @@ L:
log.Printf("error: compact: %v\n", err)
continue
}
- log.Printf("buf: %v\n", buf.String())
if buf.Len() >= bufSize {
if _, err := f.Write(buf.Next(bufSize)); err != nil {
log.Printf("error: write to file: %v\n", err)
@@ -279,6 +279,8 @@ L:
}
case <-intc:
break L
+ case <-listenErrChan:
+ break L
}
}
if _, err := f.Write(buf.Bytes()); err != nil {