mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
add gen web
This commit is contained in:
117
gen_web.go
Normal file
117
gen_web.go
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"mime"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
dir, _ := os.Getwd()
|
||||||
|
os.Chdir("web")
|
||||||
|
if run("yarn") != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if run("yarn", "run", "build") != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
os.Chdir(dir)
|
||||||
|
|
||||||
|
compileHtml := "web/build/"
|
||||||
|
srcGo := "server/web/pages/"
|
||||||
|
|
||||||
|
run("rm", "-rf", srcGo+"template/pages")
|
||||||
|
run("cp", "-r", compileHtml, srcGo+"template/pages")
|
||||||
|
|
||||||
|
files := make([]string, 0)
|
||||||
|
|
||||||
|
filepath.WalkDir(srcGo+"template/pages/", func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if !d.IsDir() {
|
||||||
|
name := strings.TrimPrefix(path, srcGo+"template/")
|
||||||
|
files = append(files, name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
fmap := writeEmbed(srcGo+"template/html.go", files)
|
||||||
|
writeRoute(srcGo+"template/route.go", fmap)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeEmbed(fname string, files []string) map[string]string {
|
||||||
|
ff, err := os.Create(fname)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer ff.Close()
|
||||||
|
embedStr := `package template
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
)
|
||||||
|
`
|
||||||
|
ret := make(map[string]string)
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
fname := cleanName(strings.TrimPrefix(f, "pages"))
|
||||||
|
embedStr += "\n\n//go:embed " + f + "\nvar " + fname + " []byte\n"
|
||||||
|
ret[strings.TrimPrefix(f, "pages")] = fname
|
||||||
|
}
|
||||||
|
|
||||||
|
ff.WriteString(embedStr)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeRoute(fname string, fmap map[string]string) {
|
||||||
|
ff, err := os.Create(fname)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer ff.Close()
|
||||||
|
embedStr := `package template
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RouteWebPages(route *gin.RouterGroup) {
|
||||||
|
route.GET("/", func(c *gin.Context) {
|
||||||
|
c.Data(200, "text/html; charset=utf-8", Indexhtml)
|
||||||
|
})
|
||||||
|
`
|
||||||
|
mime.AddExtensionType(".map", "application/json")
|
||||||
|
mime.AddExtensionType(".webmanifest", "application/manifest+json")
|
||||||
|
for link, v := range fmap {
|
||||||
|
fmime := mime.TypeByExtension(filepath.Ext(link))
|
||||||
|
embedStr += `
|
||||||
|
route.GET("` + link + `", func(c *gin.Context) {
|
||||||
|
c.Data(200, "` + fmime + `", ` + v + `)
|
||||||
|
})
|
||||||
|
|
||||||
|
`
|
||||||
|
}
|
||||||
|
embedStr += "}"
|
||||||
|
|
||||||
|
ff.WriteString(embedStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(name string, args ...string) error {
|
||||||
|
cmd := exec.Command(name, args...)
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanName(fn string) string {
|
||||||
|
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return strings.Title(reg.ReplaceAllString(fn, ""))
|
||||||
|
}
|
||||||
@@ -10,15 +10,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func SetupRoute(route *gin.RouterGroup) {
|
func SetupRoute(route *gin.RouterGroup) {
|
||||||
route.GET("/", mainPage)
|
template.RouteWebPages(route)
|
||||||
route.GET("/stat", statPage)
|
route.GET("/stat", statPage)
|
||||||
route.GET("/magnets", getTorrents)
|
route.GET("/magnets", getTorrents)
|
||||||
}
|
}
|
||||||
|
|
||||||
func mainPage(c *gin.Context) {
|
|
||||||
c.Data(200, "text/html; charset=utf-8", template.IndexHtml)
|
|
||||||
}
|
|
||||||
|
|
||||||
func statPage(c *gin.Context) {
|
func statPage(c *gin.Context) {
|
||||||
torr.WriteStatus(c.Writer)
|
torr.WriteStatus(c.Writer)
|
||||||
c.Status(200)
|
c.Status(200)
|
||||||
|
|||||||
Reference in New Issue
Block a user