This commit is contained in:
Zheng Kai
2023-03-31 10:12:18 +08:00
parent af429a393a
commit 8e72c62281
17 changed files with 229 additions and 61 deletions

View File

@@ -1,36 +1,37 @@
package core
import (
"errors"
"net/http"
"project/metrics"
"project/util"
"project/zj"
)
var errSkip = errors.New(`skip`)
// WebHandle ...
func (c *Core) WebHandle(w http.ResponseWriter, r *http.Request) {
p, err := req(w, r)
if err != nil {
metrics.ReqFailCount()
return
}
metrics.ReqBytes(len(p.Body))
pr := c.add(p, r)
go func() {
reqFile := util.CacheName(p.Hash()) + `-req.json`
if !util.FileExists(reqFile) {
util.WriteFile(reqFile, p.Body)
if err != errSkip {
metrics.ReqFailCount()
}
}()
pr.wait()
if pr.err != nil {
err500(w)
return
}
w.Write(pr.rsp)
metrics.ReqBytes(len(p.Body))
ab, cached, err := c.getAB(p, r)
if err != nil {
err500(w)
return
}
zj.J(`cached`, cached)
w.Header().Add(`Content-Type`, `application/json`)
w.Write(ab)
go doMetrics(ab, cached, r)
}