From 9ea273614ea4cb07c79f3e21cca74a9c18729db8 Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Sat, 9 Apr 2022 23:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A1=B5=E9=9D=A2=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E8=A7=81=EF=BC=9A=20https://www.yuque.com/u29422/dg5u?= =?UTF-8?q?y8/eei6ie#fF05l?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 106b00d..eebb115 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,12 @@ package main import ( "embed" "io/fs" + "log" "net/http" + "os" + "os/exec" + "os/signal" + "strings" "github.com/gin-gonic/gin" ) @@ -13,12 +18,39 @@ var FS embed.FS func main() { go func() { - gin.SetMode(gin.DebugMode) + gin.SetMode(gin.ReleaseMode) router := gin.Default() - staticFiles, _ := fs.Sub(FS, "frontend/dist") router.StaticFS("/static", http.FS(staticFiles)) - + router.NoRoute(func(c *gin.Context) { + path := c.Request.URL.Path + if strings.HasPrefix(path, "/static/") { + reader, err := staticFiles.Open("index.html") + if err != nil { + log.Fatal(err) + } + defer reader.Close() + stat, err := reader.Stat() + if err != nil { + log.Fatal(err) + } + c.DataFromReader(http.StatusOK, stat.Size(), "text/html", reader, nil) + } else { + c.Status(http.StatusNotFound) + } + }) + router.Run(":27149") }() + chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" + cmd := exec.Command(chromePath, "--app=http://127.0.0.1:27149/static/index.html") + cmd.Start() + + chSingal := make(chan os.Signal, 1) + signal.Notify(chSingal, os.Interrupt) + + select { + case <-chSingal: + cmd.Process.Kill() + } }