Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yingshaoxo committed Oct 28, 2019
1 parent bdd6614 commit 8f72a9d
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ build() {
go get github.com/gin-gonic/gin
go get github.com/gin-gonic/contrib/static
go get github.com/gin-contrib/cors
go get -u github.com/gobuffalo/packr/v2/...
#go get github.com/gobuffalo/packr/v2/...
go get github.com/GeertJohan/go.rice
go get github.com/GeertJohan/go.rice/rice

cd ..
cd src
rice embed-go
cd ..

cd bin
gox -output="{{.OS}}_{{.Arch}}" -os="linux" -os="windows" ../src
cd ..
}
Expand Down
7 changes: 4 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/gobuffalo/packr/v2"

"github.com/GeertJohan/go.rice"

"./files"
)
Expand Down Expand Up @@ -50,8 +51,8 @@ func main() {
router.Use(cors.New(config))

// Serve frontend static files
frontend_box := packr.New("frontend_box", "../client/build")
router.StaticFS("/ui/", frontend_box)
box := rice.MustFindBox("../client/build")
router.StaticFS("/ui/", box.HTTPBox())

// Serve target files
router.Use(static.Serve("/media", static.LocalFile(MEDIA_PATH, true)))
Expand Down
86 changes: 86 additions & 0 deletions src/main.go.packr
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"net/http"
"os"
"os/exec"
"runtime"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/gobuffalo/packr/v2"

"./files"
)

func openBrowser(url string) {
switch runtime.GOOS {
case "linux":
exec.Command("xdg-open", url).Start()
case "windows":
exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
exec.Command("open", url).Start()
default:
fmt.Errorf("unsupported platform")
}
}

func main() {
// get cli args
dir, _ := os.Getwd()
var MEDIA_PATH = dir //"/media"
if len(os.Args) >= 2 {
MEDIA_PATH = os.Args[1]
}

// Get all files
var file_dict, _ = files.Get_all_dir_and_files(MEDIA_PATH)
fmt.Println(file_dict)

// Set the router as the default one shipped with Gin
router := gin.Default()

// Enable cors
//router.Use(cors.Default())
config := cors.DefaultConfig()
config.AllowOrigins = []string{"*"}
router.Use(cors.New(config))

// Serve frontend static files
frontend_box := packr.New("frontend_box", "../client/build")
router.StaticFS("/ui/", frontend_box)

// Serve target files
router.Use(static.Serve("/media", static.LocalFile(MEDIA_PATH, true)))

// Setup route group for the API
api := router.Group("/api/")
{
api.Any("info/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"root_folder": MEDIA_PATH,
})
})

api.Any("update/", func(c *gin.Context) {
file_dict, _ = files.Get_all_dir_and_files(MEDIA_PATH)
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
})

api.Any("files/", func(c *gin.Context) {
//whatever := c.Query("name")
c.JSON(http.StatusOK, file_dict)
})
}

// Open link
openBrowser("http://127.0.0.1:5000/ui")

// Start and run the server
router.Run(":5000")
}
230 changes: 230 additions & 0 deletions src/rice-box.go

Large diffs are not rendered by default.

0 comments on commit 8f72a9d

Please sign in to comment.