diff --git a/cmd/configui/main.go b/cmd/configui/main.go index 9e95f6c..92f2fec 100644 --- a/cmd/configui/main.go +++ b/cmd/configui/main.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "os" - "time" "kumoly.io/tools/configui" ) @@ -107,10 +106,11 @@ func main() { // setup routes server := &http.Server{ - Addr: flagBind, - WriteTimeout: time.Second * 30, - ReadTimeout: time.Second * 30, - Handler: cui, + Addr: flagBind, + // disable timeout due to cui controlling cmd timeouts + // WriteTimeout: time.Second * 30, + // ReadTimeout: time.Second * 30, + Handler: cui, } // start server diff --git a/configui.go b/configui.go index b6ee38b..4a67ac8 100644 --- a/configui.go +++ b/configui.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "html/template" + "time" "kumoly.io/tools/configui/public" ) @@ -34,6 +35,8 @@ type ConfigUI struct { NoReconfig bool `json:"no_reconfig"` AllowIP string `json:"allow_ip"` + CmdTimeout string `json:"timeout"` + cmdTimeout time.Duration Files []*File `json:"files"` fileIndex map[string]int @@ -61,12 +64,14 @@ func New() *ConfigUI { }, }).ParseFS(tmplFS, "templates/*.tmpl", "templates/**/*.tmpl")) return &ConfigUI{ - fileIndex: map[string]int{}, - AppName: "ConfigUI", - BaseUrl: "/", - PublicFS: public.FS, - TmplFS: tmplFS, - tmpl: tmpl, + fileIndex: map[string]int{}, + AppName: "ConfigUI", + BaseUrl: "/", + PublicFS: public.FS, + TmplFS: tmplFS, + tmpl: tmpl, + CmdTimeout: "10s", + cmdTimeout: time.Second * 10, } } @@ -114,6 +119,14 @@ func (cui *ConfigUI) LoadConfig(confstr string) error { if 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 cui.ResultBellow = tmpConf.ResultBellow diff --git a/file.go b/file.go index 795bc37..723183b 100644 --- a/file.go +++ b/file.go @@ -52,7 +52,7 @@ func (f *File) Write(data []byte) error { 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 == "" { return "", nil } @@ -75,7 +75,7 @@ func (f *File) Do() (string, error) { done <- string(out) }() select { - case <-time.After(10 * time.Second): + case <-time.After(CmdTimeout): cmd.Process.Kill() log.Println("timeout") return "", errors.New("command timeout") diff --git a/handler.go b/handler.go index 2bc77f8..d78f85e 100644 --- a/handler.go +++ b/handler.go @@ -122,7 +122,7 @@ func (cui *ConfigUI) Apply(w http.ResponseWriter, r *http.Request) { response(w, 404, []byte("file not found")) return } - result, err := file.Do() + result, err := file.Do(cui.cmdTimeout) if err != nil { panic(err) }