refactor and update

This commit is contained in:
YouROK
2020-11-11 17:01:14 +03:00
parent b4a20760cc
commit 0d9f68c3c8
11 changed files with 258 additions and 13 deletions

View File

@@ -2,7 +2,6 @@ package api
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"server/torr"
"server/version" "server/version"
) )
@@ -10,13 +9,7 @@ type requestI struct {
Action string `json:"action,omitempty"` Action string `json:"action,omitempty"`
} }
type responseI struct { func SetupRoute(route *gin.Engine) {
}
var bts *torr.BTServer
func SetupRouteApi(route *gin.Engine, serv *torr.BTServer) {
bts = serv
route.GET("/echo", echo) route.GET("/echo", echo)
route.POST("/settings", settings) route.POST("/settings", settings)

View File

@@ -5,13 +5,13 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
settings2 "server/settings" sets "server/settings"
) )
//Action: get, set //Action: get, set
type setsReqJS struct { type setsReqJS struct {
requestI requestI
Sets *settings2.BTSets `json:"sets,omitempty"` Sets *sets.BTSets `json:"sets,omitempty"`
} }
func settings(c *gin.Context) { func settings(c *gin.Context) {
@@ -23,11 +23,11 @@ func settings(c *gin.Context) {
} }
if req.Action == "get" { if req.Action == "get" {
c.JSON(200, settings2.BTsets) c.JSON(200, sets.BTsets)
return return
} }
if req.Action == "set" { if req.Action == "set" {
settings2.SetBTSets(req.Sets) sets.SetBTSets(req.Sets)
c.Status(200) c.Status(200)
return return
} }

View File

@@ -0,0 +1,9 @@
package pages
import (
"github.com/gin-gonic/gin"
)
func mainPage(c *gin.Context) {
c.HTML(200, "mainPage", nil)
}

View File

@@ -0,0 +1,15 @@
package pages
import (
"github.com/gin-gonic/gin"
"server/web/pages/template"
)
var temp *template.Template
func SetupRoute(route *gin.Engine) {
temp = template.InitTemplate(route)
route.GET("/", mainPage)
}

View File

@@ -0,0 +1,49 @@
package template
const apijs = `
// Torrents
function addTorrent(link, title, poster, save, done, fail){
torrent("add",link,null,title,poster,save,done,fail);
}
function getTorrent(hash, done, fail){
torrent("get",null,hash,null,null,null,done,fail);
}
function remTorrent(hash, done, fail){
torrent("rem",null,hash,null,null,null,done,fail);
}
function listTorrent(done, fail){
torrent("list",null,null,null,null,null,done,fail);
}
function dropTorrent(hash, done, fail){
torrent("drop",null,hash,null,null,null,done,fail);
}
function torrent(action, link, hash, title, poster, save, done, fail){
var req = JSON.stringify({ action:action, link: link, title: title, poster: poster, save_to_db: save});
$.post('/torrents',req)
.done(function( data ) {
if (done)
done(data);
})
.fail(function( data ) {
if (fail)
fail(data);
});
}
//
// Settings
//
function humanizeSize(size) {
if (typeof size == 'undefined' || size == 0)
return "";
var i = Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}
`

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,24 @@
package template
import (
"server/version"
)
const header = `
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="` + faviconB64 + `" rel="icon" type="image/x-icon">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.5.4/umd/popper.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js" integrity="sha384-LtrjvnR4Twt/qOuYxE721u19sVFLVSA4hf/rRt6PrZTmiPltdZcI7q7PXQBYTKyf" crossorigin="anonymous"></script>
<title>TorrServer ` + version.Version + `</title>
</head>
`

View File

@@ -0,0 +1,66 @@
package template
import (
"server/version"
)
func (t *Template) parseMainPage() {
t.parsePage("mainPage", mainPage)
}
const mainPage = `
<!DOCTYPE html>
<html lang="ru">
` + header + `
<body>
<script src="/main.js"></script>
<style>
.wrap {
white-space: normal;
word-wrap: break-word;
word-break: break-all;
}
.content {
margin: 1%;
}
</style>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<span class="navbar-brand mx-auto">
TorrServer ` + version.Version + `
</span>
</nav>
<div class="content">
<div>
<label for="magnet">Добавить торрент: </label>
<input id="magnet" class="w-100" autocomplete="off">
</div>
<div class="btn-group d-flex" role="group">
<button id="buttonAdd" class="btn w-100" onclick="addTorr()"><i class="fas fa-plus"></i> Добавить</button>
<button id="buttonUpload" class="btn w-100"><i class="fas fa-file-upload"></i> Загрузить файл</button>
</div>
<br>
<div>
<a href="/torrent/playlist.m3u" rel="external" class="btn btn-primary w-100" role="button" ><i class="fas fa-th-list"></i> Плейлист всех торрентов</a>
</div>
<br>
<h3>Торренты: </h3>
<div id="torrents"></div>
<br>
<div class="btn-group-vertical d-flex" role="group">
<a href="/settings" rel="external" class="btn btn-primary w-100" role="button"><i class="fas fa-cog"></i> Настройки</a>
<a href="/cache" rel="external" class="btn btn-primary w-100" role="button"><i class="fas fa-info"></i> Кэш</a>
<button id="buttonShutdown" class="btn btn-primary w-100" onclick="shutdown()"><i class="fas fa-power-off"></i> Закрыть сервер</button>
</div>
<form id="uploadForm" style="display:none" action="/torrent/upload" method="post">
<input type="file" id="filesUpload" style="display:none" multiple onchange="uploadTorrent()" name="files"/>
</form>
</div>
<footer class="page-footer navbar-dark bg-dark">
<span class="navbar-brand d-flex justify-content-center">
<a rel="external" style="text-decoration: none;" href="/about">Описание</a>
</span>
</footer>
</body>
</html>
`

View File

@@ -0,0 +1,20 @@
package template
const mainjs = `
$(document).ready(function() {
watchInfo();
});
var lastTorrHtml = '';
function watchInfo(){
setInterval(function() {
}, 1000);
}
function loadTorrentInfoHtml(){
}
`

View File

@@ -0,0 +1,64 @@
package template
import (
"html/template"
"github.com/gin-gonic/gin"
)
var ctx *gin.Engine
//
// func InitTemplate(route *gin.Engine) {
// ctx = route
//
// tmpl := getTemplate("favicon", faviconB64)
// tmpl = getTemplate("header", header)
//
// route.SetHTMLTemplate(tmpl)
// }
//
// func getTemplate(name, body string) *template.Template {
// tmpl, err := template.New(name).ParseFiles(body)
// if err != nil {
// log.TLogln("error parse template", err)
// }
// return tmpl
// }
type Template struct {
templates *template.Template
}
func InitTemplate(c *gin.Engine) *Template {
temp := new(Template)
temp.parseMainPage()
// temp.parseSettingsPage()
// temp.parseAboutPage()
// temp.parseCachePage()
c.SetHTMLTemplate(temp.templates)
return temp
}
func (t *Template) render(c *gin.Context, code int, name string, data interface{}) {
c.HTML(code, name, data)
}
func (t *Template) parsePage(name, page string) error {
s := page
var tmpl *template.Template
if t.templates == nil {
t.templates = template.New(name)
}
if name == t.templates.Name() {
tmpl = t.templates
} else {
tmpl = t.templates.New(name)
}
_, err := tmpl.Parse(s)
if err != nil {
return err
}
return nil
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"server/torr" "server/torr"
"server/web/api" "server/web/api"
"server/web/pages"
) )
var ( var (
@@ -18,7 +19,8 @@ func Start(port string) {
route := gin.New() route := gin.New()
route.Use(gin.Logger(), gin.Recovery()) route.Use(gin.Logger(), gin.Recovery())
api.SetupRouteApi(route, BTS) api.SetupRoute(route)
pages.SetupRoute(route)
waitChan <- route.Run(":" + port) waitChan <- route.Run(":" + port)
} }