U go embed file

This commit is contained in:
zhenorzz 2021-03-10 15:12:21 +08:00
parent 49b3e1157a
commit 32fb5ad1ba
10 changed files with 34 additions and 37 deletions

View File

@ -1,4 +1,3 @@
statik -f -src=.\web\dist\
::go build linux
SET GOOS=linux
SET GOARCH=amd64

View File

@ -1,6 +1,4 @@
#!/bin/bash
statik -f -src=./web/dist/
GOOS=linux GOARCH=amd64 go build -o goploy main.go
GOOS=darwin GOARCH=amd64 go build -o goploy.mac main.go

View File

@ -4,9 +4,9 @@ import (
"errors"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/rakyll/statik/fs"
"github.com/zhenorzz/goploy/model"
_ "github.com/zhenorzz/goploy/statik"
"github.com/zhenorzz/goploy/web"
"io/fs"
"io/ioutil"
"log"
"mime"
@ -46,12 +46,12 @@ type Router struct {
// Start a router
func (rt *Router) Start() {
if os.Getenv("ENV") == "production" {
statikFS, err := fs.New()
subFS, err := fs.Sub(web.Dist, "dist")
if err != nil {
log.Fatal(err)
}
http.Handle("/static/", http.FileServer(statikFS))
http.Handle("/favicon.ico", http.FileServer(statikFS))
http.Handle("/static/", http.FileServer(http.FS(subFS)))
http.Handle("/favicon.ico", http.FileServer(http.FS(subFS)))
}
http.Handle("/", rt)
}
@ -94,11 +94,7 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// else serve file in npm
if os.Getenv("ENV") == "production" {
if "/" == r.URL.Path {
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
r, err := statikFS.Open("/index.html")
r, err := web.Dist.Open("dist/index.html")
if err != nil {
log.Fatal(err)
}
@ -109,6 +105,7 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
}
gp, response := rt.checkLogin(w, r)
if response != nil {
response.JSON(w)

3
go.mod
View File

@ -1,6 +1,6 @@
module github.com/zhenorzz/goploy
go 1.13
go 1.16
require (
github.com/Masterminds/squirrel v1.4.0
@ -13,7 +13,6 @@ require (
github.com/joho/godotenv v1.3.0
github.com/leodido/go-urn v1.2.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rakyll/statik v0.1.7
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect

4
go.sum
View File

@ -27,8 +27,6 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
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/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
@ -38,14 +36,12 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnk
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=

11
main.go
View File

@ -50,6 +50,17 @@ func usage() {
}
func main() {
//files, err := subFS.Open("./")
//
//if err != nil {
// log.Fatal(err)
//}
//
//for _, f := range files {
// fmt.Println(f.Name())
//}
//return
flag.Parse()
if help {
flag.Usage()

View File

@ -301,6 +301,11 @@ func remoteSync(chInput chan<- syncMessage, userInfo model.User, project model.P
rsyncOption = append(rsyncOption, "--rsync-path=mkdir -p "+destDir+" && rsync")
}
srcPath := core.GetProjectPath(project.ID) + "/"
// rsync path can not contain colon
// windows like C:\
if strings.Contains(srcPath, ":\\") {
srcPath = "/cygdrive/" + strings.Replace(srcPath, ":\\", "/", 1)
}
destPath := remoteMachine + ":" + destDir
rsyncOption = append(rsyncOption, srcPath, destPath)
cmd := exec.Command("rsync", rsyncOption...)
@ -311,13 +316,13 @@ func remoteSync(chInput chan<- syncMessage, userInfo model.User, project model.P
if err := cmd.Run(); err != nil {
core.Log(core.ERROR, "err: "+err.Error()+", detail: "+errbuf.String())
publishTraceModel.Detail = errbuf.String()
publishTraceModel.Detail = "err: " + err.Error() + ", detail: " + errbuf.String()
publishTraceModel.State = model.Fail
publishTraceModel.AddRow()
chInput <- syncMessage{
serverName: projectServer.ServerName,
projectID: project.ID,
detail: errbuf.String(),
detail: "err: " + err.Error() + ", detail: " + errbuf.String(),
state: model.ProjectFail,
}
return

File diff suppressed because one or more lines are too long

View File

@ -56,7 +56,7 @@ func ParseCommandLine(command string) ([]string, error) {
continue
}
if c == '"' || c == '\'' {
if c == '"' {
state = "quotes"
quote = string(c)
continue
@ -90,7 +90,7 @@ func ParseCommandLine(command string) ([]string, error) {
return args, nil
}
func DialSSH(user, password, path, host string, port int) (*ssh.Client, error) {
func DialSSH(user, password, path, host string, port int) (*ssh.Client, error) {
var (
auth []ssh.AuthMethod
addr string

6
web/Web.go Normal file
View File

@ -0,0 +1,6 @@
package web
import "embed"
//go:embed dist
var Dist embed.FS