Move site files to separate embed.FS

This commit is contained in:
nxshock 2021-09-24 23:35:55 +05:00
parent 15cd01725b
commit 5d632da771
7 changed files with 217 additions and 207 deletions

View File

@ -13,7 +13,7 @@ import (
func HandleRoot(w http.ResponseWriter, r *http.Request) { func HandleRoot(w http.ResponseWriter, r *http.Request) {
if r.RequestURI != "/" { if r.RequestURI != "/" {
http.Error(w, "", http.StatusNotFound) http.FileServer(http.FS(stripSiteFS)).ServeHTTP(w, r)
return return
} }

200
index.htm
View File

@ -1,200 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>File Storage</title>
<link rel="icon" href="data:image/svg+xml;utf8,<svg xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot; stroke=&quot;none&quot; viewBox=&quot;0 0 30 30&quot;><path d=&quot;M 5,30 10,20 H 0 l 5,10&quot; fill=&quot;#cc6666&quot;/><path d=&quot;M 5,10 0,20 H 10 L 5,10&quot; fill=&quot;#de935f&quot;/><path d=&quot;M 15,20 20,10 H 10 l 5,10&quot; fill=&quot;#f0c674&quot;/><path d=&quot;M 15,0 10,10 H 20 L 15,0&quot; fill=&quot;#b5bd68&quot;/><path d=&quot;M 25,30 30,20 H 20 l 5,10&quot; fill=&quot;#8abeb7&quot;/><path d=&quot;M 25,10 20,20 H 30 L 25,10&quot; fill=&quot;#81a2be&quot;/></svg>">
<style>
* {
font-family: Verdana;
font-size: 16px;
color: #abb2bf;
margin: .5em;
padding: 0;
}
html {
margin: 0;
padding: 0;
height: 100%
}
body {
background-color: #282c34;
display: flex;
justify-content: space-between;
flex-wrap: nowrap;
flex-direction: column;
height: 100%;
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #61afef;
margin: 0;
}
svg, img {
vertical-align: middle;
width: 1em;
height: 1em;
margin: 0;
padding: 0;
}
header, footer {
flex-grow: 0;
background-color: #353b45;
padding: .5em;
margin: 0;
display: flex;
justify-content: space-between;
align-items: center;
}
main {
flex-grow: 1;
}
header {
border-bottom: 1px solid #3e4451;
}
footer {
border-top: 1px solid #3e4451;
font-size: 75%;
}
h1 {
margin-top: 1em;
font-size: 150%;
}
table {
border-collapse: collapse;
width: 100%;
margin: 0;
}
tr:hover {
background-color: #353b45;
}
th, td {
border: 1px solid #3e4451;
padding: 0.5em;
}
td:nth-child(2) {
width: 5em;
text-align: right;
}
td:nth-child(3) {
width: 6em;
text-align: right;
}
th {
background-color: #353b45;
}
form {
width: calc(100% - 1em);
}
form > input {
width: calc(100% - 2em);
padding: .5em;
}
button {
padding: .5em;
margin: 0;
}
pre {
margin: 0;
font-family: monospace;
}
input[type="file"] {
display: none;
}
label {
border-radius: 0.25em;
background-color: #61afef;
margin: 0;
padding: .5em;
cursor: pointer;
color: #fff;
text-shadow: 1px 1px 1px #000;
}
</style>
</head>
<body>
<header>
<span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 15" fill="#61afef" stroke="#353b45"><circle cx="7.5" cy="7.5" r="5" fill="none" stroke="#61afef"/><circle cx="3" cy="10" r="2"/><circle cx="12" cy="10" r="2"/><circle cx="7.5" cy="2.5" r="2"/></svg>
File Storage
</span>
<label>
<input id="file-uploader" type="file" id="upload-button">
Загрузить файл
</label>
</header>
<main>
<table>
<col width="*">
<col width="0">
<col width="0">
<tr>
<th>Имя файла</th>
<th>Размер</th>
<th>Дата</th>
</tr>
{{range .Files}} <tr>
<td><img src="/icon?ext={{.Ext}}"> <a href="/download?filename={{.Name}}">{{.Name}}</a></td>
<td><pre>{{.Size}}</pre></td>
<td>{{.Date}}</td>
</tr>
{{end}} </table>
</main>
<footer>
Файлы хранятся как минимум {{.StorageDuration}} ч.
</footer>
</body>
</html>
<script type="text/javascript">
function myProgressHandler(event) {
var p = Math.floor(event.loaded/event.total*100);
document.querySelector("label").innerHTML = 'Загрузка: ' + p + '%...';
}
function myOnLoadHandler(event) {
const response = event.currentTarget;
if (response.status != 200) {
alert('Ошибка при загрузке файла:\n' + response.responseText);
}
document.querySelector("label").innerHTML = 'Загрузка завершена.';
location.reload();
}
document.getElementById("file-uploader").addEventListener('change', (e) => {
var file = document.getElementById("file-uploader").files[0];
var formData = new FormData;
formData.append('file', file);
var ajax = new XMLHttpRequest;
ajax.upload.addEventListener("progress", myProgressHandler, false);
ajax.addEventListener('load', myOnLoadHandler, false);
ajax.open('POST', '/upload', true);
ajax.send(formData);
});
</script>

15
site.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"embed"
"io/fs"
)
//go:embed site/*
var siteFS embed.FS
var stripSiteFS fs.FS
func init() {
stripSiteFS, _ = fs.Sub(siteFS, "site")
}

1
site/favicon.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" stroke="none" viewBox="0 0 30 30"><path d="M 5,30 10,20 H 0 l 5,10" fill="#cc6666"/><path d="M 5,10 0,20 H 10 L 5,10" fill="#de935f"/><path d="M 15,20 20,10 H 10 l 5,10" fill="#f0c674"/><path d="M 15,0 10,10 H 20 L 15,0" fill="#b5bd68"/><path d="M 25,30 30,20 H 20 l 5,10" fill="#8abeb7"/><path d="M 25,10 20,20 H 30 L 25,10" fill="#81a2be"/></svg>

After

Width:  |  Height:  |  Size: 403 B

71
site/index.htm Normal file
View File

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>File Storage</title>
<link rel="stylesheet" href="/style.css">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
</head>
<body>
<header>
<span>
<img src="/favicon.svg">
File Storage
</span>
<label>
<input id="file-uploader" type="file" id="upload-button">
Загрузить файл
</label>
</header>
<main>
<table>
<col width="*">
<col width="0">
<col width="0">
<tr>
<th>Имя файла</th>
<th>Размер</th>
<th>Дата</th>
</tr>
{{range .Files}} <tr>
<td><img src="/icon?ext={{.Ext}}"> <a href="/download?filename={{.Name}}">{{.Name}}</a></td>
<td><pre>{{.Size}}</pre></td>
<td>{{.Date}}</td>
</tr>
{{end}} </table>
</main>
<footer>
Файлы хранятся как минимум {{.StorageDuration}} ч.
</footer>
</body>
</html>
<script type="text/javascript">
function myProgressHandler(event) {
var p = Math.floor(event.loaded/event.total*100);
document.querySelector("label").innerHTML = 'Загрузка: ' + p + '%...';
}
function myOnLoadHandler(event) {
const response = event.currentTarget;
if (response.status != 200) {
alert('Ошибка при загрузке файла:\n' + response.responseText);
}
document.querySelector("label").innerHTML = 'Загрузка завершена.';
location.reload();
}
document.getElementById("file-uploader").addEventListener('change', (e) => {
var file = document.getElementById("file-uploader").files[0];
var formData = new FormData;
formData.append('file', file);
var ajax = new XMLHttpRequest;
ajax.upload.addEventListener("progress", myProgressHandler, false);
ajax.addEventListener('load', myOnLoadHandler, false);
ajax.open('POST', '/upload', true);
ajax.send(formData);
});
</script>

128
site/style.css Normal file
View File

@ -0,0 +1,128 @@
* {
font-family: Verdana;
font-size: 16px;
color: #abb2bf;
margin: .5em;
padding: 0;
}
html {
margin: 0;
padding: 0;
height: 100%
}
body {
background-color: #282c34;
display: flex;
justify-content: space-between;
flex-wrap: nowrap;
flex-direction: column;
height: 100%;
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #61afef;
margin: 0;
}
svg, img {
vertical-align: middle;
width: 1em;
height: 1em;
margin: 0;
padding: 0;
}
header, footer {
flex-grow: 0;
background-color: #353b45;
padding: .5em;
margin: 0;
display: flex;
justify-content: space-between;
align-items: center;
}
main {
flex-grow: 1;
}
header {
border-bottom: 1px solid #3e4451;
}
footer {
border-top: 1px solid #3e4451;
font-size: 75%;
}
h1 {
margin-top: 1em;
font-size: 150%;
}
table {
border-collapse: collapse;
width: 100%;
margin: 0;
}
tr:hover {
background-color: #353b45;
}
th, td {
border: 1px solid #3e4451;
padding: 0.5em;
}
td:nth-child(2) {
width: 5em;
text-align: right;
}
td:nth-child(3) {
width: 6em;
text-align: right;
}
th {
background-color: #353b45;
}
form {
width: calc(100% - 1em);
}
form > input {
width: calc(100% - 2em);
padding: .5em;
}
button {
padding: .5em;
margin: 0;
}
pre {
margin: 0;
font-family: monospace;
}
input[type="file"] {
display: none;
}
label {
border-radius: 0.25em;
background-color: #61afef;
margin: 0;
padding: .5em;
cursor: pointer;
color: #fff;
text-shadow: 1px 1px 1px #000;
}

View File

@ -1,15 +1,11 @@
package main package main
import ( import (
"embed"
"html/template" "html/template"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
//go:embed index.htm
var templatesFS embed.FS
var templates *template.Template var templates *template.Template
func initTemplates() error { func initTemplates() error {
@ -17,11 +13,10 @@ func initTemplates() error {
defer log.Debugln("Templates initialization finished.") defer log.Debugln("Templates initialization finished.")
var err error var err error
templates, err = template.ParseFS(templatesFS, "*.htm") templates, err = template.ParseFS(siteFS, "site/index.htm")
if err != nil { if err != nil {
return err return err
} }
templatesFS = embed.FS{} // free memory
return nil return nil
} }