添加传消息

master
ZGGSONG 3 years ago
parent 9ea273614e
commit 1f1d2d992d

@ -2,7 +2,10 @@ module github.com/zggsong/FileSync
go 1.17 go 1.17
require github.com/gin-gonic/gin v1.7.7 require (
github.com/gin-gonic/gin v1.7.7
github.com/google/uuid v1.3.0
)
require ( require (
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect

@ -16,6 +16,8 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=

@ -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})
}
}

Loading…
Cancel
Save