重构代码-拆分至各个文件

master
ZGGSONG 3 years ago
parent 1f1d2d992d
commit c09e6f186b

@ -5,6 +5,7 @@ go 1.17
require ( require (
github.com/gin-gonic/gin v1.7.7 github.com/gin-gonic/gin v1.7.7
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
) )
require ( require (

@ -30,6 +30,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=

@ -1,50 +1,16 @@
package main package main
import ( import (
"embed"
"io/fs"
"io/ioutil"
"log"
"net/http"
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"path"
"path/filepath"
"strings"
"github.com/gin-gonic/gin" "github.com/zggsong/FileSync/server"
"github.com/google/uuid"
) )
//go:embed frontend/dist/*
var FS embed.FS
func main() { func main() {
go func() { go func() {
gin.SetMode(gin.ReleaseMode) server.Run()
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" chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
@ -59,34 +25,3 @@ 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})
}
}

@ -0,0 +1,22 @@
package controller
import (
"net"
"net/http"
"github.com/gin-gonic/gin"
)
func AddressesController(c *gin.Context) {
addrs, _ := net.InterfaceAddrs()
var result []string
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
result = append(result, ipnet.IP.String())
}
}
}
c.JSON(http.StatusOK, gin.H{"addresses": result})
}

@ -0,0 +1,39 @@
package controller
import (
"log"
"net/http"
"os"
"path"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func FilesController(c *gin.Context) {
file, err := c.FormFile("raw")
if err != nil {
log.Fatal(err)
}
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+filepath.Ext(file.Filename))
fileErr := c.SaveUploadedFile(file, filepath.Join(dir, fullpath))
if fileErr != nil {
log.Fatal(fileErr)
}
c.JSON(http.StatusOK, gin.H{"url": "/" + fullpath})
}

@ -0,0 +1,21 @@
package controller
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/skip2/go-qrcode"
)
func QrcodesController(c *gin.Context) {
if content := c.Query("content"); content != "" {
png, err := qrcode.Encode(content, qrcode.Medium, 256)
if err != nil {
log.Fatal(err)
}
c.Data(http.StatusOK, "image/png", png)
} else {
c.Status(http.StatusPreconditionRequired)
}
}

@ -0,0 +1,43 @@
package controller
import (
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
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})
}
}

@ -0,0 +1,33 @@
package controller
import (
"log"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
func UploadsController(c *gin.Context) {
if path := c.Param("path"); path != "" {
target := filepath.Join(getUploadsDir(), path)
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+path)
c.Header("Content-Type", "application/octet-stream")
c.File(target)
} else {
c.Status(http.StatusNotFound)
}
}
func getUploadsDir() (uploads string) {
exe, err := os.Executable()
if err != nil {
log.Fatal(err)
}
dir := filepath.Dir(exe)
uploads = filepath.Join(dir, "uploads")
return
}

Before

Width:  |  Height:  |  Size: 448 KiB

After

Width:  |  Height:  |  Size: 448 KiB

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

@ -0,0 +1,45 @@
package server
import (
"embed"
"io/fs"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
controller "github.com/zggsong/FileSync/server/controller"
)
//go:embed frontend/dist/*
var FS embed.FS
func Run() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.POST("/api/v1/texts", controller.TextsController)
router.GET("/api/v1/addresses", controller.AddressesController)
router.GET("/api/v1/qrcodes", controller.QrcodesController)
router.GET("/uploads/:path", controller.UploadsController)
router.POST("/api/v1/files", controller.FilesController)
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")
}
Loading…
Cancel
Save