Compare commits

..

1 Commits

Author SHA1 Message Date
Evan Chen 06b6b6ed9b tmp 2021-11-12 23:09:32 +08:00
9 changed files with 5983 additions and 92 deletions

View File

@ -8,7 +8,6 @@ import (
"html/template" "html/template"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -18,9 +17,8 @@ import (
"kumoly.io/tools/configui/public" "kumoly.io/tools/configui/public"
) )
const UNIX_SHELL = "/usr/bin/sh" var UNIX_SHELL = "/usr/bin/sh"
const WIN_SHELL = "C:\\Windows\\System32\\cmd" var WIN_SHELL = "C:\\Windows\\System32\\cmd"
const DARWIN_SHELL = "/bin/bash"
const version = "v0.1.12" const version = "v0.1.12"
@ -57,7 +55,6 @@ 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"`
@ -77,8 +74,8 @@ type ConfigUI struct {
LogLevel klog.Llevel `json:"log_level"` LogLevel klog.Llevel `json:"log_level"`
// Running commands // Running commands
Onitachi []*Oni `json:"onitachi"` Onitachi []*Oni `json:"-"`
oniIndex map[string]int `json:"-"` OniIndex map[string]int
TmplFS embed.FS `json:"-"` TmplFS embed.FS `json:"-"`
tmpl *engine.Engine tmpl *engine.Engine
@ -126,18 +123,8 @@ func New() *ConfigUI {
} }
}, },
}).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: "/",
@ -149,7 +136,7 @@ func New() *ConfigUI {
cmdTimeout: time.Second * 10, cmdTimeout: time.Second * 10,
LogLevel: klog.Lerror | klog.Linfo, LogLevel: klog.Lerror | klog.Linfo,
log: klog.Sub("ConfigUI"), log: klog.Sub("ConfigUI"),
oniIndex: make(map[string]int), OniIndex: make(map[string]int),
} }
} }
@ -192,28 +179,6 @@ func (cui *ConfigUI) LoadConfig(confstr string) error {
} }
} }
// construct oni
for _, o := range tmpConf.Onitachi {
if o.Cmd == "" {
continue
}
if o.Name == "" {
o.Name = o.Cmd
}
// update if exist
j, ok := cui.oniIndex[o.Name]
if ok {
cui.Onitachi[j].Cmd = o.Cmd
cui.Onitachi[j].Args = o.Args
cui.Onitachi[j].Envs = o.Envs
cui.Onitachi[j].Dir = o.Dir
cui.Onitachi[j].Policy = o.Policy
} else {
cui.oniIndex[o.Name] = len(cui.Onitachi)
cui.Onitachi = append(cui.Onitachi, o)
}
}
// copy // copy
cui.configLock.Lock() cui.configLock.Lock()
defer cui.configLock.Unlock() defer cui.configLock.Unlock()
@ -226,7 +191,6 @@ 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.Actions = tmpConf.Actions cui.Actions = tmpConf.Actions
for i := range cui.Actions { for i := range cui.Actions {

View File

@ -80,9 +80,9 @@ func (f *File) Do(CmdTimeout time.Duration, report chan int) (string, error) {
// prepare cmd // prepare cmd
cmd := &exec.Cmd{} cmd := &exec.Cmd{}
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
cmd = exec.Command(f.owner.SHELL, "/c", f.Cmd) cmd = exec.Command(WIN_SHELL, "/c", f.Cmd)
} else { } else {
cmd = exec.Command(f.owner.SHELL, "-c", f.Cmd) cmd = exec.Command(UNIX_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)

1
go.mod
View File

@ -10,5 +10,4 @@ require (
require ( require (
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
kumoly.io/lib/stat v0.0.1 // indirect
) )

2
go.sum
View File

@ -10,5 +10,3 @@ kumoly.io/lib/ksrv v0.0.1 h1:JfWwJ9GeiTtDfGoeG7YxJwsckralbhsLKEPLQb20Uzo=
kumoly.io/lib/ksrv v0.0.1/go.mod h1:ykHXeAPjNvA5jEZo5rp32edzkugLf0e+2pspct3FOFQ= kumoly.io/lib/ksrv v0.0.1/go.mod h1:ykHXeAPjNvA5jEZo5rp32edzkugLf0e+2pspct3FOFQ=
kumoly.io/lib/ksrv v0.0.2-0.20211112060911-0d61b343a298 h1:0raqoIXmNpD6s1SrJbieAyIIkDyhe+aqfaXvx8wenrI= kumoly.io/lib/ksrv v0.0.2-0.20211112060911-0d61b343a298 h1:0raqoIXmNpD6s1SrJbieAyIIkDyhe+aqfaXvx8wenrI=
kumoly.io/lib/ksrv v0.0.2-0.20211112060911-0d61b343a298/go.mod h1:pwd+NspxnoxPJAETRY2V4i2qZc+orKLxvWzGUBiqBW8= 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=

View File

@ -196,8 +196,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
@ -260,23 +260,13 @@ func (cui *ConfigUI) GetConfig(w http.ResponseWriter, r *http.Request) {
w.Write(data) w.Write(data)
} }
func (cui *ConfigUI) GetOni(w http.ResponseWriter, r *http.Request) { func (cui *ConfigUI) GetRunning(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name") ksrv.JSON(w, cui.Onitachi)
if name == "" {
ksrv.JSON(w, cui.Onitachi)
return
}
i, ok := cui.oniIndex[name]
if !ok {
panic(ErrorOniNotFound.New(name))
}
oni := cui.Onitachi[i]
ksrv.JSON(w, oni)
} }
func (cui *ConfigUI) OniStart(w http.ResponseWriter, r *http.Request) { func (cui *ConfigUI) OniStart(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
i, ok := cui.oniIndex[name] i, ok := cui.OniIndex[name]
if !ok { if !ok {
panic(ErrorOniNotFound.New(name)) panic(ErrorOniNotFound.New(name))
} }
@ -287,16 +277,13 @@ func (cui *ConfigUI) OniStart(w http.ResponseWriter, r *http.Request) {
case STARTED: case STARTED:
panic(ErrorOniHasStarted) panic(ErrorOniHasStarted)
} }
err := oni.Start() oni.Start()
if err != nil {
panic(err)
}
w.Write([]byte("ok")) w.Write([]byte("ok"))
} }
func (cui *ConfigUI) OniStop(w http.ResponseWriter, r *http.Request) { func (cui *ConfigUI) OniStop(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
i, ok := cui.oniIndex[name] i, ok := cui.OniIndex[name]
if !ok { if !ok {
panic(ErrorOniNotFound.New(name)) panic(ErrorOniNotFound.New(name))
} }

5961
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,10 @@
"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"
} }
} }

View File

@ -5,7 +5,6 @@ import (
"strings" "strings"
"kumoly.io/lib/ksrv" "kumoly.io/lib/ksrv"
"kumoly.io/lib/stat"
) )
func (cui *ConfigUI) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (cui *ConfigUI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -99,30 +98,9 @@ func (cui *ConfigUI) mux() *http.ServeMux {
w.WriteHeader(404) w.WriteHeader(404)
} }
}) })
cuiR.HandleFunc("/api/profile", func(w http.ResponseWriter, r *http.Request) { cuiR.HandleFunc("/api/running", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
ksrv.JSON(w, stat.GetProfile()) cui.GetRunning(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
cui.GetOni(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/start", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.OniStart(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/stop", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.OniStart(w, r)
} else { } else {
w.WriteHeader(404) w.WriteHeader(404)
} }

View File

@ -14,7 +14,7 @@
</li> </li>
{{ end }} {{ end }}
</ul> </ul>
{{/* {{if .Actions}} <!-- {{if .Actions}}
<p class="menu-label"> <p class="menu-label">
Actions Actions
</p> </p>
@ -31,7 +31,7 @@
</li> </li>
{{- end -}} {{- end -}}
</ul> </ul>
{{end}} */}} {{end}} -->
{{if not .HideConfig}} {{if not .HideConfig}}
<p class="menu-label"> <p class="menu-label">
System System