Skip to content

Commit

Permalink
Merge pull request #13 from numary/dev/ui
Browse files Browse the repository at this point in the history
Add embedded Numary Control
  • Loading branch information
altitude authored Jul 9, 2021
2 parents 41a56e4 + df72565 commit 71d7559
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ jobs:
${{ runner.os }}-go-
- name: get deps
run: CGO_ENABLED=1 go get
- name: fetch numary control
run: make fetch-control
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pr_open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
${{ runner.os }}-go-
- name: get deps
run: go get
- name: fetch numary control
run: make fetch-control
- name: run tests
run: go test -v -coverpkg=./... -coverprofile=coverage.out ./...
Lint:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
${{ runner.os }}-go-
- name: get deps
run: CGO_ENABLED=1 go get
- name: fetch numary control
run: make fetch-control
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
numary
coverage.out
/dist/
cmd/control/*
!cmd/control/gitkeep
.DS_Store
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ test:
go test -v -coverprofile=coverage.out -coverpkg=./... ./...

bench:
go test -test.bench=. -run=^a ./...
go test -test.bench=. -run=^a ./...

fetch-control:
cd cmd/control && \
curl -OL https://numary-control-releases.s3.eu-west-1.amazonaws.com/numary-control.tar.gz && \
tar -zxvf numary-control.tar.gz && \
rm numary-control.tar.gz
Empty file added cmd/control/gitkeep
Empty file.
43 changes: 26 additions & 17 deletions cmd/ui.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package cmd

import (
"embed"
"fmt"
"log"
"os"
"net/http"
"os/exec"
"path"
"regexp"
"runtime"

"github.com/spf13/cobra"
)

//go:embed control
var uipath embed.FS

func openuri(uri string) {
var err error

Expand All @@ -33,20 +37,25 @@ func openuri(uri string) {
var UICmd = &cobra.Command{
Use: "ui",
Run: func(cmd *cobra.Command, args []string) {
tmp := os.TempDir()
dir := path.Join(tmp, "numary-ui")
os.Mkdir(dir, 0700)
os.Chdir(dir)

os.WriteFile("index.html", []byte(`
<html>
<head></head>
<body>
<h1>coming soon</h1>
</body>
</html>
`), 0644)

openuri(path.Join(dir, "index.html"))
addr := "localhost:3078"

handler := http.FileServer(http.FS(uipath))
fmt.Println(uipath)

http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
isFile := regexp.MustCompile(`\.[a-z]{2,}$`)
path := r.URL.Path
if !isFile.MatchString(path) {
path = "/"
}
r.URL.Path = fmt.Sprintf("/control%s", path)

handler.ServeHTTP(rw, r)
})

openuri(addr)
fmt.Printf("Numary control is live on http://%s\n", addr)

http.ListenAndServe(addr, nil)
},
}

0 comments on commit 71d7559

Please sign in to comment.