1
0
Fork 0

Compare commits

...

4 Commits

10 changed files with 131 additions and 79 deletions

View File

@ -7,10 +7,8 @@ import (
"fmt"
"log"
"net/http"
"net/netip"
"os"
"os/signal"
"strings"
"syscall"
"git.arav.su/Arav/httpr"
@ -32,23 +30,6 @@ func main() {
return
}
var network string
if !strings.ContainsRune(*listenAddress, ':') {
network = "unix"
defer os.Remove(*listenAddress)
} else {
ap, err := netip.ParseAddrPort(*listenAddress)
if err != nil {
log.Fatalln(err)
}
if ap.Addr().Is4() {
network = "tcp4"
} else if ap.Addr().Is6() {
network = "tcp6"
}
}
hand := dwhttp.New(directoryPath, !*enableFileHandler)
r := httpr.New()
@ -58,7 +39,7 @@ func main() {
r.Handler(http.MethodGet, "/", hand.Index)
srv := dwhttp.NewHttpServer(r)
if err := srv.Start(network, *listenAddress); err != nil {
if err := srv.Start(*listenAddress); err != nil {
log.Fatalln(err)
}

2
go.mod
View File

@ -2,4 +2,4 @@ module dwelling-files
go 1.16
require git.arav.su/Arav/httpr v0.2.0
require git.arav.su/Arav/httpr v0.3.1

4
go.sum
View File

@ -1,2 +1,2 @@
git.arav.su/Arav/httpr v0.2.0 h1:rtwUVl4ZDfvMf9DeLktxvups5GDY0ARVcUuUHR7Eb9E=
git.arav.su/Arav/httpr v0.2.0/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=
git.arav.su/Arav/httpr v0.3.1 h1:8ba90SJ4XYUWfIlC3V0Zuw3+CcOb9IYVkOZ/2mB9JO0=
git.arav.su/Arav/httpr v0.3.1/go.mod h1:z0SVYwe5dBReeVuFU9QH2PmBxICJwchxqY5OfZbeVzU=

View File

@ -37,10 +37,11 @@ func (h *FilesHandlers) Index(w http.ResponseWriter, r *http.Request) {
entries, stats, err := files.ScanDirectory(h.directoryPath+path, path)
if err != nil {
log.Println(err)
Error(w, http.StatusNotFound, "", "", r.Referer())
return
}
web.Index(utils.MainSite(r.Host), currentPath, &stats, &entries, r, w)
web.Index("Files", utils.MainSite(r.Host), currentPath, &stats, &entries, r, w)
}
func (h *FilesHandlers) File(w http.ResponseWriter, r *http.Request) {
@ -56,3 +57,8 @@ func RobotsTxt(w http.ResponseWriter, r *http.Request) {
fc, _ := web.AssetsGetFile("robots.txt")
w.Write(fc)
}
func Error(w http.ResponseWriter, code int, reason, message, referer string) {
w.WriteHeader(code)
web.ErrorXXX("/ "+http.StatusText(code), reason, message, referer, code, w)
}

View File

@ -5,12 +5,15 @@ import (
"log"
"net"
"net/http"
"net/netip"
"os"
"strings"
"time"
)
type HttpServer struct {
s http.Server
s http.Server
addr net.Addr
}
func NewHttpServer(r http.Handler) *HttpServer {
@ -20,7 +23,23 @@ func NewHttpServer(r http.Handler) *HttpServer {
Handler: r}}
}
func (s *HttpServer) Start(network, address string) error {
func (s *HttpServer) Start(address string) error {
var network string
if !strings.ContainsRune(address, ':') {
network = "unix"
} else {
ap, err := netip.ParseAddrPort(address)
if err != nil {
return err
}
if ap.Addr().Is4() {
network = "tcp4"
} else if ap.Addr().Is6() {
network = "tcp6"
}
}
listener, err := net.Listen(network, address)
if err != nil {
return err
@ -30,6 +49,8 @@ func (s *HttpServer) Start(network, address string) error {
os.Chmod(address, 0777)
}
s.addr = listener.Addr()
go func() {
if err = s.s.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Fatalln(err)
@ -43,6 +64,10 @@ func (s *HttpServer) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if s.addr.Network() == "unix" {
defer os.Remove(s.addr.String())
}
if err := s.s.Shutdown(ctx); err != nil {
return err
}

7
web/assets/css/error.css Normal file
View File

@ -0,0 +1,7 @@
#error {
font-size: 3.5rem;
line-height: 5rem;
text-align: center;
margin: 6rem 0; }
#error h1 { font-size: 8rem; }

25
web/templates/base.jade Normal file
View File

@ -0,0 +1,25 @@
doctype html
html(lang='en')
head
block head
title Arav's dwelling / #{title}
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible' content='IE=edge')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
meta(name='theme-color' content='#cd2682')
meta(name='description' content='My file share.')
link(rel='icon' type='image/svg+xml' href='/assets/img/favicon.svg' sizes='any')
link(href='/assets/css/main.css' rel='stylesheet')
script(src='/assets/js/main.js' defer='')
body
header
svg#logo(viewBox='0 -25 216 40')
text.logo Arav's dwelling
text.under(y='11') Welcome to my sacred place, wanderer
nav
block nav
block content
footer
a(href='/rss.xml' title="Stay up to date on what's going on.") RSS feed
br
| 2017—2023 Arav <#[a(href='mailto:me@arav.su') me@arav.su]> #[a(href='/privacy') Privacy statements]

View File

@ -0,0 +1,24 @@
extends base.jade
block meta_description
meta(name='description' content=http.StatusText(code))
block append head
link(href='/assets/css/error.css' rel='stylesheet')
block nav
a(href='/') Back to index page
h1 #{http.StatusText(code)}
block content
:go:func ErrorXXX(title, reason, message, referer string, code int)
section#error
h1 #{code}
| #{http.StatusText(code)}
if reason != ""
p #{reason}
if message != ""
p #{message}
if referer != ""
section
h2 #[a(href=referer) Go back]

View File

@ -1,56 +1,39 @@
:go:func Index(mainSite, currentPath string, stats *files.DirStats, items *[]files.DirEntry, r *http.Request)
extends base.jade
:go:import "dwelling-files/pkg/files"
:go:import "dwelling-files/pkg/utils"
block nav
a(href=mainSite) Back to main website
h1 Files
doctype html
html(lang='en')
head
title Arav's dwelling / Files
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible' content='IE=edge')
meta(name='viewport' content='width=device-width, initial-scale=1.0')
meta(name='theme-color' content='#cd2682')
meta(name='description' content='My file share.')
link(rel='icon' type='image/svg+xml' href='/assets/img/favicon.svg' sizes='any')
link(href='/assets/css/main.css' rel='stylesheet')
script(src='/assets/js/main.js' defer='')
body
header
svg#logo(viewBox='0 -25 216 40')
text.logo Arav's dwelling
text.under(y='11') Welcome to my sacred place, wanderer
nav
a(href=mainSite) Back to main website
h1 Files
section#files
span!= currentPath
p Files: #{stats.Files} (#{stats.FilesSize}); Directories: #{stats.Directories}.
input.hidden(type="text", name="filter" placeholder="Type in to filter this directory (case insensitive)")
table
thead
tr
th Name
th Date
th Size
tr(tabindex=0)
td #[a(href="../") ../]
tbody
each item, i in *items
tr(tabindex=i+1)
td #[a(href=item.Link) #{item.Name}]
td!= utils.ToClientTimezone(item.Datetime, r).Format(files.FileDateFormat)
td= item.Size
section
span!= currentPath
section#usage
p On a page use up and down arrow keys to navigate through list. Use home and end keys to go to the start and end of a list. Use Ctrl+Backspace to return to a parent directory.
p In an overlay use a mouse wheel to change a scale of a video or a picture. Use left and right arrow keys to go through media. Use space key to toggle pause. Use escape key to close an overlay, or click outside a media. An audio volume is being kept across site using LocalStorage API.
footer
| 2017—2023 Arav <#[a(href='mailto:me@arav.su') me@arav.su]> #[a(href=mainSite+'/privacy') Privacy statements]
div#overlay
button(name='prev') ❰
block content
:go:func Index(title, mainSite, currentPath string, stats *files.DirStats, items *[]files.DirEntry, r *http.Request)
:go:import "dwelling-files/pkg/files"
:go:import "dwelling-files/pkg/utils"
section#files
span!= currentPath
p Files: #{stats.Files} (#{stats.FilesSize}); Directories: #{stats.Directories}.
input.hidden(type="text", name="filter" placeholder="Type in to filter this directory (case insensitive)")
table
thead
tr
th Name
th Date
th Size
tr(tabindex=0)
td #[a(href="../") ../]
tbody
each item, i in *items
tr(tabindex=i+1)
td #[a(href=item.Link) #{item.Name}]
td!= utils.ToClientTimezone(item.Datetime, r).Format(files.FileDateFormat)
td= item.Size
section
span!= currentPath
section#usage
p On a page use up and down arrow keys to navigate through list. Use home and end keys to go to the start and end of a list. Use Ctrl+Backspace to return to a parent directory.
p In an overlay use a mouse wheel to change a scale of a video or a picture. Use left and right arrow keys to go through media. Use space key to toggle pause. Use escape key to close an overlay, or click outside a media. An audio volume is being kept across site using LocalStorage API.
div#overlay
button(name='prev') ❰
div
div
div
span
button(name='next') ❱
span
button(name='next') ❱

View File

@ -7,6 +7,7 @@ import (
)
//go:generate $GOPATH/bin/jade -pkg=web -stdbuf -writer templates/index.jade
//go:generate $GOPATH/bin/jade -pkg=web -stdbuf -writer templates/errorXXX.jade
//go:embed assets
var assetsDir embed.FS