Compare commits

...

12 Commits

Author SHA1 Message Date
Evan Chen 639e3e97a5 feat: shrink modal width 2021-11-30 18:42:33 +08:00
Evan Chen 49b28eef61 update version 2021-11-30 16:40:58 +08:00
Evan Chen 9bbabf6479 add configui
continuous-integration/drone/tag Build is passing Details
2021-11-30 16:34:36 +08:00
Evan Chen dcc140b722 fix: file blocked by reporter
continuous-integration/drone/tag Build is passing Details
2021-11-18 01:51:18 +08:00
Evan Chen 5634bf108f fix: shell not configuable
continuous-integration/drone/tag Build is passing Details
2021-11-18 01:24:47 +08:00
Evan 591fb2cfa1 chore: change wording 2021-11-17 22:11:49 +08:00
Evan Chen 6801e56013 chore 2021-11-17 18:36:10 +08:00
Evan Chen 93410f9ddf fix: change highlight color
continuous-integration/drone/tag Build is failing Details
2021-11-17 18:35:10 +08:00
Evan Chen 0b0a54085c fix: #58 2021-11-17 17:41:21 +08:00
Evan Chen 221b52da75 build: go mod tidy 2021-11-17 17:24:46 +08:00
Evan Chen 3895e90876 fix: #56 2021-11-17 17:24:04 +08:00
Evan Chen b0d76dff3e chore 2021-11-15 16:54:55 +08:00
18 changed files with 427 additions and 5997 deletions

View File

@ -65,6 +65,33 @@ sudo sh -c "curl -fsSL RELEASE_URL | tar -C /usr/local/bin/ -xz"
`configui -f PATH/TO/CONFIG` `configui -f PATH/TO/CONFIG`
## Add custom links in nav
**config**
```json
{
"cust":"path/to/dir"
}
```
**No link**
> path/to/dir/none.tmpl
```go
{{ define "links" }}
<!-- none -->
{{end}}
```
**Custom links**
> path/to/dir/links.tmpl
```go
{{ define "links" }}
<a class="button is-white level-item"
href="https://kumoly.io/tools/configui/issues"
>Report Issue</a>
{{end}}
```
## Add integrations to ConfigUI ## Add integrations to ConfigUI
```go ```go

View File

@ -18,6 +18,7 @@ var (
flagConfigPath string flagConfigPath string
flagBind string flagBind string
flagAddr string
flagNoReconfig bool flagNoReconfig bool
flagLogFile string flagLogFile string
flagAllow string flagAllow string
@ -36,7 +37,8 @@ func init() {
flag.StringVar(&flagAction, "c", "", "cmd to apply") flag.StringVar(&flagAction, "c", "", "cmd to apply")
flag.StringVar(&flagLogFile, "log", "", "log to file") flag.StringVar(&flagLogFile, "log", "", "log to file")
flag.StringVar(&flagAllow, "allow", "", "IPs to allow, blank to allow all") flag.StringVar(&flagAllow, "allow", "", "IPs to allow, blank to allow all")
flag.StringVar(&flagBind, "bind", "0.0.0.0:8000", "address to bind") flag.StringVar(&flagBind, "bind", "0.0.0.0:8000", "address to bind, (deprecated, use -addr instead)")
flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to bind")
flag.BoolVar(&flagNoReconfig, "static", false, "disable config api") flag.BoolVar(&flagNoReconfig, "static", false, "disable config api")
flag.BoolVar(&flagVer, "v", false, "show version") flag.BoolVar(&flagVer, "v", false, "show version")
flag.Usage = func() { flag.Usage = func() {
@ -53,6 +55,11 @@ func main() {
return return
} }
// deprecate fix
if flagBind != "0.0.0.0:8000" && flagAddr == "0.0.0.0:8000" {
flagAddr = flagBind
}
cui := configui.New() cui := configui.New()
// setup values // setup values
@ -98,7 +105,7 @@ func main() {
// setup routes // setup routes
server := &http.Server{ server := &http.Server{
Addr: flagBind, Addr: flagAddr,
// disable timeout due to cui controlling cmd timeouts // disable timeout due to cui controlling cmd timeouts
// WriteTimeout: time.Second * 30, // WriteTimeout: time.Second * 30,
// ReadTimeout: time.Second * 30, // ReadTimeout: time.Second * 30,
@ -106,7 +113,7 @@ func main() {
} }
// start server // start server
log.Info("Listening on ", flagBind) log.Info("Listening on ", flagAddr)
err := server.ListenAndServe() err := server.ListenAndServe()
if err != nil { if err != nil {
log.Error(err) log.Error(err)

View File

@ -7,6 +7,8 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"os" "os"
"path/filepath"
"runtime"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -16,14 +18,18 @@ import (
"kumoly.io/tools/configui/public" "kumoly.io/tools/configui/public"
) )
var UNIX_SHELL = "/usr/bin/sh" const UNIX_SHELL = "/usr/bin/sh"
var WIN_SHELL = "C:\\Windows\\System32\\cmd" const WIN_SHELL = "C:\\Windows\\System32\\cmd"
const DARWIN_SHELL = "/bin/bash"
const version = "v0.1.12" const version = "v0.1.14"
//go:embed templates //go:embed templates
var tmplFS embed.FS var tmplFS embed.FS
//go:embed schema.json
var SCHEMA []byte
var Ext2Mode map[string]string = map[string]string{ var Ext2Mode map[string]string = map[string]string{
"go": "golang", "go": "golang",
"log": "sh", "log": "sh",
@ -52,6 +58,7 @@ type ConfigUI struct {
Prod bool `json:"production"` Prod bool `json:"production"`
BaseUrl string `json:"base_url"` BaseUrl string `json:"base_url"`
ConfigPath string `json:"config_path"` ConfigPath string `json:"config_path"`
SHELL string `json:"shell"`
NoReconfig bool `json:"no_reconfig"` NoReconfig bool `json:"no_reconfig"`
AllowIP string `json:"allow_ip"` AllowIP string `json:"allow_ip"`
@ -71,6 +78,7 @@ type ConfigUI struct {
LogLevel klog.Llevel `json:"log_level"` LogLevel klog.Llevel `json:"log_level"`
TmplFS embed.FS `json:"-"` TmplFS embed.FS `json:"-"`
CustTmpl string `json:"cust,omitempty"`
tmpl *engine.Engine tmpl *engine.Engine
PublicFS embed.FS `json:"-"` PublicFS embed.FS `json:"-"`
log *klog.Logger log *klog.Logger
@ -92,8 +100,18 @@ func New() *ConfigUI {
return strings.ReplaceAll(name, " ", "-") return strings.ReplaceAll(name, " ", "-")
}, },
}).ParseFS(tmplFS, "templates/*.tmpl", "templates/**/*.tmpl")) }).ParseFS(tmplFS, "templates/*.tmpl", "templates/**/*.tmpl"))
sh := ""
switch runtime.GOOS {
case "darwin":
sh = DARWIN_SHELL
case "windows":
sh = WIN_SHELL
default:
sh = UNIX_SHELL
}
return &ConfigUI{ return &ConfigUI{
fileIndex: map[string]int{}, fileIndex: map[string]int{},
SHELL: sh,
Prod: true, Prod: true,
AppName: "ConfigUI", AppName: "ConfigUI",
BaseUrl: "/", BaseUrl: "/",
@ -134,6 +152,9 @@ func (cui *ConfigUI) LoadConfig(confstr string) error {
// construct fileIndex // construct fileIndex
tmpIndex := map[string]int{} tmpIndex := map[string]int{}
for i, f := range tmpConf.Files { for i, f := range tmpConf.Files {
if f.Name == "" && f.Path == "" {
continue
}
if f.Name == "" { if f.Name == "" {
f.Name = f.Path f.Name = f.Path
} }
@ -159,6 +180,17 @@ func (cui *ConfigUI) LoadConfig(confstr string) error {
cui.HideConfig = tmpConf.HideConfig cui.HideConfig = tmpConf.HideConfig
cui.NoReconfig = tmpConf.NoReconfig cui.NoReconfig = tmpConf.NoReconfig
cui.ResultBellow = tmpConf.ResultBellow cui.ResultBellow = tmpConf.ResultBellow
cui.SHELL = tmpConf.SHELL
cui.CustTmpl = tmpConf.CustTmpl
if cui.CustTmpl != "" {
ntmpl, err := cui.tmpl.OverrideGlob(filepath.Join(cui.CustTmpl, "*.tmpl"))
if err != nil {
cui.log.Error(err)
} else {
cui.tmpl = ntmpl
}
}
cui.Actions = tmpConf.Actions cui.Actions = tmpConf.Actions
for i := range cui.Actions { for i := range cui.Actions {

13
errors.go Normal file
View File

@ -0,0 +1,13 @@
package configui
import (
"net/http"
"kumoly.io/lib/ksrv"
)
var ErrorServerReloading = ksrv.Error{
Code: http.StatusServiceUnavailable,
ID: "ErrorServerReloading",
Message: "server is reloading",
}

View File

@ -17,7 +17,7 @@ type File struct {
Cmd string `json:"cmd"` Cmd string `json:"cmd"`
// RO is readonly // RO is readonly
RO bool `json:"ro"` RO bool `json:"ro"`
Lang string `json:"lang"` Lang string `json:"lang,omitempty"`
// Order order of the display on ui // Order order of the display on ui
Order int `json:"order"` Order int `json:"order"`
@ -58,7 +58,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(CmdTimeout time.Duration) (string, error) { func (f *File) Do(CmdTimeout time.Duration, report chan int) (string, error) {
if f.Cmd == "" { if f.Cmd == "" {
return "", nil return "", nil
} }
@ -80,9 +80,9 @@ func (f *File) Do(CmdTimeout time.Duration) (string, error) {
// prepare cmd // prepare cmd
cmd := &exec.Cmd{} cmd := &exec.Cmd{}
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
cmd = exec.Command(WIN_SHELL, "/c", f.Cmd) cmd = exec.Command(f.owner.SHELL, "/c", f.Cmd)
} else { } else {
cmd = exec.Command(UNIX_SHELL, "-c", f.Cmd) cmd = exec.Command(f.owner.SHELL, "-c", f.Cmd)
} }
f.owner.log.Info("DO: ", f.Cmd) f.owner.log.Info("DO: ", f.Cmd)
done := make(chan string, 1) done := make(chan string, 1)
@ -96,6 +96,7 @@ func (f *File) Do(CmdTimeout time.Duration) (string, error) {
} }
go func() { go func() {
f.pid = cmd.Process.Pid f.pid = cmd.Process.Pid
report <- cmd.Process.Pid
cmd.Wait() cmd.Wait()
done <- b.String() done <- b.String()
}() }()

6
go.mod
View File

@ -4,10 +4,14 @@ go 1.17
require ( require (
kumoly.io/lib/klog v0.0.8 kumoly.io/lib/klog v0.0.8
kumoly.io/lib/ksrv v0.0.1 kumoly.io/lib/ksrv v0.0.2-0.20211112060911-0d61b343a298
kumoly.io/lib/stat v0.0.1
kumoly.io/tools/kconfig v0.1.3
) )
require ( require (
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rs/zerolog v1.26.0 // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
kumoly.io/lib/guard v0.1.2-0.20211124052638-9dfd98f9a848 // indirect
) )

40
go.sum
View File

@ -1,10 +1,44 @@
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE=
github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4= golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
kumoly.io/lib/klog v0.0.4/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
kumoly.io/lib/guard v0.1.2-0.20211124052638-9dfd98f9a848 h1:ALCeJga3775AJDkk7YbMQEcYxpwHtinUN+YjkpacNhA=
kumoly.io/lib/guard v0.1.2-0.20211124052638-9dfd98f9a848/go.mod h1:yWg9RDSI6YXkOPmP6Ad93aMqzlxhgW8LOe/ZRjjYX3U=
kumoly.io/lib/klog v0.0.8 h1:6hTfDlZh7KGnPrd2tUrauCKRImSnyyN9DHXpey3Czn8= kumoly.io/lib/klog v0.0.8 h1:6hTfDlZh7KGnPrd2tUrauCKRImSnyyN9DHXpey3Czn8=
kumoly.io/lib/klog v0.0.8/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= kumoly.io/lib/klog v0.0.8/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M=
kumoly.io/lib/ksrv v0.0.1 h1:JfWwJ9GeiTtDfGoeG7YxJwsckralbhsLKEPLQb20Uzo= kumoly.io/lib/ksrv v0.0.2-0.20211112060911-0d61b343a298 h1:0raqoIXmNpD6s1SrJbieAyIIkDyhe+aqfaXvx8wenrI=
kumoly.io/lib/ksrv v0.0.1/go.mod h1:ykHXeAPjNvA5jEZo5rp32edzkugLf0e+2pspct3FOFQ= kumoly.io/lib/ksrv v0.0.2-0.20211112060911-0d61b343a298/go.mod h1:pwd+NspxnoxPJAETRY2V4i2qZc+orKLxvWzGUBiqBW8=
kumoly.io/lib/stat v0.0.1 h1:Ck596El7Ixk7GZyzQq/86F1YCl7iYffHmzEdFx1sSRM=
kumoly.io/lib/stat v0.0.1/go.mod h1:zqMV9q4TC94VGbpDn/mGBTwRNWBVWlVg1taLlCBAWc8=
kumoly.io/tools/kconfig v0.1.3 h1:okLWqlvASZzfAj3kXzpB/Q8V7WZNd24Y6MLPhWPTwP8=
kumoly.io/tools/kconfig v0.1.3/go.mod h1:j6GYUb50c0pFMxfQgoUl5n4P++AapoG+L5YHxHyyjN4=

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"time"
"kumoly.io/lib/ksrv" "kumoly.io/lib/ksrv"
) )
@ -122,7 +121,7 @@ func (cui *ConfigUI) Apply(w http.ResponseWriter, r *http.Request) {
ksrv.Response(w, 404, []byte("file not found")) ksrv.Response(w, 404, []byte("file not found"))
return return
} }
result, err := file.Do(cui.cmdTimeout) result, err := file.Do(cui.cmdTimeout, make(chan int, 1))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -146,11 +145,11 @@ func (cui *ConfigUI) DoAction(w http.ResponseWriter, r *http.Request) {
} }
file := &File{Name: name, Cmd: v.Cmd, owner: cui} file := &File{Name: name, Cmd: v.Cmd, owner: cui}
pid := make(chan int)
go func() { go func() {
<-time.After(time.Millisecond * 10) cui.Actions[i].pid = <-pid
cui.Actions[i].pid = file.pid
}() }()
result, err := file.Do(cui.cmdTimeout) result, err := file.Do(cui.cmdTimeout, pid)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -196,8 +195,8 @@ func (cui *ConfigUI) Download(w http.ResponseWriter, r *http.Request) {
panic(err) panic(err)
} }
w.Header().Set( w.Header().Set(
"Content-Disposition", ` "Content-Disposition",
attachment; filename="`+cui.AppName+`.json"`, `attachment; filename="`+cui.AppName+`.json"`,
) )
w.Write(data) w.Write(data)
return return

5961
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -17,10 +17,6 @@
"dependencies": { "dependencies": {
"@creativebulma/bulma-tooltip": "^1.2.0", "@creativebulma/bulma-tooltip": "^1.2.0",
"bulma": "^0.9.3", "bulma": "^0.9.3",
"prismjs": "^1.25.0",
"sass": "^1.43.2" "sass": "^1.43.2"
},
"devDependencies": {
"parcel": "^2.0.0"
} }
} }

File diff suppressed because one or more lines are too long

148
schema.json Normal file
View File

@ -0,0 +1,148 @@
{
"title": "ConfigUI",
"type": "object",
"id": "configui",
"properties": {
"app_name": {
"type": "string",
"title": "AppName",
"default": "ConfigUI",
"required":true
},
"files":{
"title":"Files",
"type": "array",
"format":"table",
"items":{
"$ref": "#/definitions/file"
}
},
"actions":{
"title":"Actions",
"type": "array",
"format":"table",
"items":{
"$ref": "#/definitions/action"
}
},
"base_url":{
"type": "string",
"title": "BaseUrl",
"default": "/"
},
"config_path":{
"title":"Config Path",
"type": "string",
"discription":"path to config file"
},
"shell":{
"title":"Shell",
"type": "string",
"description":"shell to use when running cmds",
"required":true,
"options":{
"infoText":"run commands are structured as SHELL -c \"CMD\", \nthe default shell is \nunix:`/usr/bin/sh`\nwindows:`C:\\Windows\\System32\\cmd`\ndarwin:`/bin/bash`"
}
},
"allow_ip":{
"type":"string",
"title":"AllowedIP",
"description":"IPs to allow, blank to allow all"
},
"timeout":{
"type": "string",
"title": "Command Timeout",
"default": "10s",
"description": "timeout to wait for command to finish"
},
"log_path": {
"type":"string",
"title":"Log Path",
"description":"empty for stdout"
},
"log_level": {
"type": "integer",
"title": "log level",
"default": 9
},
"cust":{
"title": "Custom Template",
"type": "string",
"description": "path to custom templates"
},
"result_bellow": {
"title":"Result Bellow",
"type": "boolean",
"format": "checkbox",
"description":"show results bellow editor",
"default": false
},
"production":{
"type": "boolean",
"format": "checkbox",
"title": "Production Mode",
"default": true
},
"no_reconfig":{
"title":"NoReconfig",
"type": "boolean",
"format": "checkbox",
"description":"disable config at runtime",
"default": false
},
"hide_config": {
"title":"Hide Config",
"type": "boolean",
"format": "checkbox",
"description":"hide config panel",
"default": false
}
},
"definitions": {
"file": {
"type": "object",
"properties":{
"name": {
"title": "Name",
"type": "string"
},
"path": {
"title": "Path",
"type": "string"
},
"cmd": {
"title": "Command",
"type": "string"
},
"ro": {
"title": "Read Only",
"format":"checkbox",
"type": "boolean"
},
"lang": {
"title": "Lang",
"type": "string",
"description":"set editor language mode"
},
"order": {
"type": "integer",
"default":0
}
}
},
"action":{
"type": "object",
"properties":{
"name": {
"title": "Name",
"type": "string"
},
"cmd": {
"title": "Command",
"type": "string"
}
}
}
}
}

View File

@ -2,9 +2,11 @@ package configui
import ( import (
"net/http" "net/http"
"strings" "path/filepath"
"kumoly.io/lib/ksrv" "kumoly.io/lib/ksrv"
"kumoly.io/lib/stat"
"kumoly.io/tools/kconfig"
) )
func (cui *ConfigUI) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (cui *ConfigUI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -16,10 +18,19 @@ func (cui *ConfigUI) middleware(next http.Handler) http.Handler {
cui.ksrv_log = k.GetLogger() cui.ksrv_log = k.GetLogger()
cui.setLog() cui.setLog()
k.SetNoLogCondition(func(r *http.Request) bool { k.SetNoLogCondition(func(r *http.Request) bool {
if strings.HasPrefix(r.URL.Path, "/public") || r.URL.Query().Get("f") == "true" { ext := filepath.Ext(r.URL.Path)
switch ext {
case ".js":
return true return true
case ".css":
return true
case ".ttf":
return true
case ".ico":
return true
} }
return false return r.URL.Query().Get("f") == "true"
}) })
return k.Middleware( return k.Middleware(
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
@ -38,6 +49,13 @@ func (cui *ConfigUI) middleware(next http.Handler) http.Handler {
func (cui *ConfigUI) mux() *http.ServeMux { func (cui *ConfigUI) mux() *http.ServeMux {
cuiR := http.NewServeMux() cuiR := http.NewServeMux()
cuiR.HandleFunc("/api/profile", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
ksrv.JSON(w, stat.GetProfile())
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/conf", func(w http.ResponseWriter, r *http.Request) { cuiR.HandleFunc("/api/conf", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
cui.GetConfig(w, r) cui.GetConfig(w, r)
@ -98,6 +116,26 @@ func (cui *ConfigUI) mux() *http.ServeMux {
w.WriteHeader(404) w.WriteHeader(404)
} }
}) })
k := kconfig.New()
k.Schema = SCHEMA
k.Load = func() []byte {
b, err := cui.Config()
if err != nil {
cui.log.Error(err)
return []byte{}
}
return b
}
k.Apply = func(b []byte) error {
err := cui.LoadConfig(string(b))
if err == nil {
k.AppName = cui.AppName
}
return err
}
cuiR.Handle("/kconfig/", http.StripPrefix("/kconfig", k))
cuiR.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.FS(cui.PublicFS)))) cuiR.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.FS(cui.PublicFS))))
cuiR.HandleFunc("/", cui.App) cuiR.HandleFunc("/", cui.App)
return cuiR return cuiR

View File

@ -22,6 +22,13 @@ $footer-padding: 0.5rem 1.5rem 0.5rem;
padding: 0.5rem; padding: 0.5rem;
} }
#kconfigFrame{
border: none;
width: 100%;
height: 100%;
display: block;
}
.icn-spinner { .icn-spinner {
animation: spin-animation 1s infinite; animation: spin-animation 1s infinite;
display: inline-block; display: inline-block;
@ -40,3 +47,9 @@ $footer-padding: 0.5rem 1.5rem 0.5rem;
transform: rotate(359deg); transform: rotate(359deg);
} }
} }
.ace_selection {
// background: #999900 !important;
background: #7f7f00 !important;
}

View File

@ -123,7 +123,7 @@ function setResult(){
result_editor.session.setMode("ace/mode/sh"); result_editor.session.setMode("ace/mode/sh");
result_editor.setReadOnly(true); result_editor.setReadOnly(true);
result_editor.setOptions({ result_editor.setOptions({
maxLines: 1000 maxLines: Infinity
}); });
{{if not .ResultBellow}} {{if not .ResultBellow}}
document.addEventListener('keydown', function (e) { document.addEventListener('keydown', function (e) {

View File

@ -0,0 +1,42 @@
{{define "components/kconfig"}}
<div class="modal" id="kconfig">
<div class="modal-background"></div>
<div class="modal-card" style="max-width:960px">
<header class="modal-card-head">
<p class="modal-card-title">{{.AppName}} Config</p>
<button class="delete" aria-label="close" onclick="KconfigViewTog()"></button>
</header>
<section class="modal-card-body" style="height:80vh;">
<iframe src="{{.BaseUrl}}kconfig/" id="kconfigFrame"></iframe>
</section>
<footer class="modal-card-foot">
<div class="level">
<div class="level-left"></div>
<div class="level-left">
<button class="button" onclick="KconfigViewTog()">OK</button>
</div>
</div>
</footer>
</div>
</div>
<script>
document.addEventListener('keydown', function (e) {
if (e.key === "Escape"){
let el = document.getElementById('kconfig')
el.classList.remove('is-active')
}
})
function KSubmit(){
window.location.reload();
window.ContentChanged = false
}
function KconfigViewTog(){
let el = document.getElementById('kconfig')
el.classList.toggle('is-active')
}
</script>
{{end}}

View File

@ -1,17 +1,28 @@
{{define "home"}} {{define "home"}}
{{template "base/header" .}} {{template "base/header" .}}
{{template "components/error" .}} {{template "components/error" .}}
{{template "components/kconfig" .}}
<script> <script>
var Active = "{{.File.Name}}"; var Active = "{{.File.Name}}";
</script> </script>
<section class="hero is-small is-primary"> <nav class="level py-1">
<div class="hero-body" id="title"> <div class="level-left">
<p class="title"> <div class="level-item">
{{.AppName}} <a class="title is-size-4 ml-2" href="{{.BaseUrl}}">
</p> {{.AppName}}
</a>
</div>
</div> </div>
</section> <div class="level-right">
{{ block "links" .}}
<a class="button is-white level-item"
href="https://kumoly.io/tools/configui/issues"
>Report Bug</a>
{{end}}
<p class="level-item mr-2"></p>
</div>
</nav>
<div class="columns"> <div class="columns">
<div class="column is-one-quarter"> <div class="column is-one-quarter">
@ -46,6 +57,7 @@ var Active = "{{.File.Name}}";
{{end}} {{end}}
<div class="box has-text-centered"> <div class="box has-text-centered">
<a href="{{.BaseUrl}}api/export" class="button is-small">Export Files</a> <a href="{{.BaseUrl}}api/export" class="button is-small">Export Files</a>
{{if not .HideConfig}}<a onclick="KconfigViewTog()" class="button is-small">Config</a>{{end}}
</div> </div>
</div> </div>
<div class="column"> <div class="column">

25
util.go
View File

@ -5,7 +5,10 @@ import (
"compress/gzip" "compress/gzip"
"io" "io"
"os" "os"
"path/filepath"
"reflect"
"strings" "strings"
"sync"
) )
func bundle(buf io.Writer, paths []string, rootDir string, flat bool) error { func bundle(buf io.Writer, paths []string, rootDir string, flat bool) error {
@ -65,3 +68,25 @@ func bundle(buf io.Writer, paths []string, rootDir string, flat bool) error {
return nil return nil
} }
func mkdir(args ...interface{}) error {
var path string
var mode os.FileMode
mode = 0755
for _, arg := range args {
switch arg := arg.(type) {
case string:
path = filepath.Join(path, arg)
case os.FileMode:
mode = arg
}
}
return os.MkdirAll(path, mode)
}
const mutexLocked = 1
func MutexLocked(m *sync.Mutex) bool {
state := reflect.ValueOf(m).Elem().FieldByName("state")
return state.Int()&mutexLocked == mutexLocked
}