fix: #46
continuous-integration/drone/tag Build is passing Details

feat/muzan v0.1.6
Evan Chen 2021-11-03 18:08:37 +08:00
parent 3a64217a10
commit cff4c13b78
4 changed files with 27 additions and 14 deletions

View File

@ -7,7 +7,6 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"time"
"kumoly.io/tools/configui" "kumoly.io/tools/configui"
) )
@ -108,8 +107,9 @@ func main() {
// setup routes // setup routes
server := &http.Server{ server := &http.Server{
Addr: flagBind, Addr: flagBind,
WriteTimeout: time.Second * 30, // disable timeout due to cui controlling cmd timeouts
ReadTimeout: time.Second * 30, // WriteTimeout: time.Second * 30,
// ReadTimeout: time.Second * 30,
Handler: cui, Handler: cui,
} }

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"time"
"kumoly.io/tools/configui/public" "kumoly.io/tools/configui/public"
) )
@ -34,6 +35,8 @@ type ConfigUI struct {
NoReconfig bool `json:"no_reconfig"` NoReconfig bool `json:"no_reconfig"`
AllowIP string `json:"allow_ip"` AllowIP string `json:"allow_ip"`
CmdTimeout string `json:"timeout"`
cmdTimeout time.Duration
Files []*File `json:"files"` Files []*File `json:"files"`
fileIndex map[string]int fileIndex map[string]int
@ -67,6 +70,8 @@ func New() *ConfigUI {
PublicFS: public.FS, PublicFS: public.FS,
TmplFS: tmplFS, TmplFS: tmplFS,
tmpl: tmpl, tmpl: tmpl,
CmdTimeout: "10s",
cmdTimeout: time.Second * 10,
} }
} }
@ -114,6 +119,14 @@ func (cui *ConfigUI) LoadConfig(confstr string) error {
if cui.BaseUrl == "" { if cui.BaseUrl == "" {
cui.BaseUrl = "/" cui.BaseUrl = "/"
} }
ct, err := time.ParseDuration(tmpConf.CmdTimeout)
if err != nil || cui.CmdTimeout == "" {
cui.CmdTimeout = "10s"
cui.cmdTimeout = time.Second * 10
} else {
cui.CmdTimeout = tmpConf.CmdTimeout
cui.cmdTimeout = ct
}
// NOT implemented // NOT implemented
cui.ResultBellow = tmpConf.ResultBellow cui.ResultBellow = tmpConf.ResultBellow

View File

@ -52,7 +52,7 @@ func (f *File) Write(data []byte) error {
return os.WriteFile(f.Path, data, info.Mode()) return os.WriteFile(f.Path, data, info.Mode())
} }
func (f *File) Do() (string, error) { func (f *File) Do(CmdTimeout time.Duration) (string, error) {
if f.Action == "" { if f.Action == "" {
return "", nil return "", nil
} }
@ -75,7 +75,7 @@ func (f *File) Do() (string, error) {
done <- string(out) done <- string(out)
}() }()
select { select {
case <-time.After(10 * time.Second): case <-time.After(CmdTimeout):
cmd.Process.Kill() cmd.Process.Kill()
log.Println("timeout") log.Println("timeout")
return "", errors.New("command timeout") return "", errors.New("command timeout")

View File

@ -122,7 +122,7 @@ func (cui *ConfigUI) Apply(w http.ResponseWriter, r *http.Request) {
response(w, 404, []byte("file not found")) response(w, 404, []byte("file not found"))
return return
} }
result, err := file.Do() result, err := file.Do(cui.cmdTimeout)
if err != nil { if err != nil {
panic(err) panic(err)
} }