|
|
@ -3,14 +3,18 @@ package main
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"embed"
|
|
|
|
"io/fs"
|
|
|
|
"io/fs"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
//go:embed frontend/dist/*
|
|
|
|
//go:embed frontend/dist/*
|
|
|
@ -20,6 +24,7 @@ func main() {
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
router := gin.Default()
|
|
|
|
router := gin.Default()
|
|
|
|
|
|
|
|
router.POST("/api/v1/texts", TextsController)
|
|
|
|
staticFiles, _ := fs.Sub(FS, "frontend/dist")
|
|
|
|
staticFiles, _ := fs.Sub(FS, "frontend/dist")
|
|
|
|
router.StaticFS("/static", http.FS(staticFiles))
|
|
|
|
router.StaticFS("/static", http.FS(staticFiles))
|
|
|
|
router.NoRoute(func(c *gin.Context) {
|
|
|
|
router.NoRoute(func(c *gin.Context) {
|
|
|
@ -54,3 +59,34 @@ func main() {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
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})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|