-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdd6614
commit 8f72a9d
Showing
4 changed files
with
330 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.