|
|
|
@ -3,14 +3,18 @@ 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/*
|
|
|
|
@ -20,6 +24,7 @@ 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) {
|
|
|
|
@ -54,3 +59,34 @@ func main() {
|
|
|
|
|
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})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|