2020-11-07 08:27:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2020-11-07 08:56:12 +00:00
|
|
|
"fmt"
|
2020-11-07 08:27:58 +00:00
|
|
|
"github.com/pkg/browser"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate go run ./builder
|
|
|
|
//go:embed build/for-build
|
|
|
|
var asset embed.FS
|
2020-11-07 08:56:12 +00:00
|
|
|
var listen = "localhost:8086"
|
2020-11-07 08:27:58 +00:00
|
|
|
|
|
|
|
func main() {
|
2020-11-07 08:56:12 +00:00
|
|
|
if tmp := os.Getenv("UM_LISTEN"); tmp != "" {
|
|
|
|
listen = tmp
|
|
|
|
}
|
2020-11-07 08:27:58 +00:00
|
|
|
pfxFs := WithPrefix(asset, "build/for-build")
|
|
|
|
go func() {
|
2020-11-07 08:56:12 +00:00
|
|
|
err := http.ListenAndServe(listen, http.FileServer(http.FS(pfxFs)))
|
2020-11-07 08:27:58 +00:00
|
|
|
if err != nil {
|
2020-11-07 08:56:12 +00:00
|
|
|
log.Println("启动出错,请检查是否有其他程序占用了端口:" + listen)
|
|
|
|
_, _ = fmt.Scanln()
|
2020-11-07 08:27:58 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2020-11-07 08:56:12 +00:00
|
|
|
log.Printf("使用浏览器打开: %s 即可访问。", listen)
|
|
|
|
err := browser.OpenURL("http://" + listen)
|
2020-11-07 08:27:58 +00:00
|
|
|
if err != nil {
|
2020-11-07 08:56:12 +00:00
|
|
|
log.Println("自动打开浏览器错误,需要手动打开: "+listen, err)
|
2020-11-07 08:27:58 +00:00
|
|
|
}
|
|
|
|
sign := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sign, os.Interrupt, os.Kill)
|
|
|
|
<-sign
|
|
|
|
}
|
|
|
|
|
|
|
|
type WrappedFS struct {
|
|
|
|
inner fs.FS
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f WrappedFS) Open(p string) (fs.File, error) {
|
|
|
|
p = path.Join(f.prefix, p)
|
|
|
|
log.Printf("serving: %s\n", p)
|
|
|
|
return f.inner.Open(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// same thing but for ReadFile, Stat, ReadDir, Glob, and maybe Rename, OpenFile?
|
|
|
|
|
|
|
|
func WithPrefix(f fs.FS, p string) WrappedFS {
|
|
|
|
return WrappedFS{f, p}
|
|
|
|
}
|