mirror of
https://github.com/pldubouilh/gossa
synced 2025-12-06 08:22:32 +01:00
small cleanup gossa.go
This commit is contained in:
parent
9de04f2268
commit
7b4987d503
2 changed files with 34 additions and 42 deletions
42
gossa.go
42
gossa.go
|
|
@ -23,6 +23,29 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type rowTemplate struct {
|
||||
Name string
|
||||
Href template.HTML
|
||||
Size string
|
||||
Ext string
|
||||
}
|
||||
|
||||
type pageTemplate struct {
|
||||
Title template.HTML
|
||||
ExtraPath template.HTML
|
||||
Ro bool
|
||||
RowsFiles []rowTemplate
|
||||
RowsFolders []rowTemplate
|
||||
}
|
||||
|
||||
var host = flag.String("h", "127.0.0.1", "host to listen to")
|
||||
var port = flag.String("p", "8001", "port to listen to")
|
||||
var extraPath = flag.String("prefix", "/", "url prefix at which gossa can be reached, e.g. /gossa/ (slashes of importance)")
|
||||
var symlinks = flag.Bool("symlinks", false, "follow symlinks \033[4mWARNING\033[0m: symlinks will by nature allow to escape the defined path (default: false)")
|
||||
var verb = flag.Bool("verb", false, "verbosity")
|
||||
var skipHidden = flag.Bool("k", true, "\nskip hidden files")
|
||||
var ro = flag.Bool("ro", false, "read only mode (no upload, rename, move, etc...)")
|
||||
|
||||
type rpcCall struct {
|
||||
Call string `json:"call"`
|
||||
Args []string `json:"args"`
|
||||
|
|
@ -92,15 +115,18 @@ func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path str
|
|||
}
|
||||
|
||||
href := url.PathEscape(el.Name())
|
||||
name := el.Name()
|
||||
|
||||
if el.IsDir() && strings.HasPrefix(href, "/") {
|
||||
href = strings.Replace(href, "/", "", 1)
|
||||
}
|
||||
|
||||
if el.IsDir() {
|
||||
p.RowsFolders = append(p.RowsFolders, rowTemplate{el.Name() + "/", template.HTML(href), "", "folder"})
|
||||
p.RowsFolders = append(p.RowsFolders, rowTemplate{name + "/", template.HTML(href), "", "folder"})
|
||||
} else {
|
||||
sl := strings.Split(el.Name(), ".")
|
||||
sl := strings.Split(name, ".")
|
||||
ext := strings.ToLower(sl[len(sl)-1])
|
||||
p.RowsFiles = append(p.RowsFiles, rowTemplate{el.Name(), template.HTML(href), humanize(el.Size()), ext})
|
||||
p.RowsFiles = append(p.RowsFiles, rowTemplate{name, template.HTML(href), humanize(el.Size()), ext})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -246,16 +272,6 @@ func main() {
|
|||
check(err)
|
||||
server := &http.Server{Addr: *host + ":" + *port, Handler: handler}
|
||||
|
||||
// clean shutdown - used only for coverage test
|
||||
// go func() {
|
||||
// sigchan := make(chan os.Signal, 1)
|
||||
// signal.Notify(sigchan, os.Interrupt)
|
||||
// <-sigchan
|
||||
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
// defer cancel()
|
||||
// server.Shutdown(ctx)
|
||||
// }()
|
||||
|
||||
if !*ro {
|
||||
http.HandleFunc(*extraPath+"rpc", rpc)
|
||||
http.HandleFunc(*extraPath+"post", upload)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
_ "embed"
|
||||
"encoding/base64"
|
||||
"flag"
|
||||
"html/template"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -18,33 +17,10 @@ var styleCss string
|
|||
var faviconSvg []byte
|
||||
|
||||
//go:embed ui/ui.tmpl
|
||||
var templateStr string
|
||||
var template0 string
|
||||
|
||||
// fill in template
|
||||
var templateCss = strings.Replace(templateStr, "css_will_be_here", styleCss, 1)
|
||||
var templateCssJs = strings.Replace(templateCss, "js_will_be_here", scriptJs, 1)
|
||||
var templateCssJssIcon = strings.Replace(templateCssJs, "favicon_will_be_here", base64.StdEncoding.EncodeToString(faviconSvg), 2)
|
||||
var templateParsed, _ = template.New("").Parse(templateCssJssIcon)
|
||||
|
||||
type rowTemplate struct {
|
||||
Name string
|
||||
Href template.HTML
|
||||
Size string
|
||||
Ext string
|
||||
}
|
||||
|
||||
type pageTemplate struct {
|
||||
Title template.HTML
|
||||
ExtraPath template.HTML
|
||||
Ro bool
|
||||
RowsFiles []rowTemplate
|
||||
RowsFolders []rowTemplate
|
||||
}
|
||||
|
||||
var host = flag.String("h", "127.0.0.1", "host to listen to")
|
||||
var port = flag.String("p", "8001", "port to listen to")
|
||||
var extraPath = flag.String("prefix", "/", "url prefix at which gossa can be reached, e.g. /gossa/ (slashes of importance)")
|
||||
var symlinks = flag.Bool("symlinks", false, "follow symlinks \033[4mWARNING\033[0m: symlinks will by nature allow to escape the defined path (default: false)")
|
||||
var verb = flag.Bool("verb", false, "verbosity")
|
||||
var skipHidden = flag.Bool("k", true, "\nskip hidden files")
|
||||
var ro = flag.Bool("ro", false, "read only mode (no upload, rename, move, etc...)")
|
||||
var template1 = strings.Replace(template0, "css_will_be_here", styleCss, 1)
|
||||
var template2 = strings.Replace(template1, "js_will_be_here", scriptJs, 1)
|
||||
var template3 = strings.Replace(template2, "favicon_will_be_here", base64.StdEncoding.EncodeToString(faviconSvg), 2)
|
||||
var templateParsed, _ = template.New("").Parse(template3)
|
||||
|
|
|
|||
Loading…
Reference in a new issue