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"
"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

View File

@ -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

View File

@ -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")

View File

@ -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)
}