Add local web server to serve the app

This commit is contained in:
Ivan Danyliuk
2018-11-15 09:21:21 +01:00
parent 924d249fbc
commit a0a6c49ae4
19 changed files with 4174 additions and 149 deletions
+1
View File
@@ -0,0 +1 @@
txqr-tester
+15
View File
@@ -0,0 +1,15 @@
package main
import (
"github.com/gopherjs/vecty"
)
func main() {
app := NewApp()
vecty.SetTitle("TXQR Automated Tester")
vecty.AddStylesheet("css/bulma.css")
vecty.AddStylesheet("css/bulma-extensions.min.css")
vecty.AddStylesheet("css/custom.css")
vecty.RenderBody(app)
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+20
View File
@@ -0,0 +1,20 @@
package main
import "net"
// GetLocalIP returns the non loopback local IP of the host.
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
+6 -7
View File
@@ -1,15 +1,14 @@
package main
import (
"github.com/gopherjs/vecty"
"flag"
"log"
)
func main() {
app := NewApp()
flag.Parse()
vecty.SetTitle("TXQR Automated Tester")
vecty.AddStylesheet("css/bulma.css")
vecty.AddStylesheet("css/bulma-extensions.min.css")
vecty.AddStylesheet("css/custom.css")
vecty.RenderBody(app)
if err := StartApp(":1999"); err != nil {
log.Fatalf("[ERROR] Can't start web server to serve the app")
}
}
File diff suppressed because one or more lines are too long
+67
View File
@@ -0,0 +1,67 @@
//go:generate go-bindata-assetfs app/index.html app/css/... app/txqr-tester.js
package main
import (
"fmt"
"html/template"
"net/http"
"os"
"os/exec"
"runtime"
)
type PageInfo struct {
Title string
}
// indexTmpl is a html template for index page.
var (
indexTmpl *template.Template
)
func init() {
data, err := Asset("app/index.html")
if err != nil {
panic(err)
}
indexTmpl = template.Must(template.New("index.html").Parse(string(data)))
}
// StartApp generates app page, serves it via http
// and tries to open it using default browser.
func StartApp(bind string) error {
http.Handle("/", http.FileServer(assetFS()))
go StartBrowser("http://localhost" + bind)
return http.ListenAndServe(bind, nil)
}
// handler handles index page.
func handler(w http.ResponseWriter, r *http.Request, info *PageInfo) {
err := indexTmpl.Execute(w, info)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(os.Stderr, "[ERROR] Failed to render template:", err)
return
}
}
// StartBrowser tries to open the URL in a browser
// and reports whether it succeeds.
//
// Orig. code: golang.org/x/tools/cmd/cover/html.go
func StartBrowser(url string) bool {
// try to start the browser
var args []string
switch runtime.GOOS {
case "darwin":
args = []string{"open"}
case "windows":
args = []string{"cmd", "/c", "start"}
default:
args = []string{"xdg-open"}
}
cmd := exec.Command(args[0], append(args[1:], url)...)
fmt.Println("If browser window didn't appear, please go to this url:", url)
return cmd.Start() == nil
}