From 1f1d2d992db514f6a1b752d09d21c5bb80f496b3 Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Sun, 10 Apr 2022 01:03:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BC=A0=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 ++++- go.sum | 2 ++ main.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 1798019..eae3714 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/zggsong/FileSync 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 ( github.com/gin-contrib/sse v0.1.0 // indirect diff --git a/go.sum b/go.sum index 5ee9be1..0f8d844 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 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/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= diff --git a/main.go b/main.go index eebb115..2413661 100644 --- a/main.go +++ b/main.go @@ -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}) + } + +}