master
Evan Chen 2021-11-19 11:58:55 +08:00
commit b2d2f84bc6
15 changed files with 4788 additions and 0 deletions

20
.drone.yml Normal file
View File

@ -0,0 +1,20 @@
kind: pipeline
name: default
steps:
- name: build
image: golang:1.17.2
commands:
- git tag $DRONE_TAG
- bash release.sh cmd/kconfig/main.go
- echo -n "latest,${DRONE_TAG#v}" > .tags
- name: gitea_release
image: plugins/gitea-release
settings:
api_key:
from_secret: gitea_api_key
base_url: https://kumoly.io
files: dist/*
checksum:
- sha256
trigger:
event: tag

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
dist
.parcel-cache

3
assets/default.kfj Normal file
View File

@ -0,0 +1,3 @@
{
"test":"test"
}

67
cmd/kconfig/main.go Normal file
View File

@ -0,0 +1,67 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"kumoly.io/lib/guard"
"kumoly.io/tools/kconfig"
)
var (
flagAddr string
flagAppName string
flagDev bool
flagLogLevel int
flagLogPretty bool
)
func init() {
flag.StringVar(&flagAddr, "addr", ":8000", "address to listen")
flag.StringVar(&flagAppName, "name", "kconfig", "name of the app")
flag.BoolVar(&flagDev, "dev", false, "turn on dev mode")
flag.IntVar(&flagLogLevel, "level", 1, "log level [-1:5] 7 to disable")
flag.BoolVar(&flagLogPretty, "pretty", false, "log message in human readable format (the original log is json)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: kconfig [options]\n")
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.Level(flagLogLevel))
if flagLogPretty {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006/01/02 15:04:05",
})
}
if flagDev {
log.Logger = log.With().Caller().Logger()
}
mux := http.NewServeMux()
// mux.Handle("/k/", http.StripPrefix("/k", kconfig.New()))
mux.Handle("/", kconfig.New())
g := guard.New()
server := &http.Server{
Addr: flagAddr,
Handler: g.Guard(mux),
}
log.Info().Msgf("%s started at %s", flagAppName, flagAddr)
err := server.ListenAndServe()
if err != nil {
log.Error().Err(err)
}
}

7
go.mod Normal file
View File

@ -0,0 +1,7 @@
module kumoly.io/tools/kconfig
go 1.17
require github.com/rs/zerolog v1.26.0
require kumoly.io/lib/guard v0.1.2-0.20211124052638-9dfd98f9a848 // indirect

35
go.sum Normal file
View File

@ -0,0 +1,35 @@
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/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-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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.1 h1:aUcn0qVtX6TqRhp7bWSjIRlbWRWtuK0cjCArILTbAcY=
kumoly.io/lib/guard v0.1.1/go.mod h1:yWg9RDSI6YXkOPmP6Ad93aMqzlxhgW8LOe/ZRjjYX3U=
kumoly.io/lib/guard v0.1.2-0.20211124044332-f03173b8a9ed h1:2Q7idxzRw86gJq/8pkhP2fBfVVbGsF8FVNX/OEfWPE4=
kumoly.io/lib/guard v0.1.2-0.20211124044332-f03173b8a9ed/go.mod h1:yWg9RDSI6YXkOPmP6Ad93aMqzlxhgW8LOe/ZRjjYX3U=
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=

12
index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>{{.AppName}}</title>
</head>
<body>
<script type="module" src="./main.js"></script>
</body>
</html>

59
kconfig.go Normal file
View File

@ -0,0 +1,59 @@
package kconfig
import (
_ "embed"
"net/http"
"strings"
"kumoly.io/lib/guard/engine"
"kumoly.io/tools/kconfig/public"
)
//go:embed public/index.html
var index string
var servePublic = http.FileServer(http.FS(public.FS))
var tmpl *engine.Engine
func init() {
tmpl = engine.Must(engine.New("").Parse(index))
}
type Kconfig struct {
AppName string
KFJstr string
Mux *http.ServeMux
OnSave func(string) error
Get func() string
}
func New() *Kconfig {
k := &Kconfig{}
mux := http.NewServeMux()
mux.HandleFunc("/", k.App)
k.Mux = mux
return k
}
func (k *Kconfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
k.Mux.ServeHTTP(w, r)
}
func (k *Kconfig) App(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
tmpl.Execute(w, struct{ AppName string }{k.AppName})
return
}
file, err := public.FS.Open(strings.TrimPrefix(r.URL.String(), "/"))
if err != nil {
http.NotFound(w, r)
return
}
stat, err := file.Stat()
if err != nil || stat.IsDir() {
http.NotFound(w, r)
return
}
servePublic.ServeHTTP(w, r)
}

3
main.js Normal file
View File

@ -0,0 +1,3 @@
(function() {
console.log("start")
})();

32
make.sh Normal file
View File

@ -0,0 +1,32 @@
VERSION=$(git describe --tags --abbrev=0)
if [ $? -ne 0 ]; then VERSION=$DRONE_TAG; fi
BUILD=$(git rev-parse --short HEAD)
if [ $? -ne 0 ]; then BUILD=${DRONE_COMMIT:0:7}; fi
PROJ=kconfig
DIST=dist
LDFLAGS="-ldflags \"-X main.Version=${VERSION} -X main.Build=${BUILD} -w -s\""
FAILURES=""
PLATFORMS="darwin/amd64 darwin/arm64"
PLATFORMS="$PLATFORMS windows/amd64"
PLATFORMS="$PLATFORMS linux/amd64"
for PLATFORM in $PLATFORMS; do
GOOS=${PLATFORM%/*}
GOARCH=${PLATFORM#*/}
BIN_FILENAME="${PROJ}"
if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi
CMD="GOOS=${GOOS} GOARCH=${GOARCH} go build ${LDFLAGS} -o ${DIST}/${BIN_FILENAME} $@"
echo "${CMD}"
eval $CMD || FAILURES="${FAILURES} ${PLATFORM}"
sh -c "cd ${DIST} && tar -czf ${PROJ}-${VERSION}-${GOOS}-${GOARCH}.tar.gz ${BIN_FILENAME} && rm ${BIN_FILENAME}"
done
if [[ "${FAILURES}" != "" ]]; then
echo ""
echo "${SCRIPT_NAME} failed on: ${FAILURES}"
exit 1
fi

21
package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "kconfig",
"version": "1.0.0",
"repository": "git@kumoly.io:tools/kconfig.git",
"author": "Evan Chen <evanchen@kumoly.io>",
"license": "MIT",
"scripts": {
"build":"rm public/*.js public/*.css; yarn parcel build index.html"
},
"dependencies": {
"@json-editor/json-editor": "^2.6.1",
"parcel": "^2.0.1"
},
"targets":{
"default":{
"distDir":"public",
"publicUrl":"./",
"sourceMap": false
}
}
}

1
public/index.2af1befd.js Normal file
View File

@ -0,0 +1 @@
console.log("start");

1
public/index.html Normal file
View File

@ -0,0 +1 @@
<!DOCTYPE html><html><head><title>{{.AppName}}</title></head><body> <script type="module" src="index.2af1befd.js"></script> </body></html>

6
public/public.go Normal file
View File

@ -0,0 +1,6 @@
package public
import "embed"
//go:embed *
var FS embed.FS

4518
yarn.lock Normal file

File diff suppressed because it is too large Load Diff