parent
3a64217a10
commit
cff4c13b78
|
@ -7,7 +7,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"kumoly.io/tools/configui"
|
"kumoly.io/tools/configui"
|
||||||
)
|
)
|
||||||
|
@ -107,10 +106,11 @@ 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,
|
||||||
Handler: cui,
|
// ReadTimeout: time.Second * 30,
|
||||||
|
Handler: cui,
|
||||||
}
|
}
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
|
|
25
configui.go
25
configui.go
|
@ -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
|
||||||
|
@ -61,12 +64,14 @@ func New() *ConfigUI {
|
||||||
},
|
},
|
||||||
}).ParseFS(tmplFS, "templates/*.tmpl", "templates/**/*.tmpl"))
|
}).ParseFS(tmplFS, "templates/*.tmpl", "templates/**/*.tmpl"))
|
||||||
return &ConfigUI{
|
return &ConfigUI{
|
||||||
fileIndex: map[string]int{},
|
fileIndex: map[string]int{},
|
||||||
AppName: "ConfigUI",
|
AppName: "ConfigUI",
|
||||||
BaseUrl: "/",
|
BaseUrl: "/",
|
||||||
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
|
||||||
|
|
4
file.go
4
file.go
|
@ -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")
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue