master
Evan Chen 2021-11-15 16:54:55 +08:00
parent 4b1ad5b791
commit b0d76dff3e
11 changed files with 88 additions and 5985 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt"
"html/template"
"os"
"runtime"
"strings"
"sync"
"time"
@ -16,8 +17,9 @@ import (
"kumoly.io/tools/configui/public"
)
var UNIX_SHELL = "/usr/bin/sh"
var WIN_SHELL = "C:\\Windows\\System32\\cmd"
const UNIX_SHELL = "/usr/bin/sh"
const WIN_SHELL = "C:\\Windows\\System32\\cmd"
const DARWIN_SHELL = "/bin/bash"
const version = "v0.1.12"
@ -36,6 +38,8 @@ var Ext2Mode map[string]string = map[string]string{
type Action struct {
Name string `json:"name"`
Cmd string `json:"cmd"`
Dir string `json:"dir"`
Env []string `json"env,omitempty"`
run chan struct{} `json:"-"`
pid int `json:"-"`
}
@ -52,6 +56,7 @@ type ConfigUI struct {
Prod bool `json:"production"`
BaseUrl string `json:"base_url"`
ConfigPath string `json:"config_path"`
SHELL string `json:"shell"`
NoReconfig bool `json:"no_reconfig"`
AllowIP string `json:"allow_ip"`
@ -92,8 +97,18 @@ func New() *ConfigUI {
return strings.ReplaceAll(name, " ", "-")
},
}).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{
fileIndex: map[string]int{},
SHELL: sh,
Prod: true,
AppName: "ConfigUI",
BaseUrl: "/",

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

@ -58,7 +58,7 @@ func (f *File) Write(data []byte) error {
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 == "" {
return "", nil
}
@ -80,9 +80,9 @@ func (f *File) Do(CmdTimeout time.Duration) (string, error) {
// prepare cmd
cmd := &exec.Cmd{}
if runtime.GOOS == "windows" {
cmd = exec.Command(WIN_SHELL, "/c", f.Cmd)
cmd = exec.Command(f.owner.SHELL, "/c", f.Cmd)
} 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)
done := make(chan string, 1)
@ -96,6 +96,7 @@ func (f *File) Do(CmdTimeout time.Duration) (string, error) {
}
go func() {
f.pid = cmd.Process.Pid
report <- cmd.Process.Pid
cmd.Wait()
done <- b.String()
}()

3
go.mod
View File

@ -4,10 +4,11 @@ go 1.17
require (
kumoly.io/lib/klog v0.0.8
kumoly.io/lib/ksrv v0.0.1
kumoly.io/lib/ksrv v0.0.2-0.20211112060911-0d61b343a298
)
require (
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
kumoly.io/lib/stat v0.0.1 // indirect
)

4
go.sum
View File

@ -8,3 +8,7 @@ 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/ksrv v0.0.1 h1:JfWwJ9GeiTtDfGoeG7YxJwsckralbhsLKEPLQb20Uzo=
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/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

@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"strconv"
"time"
"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"))
return
}
result, err := file.Do(cui.cmdTimeout)
result, err := file.Do(cui.cmdTimeout, make(chan int))
if err != nil {
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}
pid := make(chan int)
go func() {
<-time.After(time.Millisecond * 10)
cui.Actions[i].pid = file.pid
cui.Actions[i].pid = <-pid
}()
result, err := file.Do(cui.cmdTimeout)
result, err := file.Do(cui.cmdTimeout, pid)
if err != nil {
panic(err)
}
@ -196,8 +195,8 @@ func (cui *ConfigUI) Download(w http.ResponseWriter, r *http.Request) {
panic(err)
}
w.Header().Set(
"Content-Disposition", `
attachment; filename="`+cui.AppName+`.json"`,
"Content-Disposition",
`attachment; filename="`+cui.AppName+`.json"`,
)
w.Write(data)
return

5961
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -5,6 +5,7 @@ import (
"strings"
"kumoly.io/lib/ksrv"
"kumoly.io/lib/stat"
)
func (cui *ConfigUI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -38,6 +39,13 @@ func (cui *ConfigUI) middleware(next http.Handler) http.Handler {
func (cui *ConfigUI) mux() *http.ServeMux {
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) {
if r.Method == "GET" {
cui.GetConfig(w, r)

View File

@ -5,13 +5,15 @@
var Active = "{{.File.Name}}";
</script>
<section class="hero is-small is-primary">
<div class="hero-body" id="title">
<p class="title">
<nav class="level">
<div class="level-left">
<div class="level-item">
<a class="title is-size-4 ml-2 mt-1" href="{{.BaseUrl}}">
{{.AppName}}
</p>
</a>
</div>
</section>
</div>
</nav>
<div class="columns">
<div class="column is-one-quarter">

25
util.go
View File

@ -5,7 +5,10 @@ import (
"compress/gzip"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
)
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
}
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
}