You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
1.9 KiB
93 lines
1.9 KiB
package main
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"os/signal"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
//go:embed frontend/dist/*
|
|
var FS embed.FS
|
|
|
|
func main() {
|
|
go func() {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
router := gin.Default()
|
|
router.POST("/api/v1/texts", TextsController)
|
|
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()
|
|
}
|
|
}
|
|
|
|
func TextsController(c *gin.Context) {
|
|
var json struct {
|
|
Raw string `json:"raw"`
|
|
}
|
|
if err := c.ShouldBindJSON(&json); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
} else {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
dir := filepath.Dir(exe)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
filename := uuid.New().String()
|
|
uploads := filepath.Join(dir, "uploads")
|
|
err = os.MkdirAll(uploads, os.ModePerm)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fullpath := path.Join("uploads", filename+".txt")
|
|
err = ioutil.WriteFile(filepath.Join(dir, fullpath), []byte(json.Raw), 0644)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"url": "/" + fullpath})
|
|
}
|
|
|
|
}
|