2022-06-27 04:38:09 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dwelling-files/internal/handlers"
|
2022-10-31 01:46:55 +04:00
|
|
|
"dwelling-files/internal/server"
|
2022-06-28 00:39:14 +04:00
|
|
|
"dwelling-files/web"
|
2022-06-27 04:38:09 +04:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2022-08-02 01:12:47 +04:00
|
|
|
var version string
|
|
|
|
|
2022-06-27 04:38:09 +04:00
|
|
|
var listenAddress *string = flag.String("listen", "/var/run/dwelling-files/f.sock", "listen address (ip:port|unix_path)")
|
|
|
|
var directoryPath *string = flag.String("path", "/srv/ftp", "path to file share")
|
2022-06-28 01:07:46 +04:00
|
|
|
var disableFileHandler *bool = flag.Bool("no-file-handling", false, "disable file handling if it is handled by something else (e.g. NGiNX)")
|
2022-06-27 04:38:09 +04:00
|
|
|
var showVersion *bool = flag.Bool("v", false, "show version")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
|
|
|
if *showVersion {
|
2022-08-02 01:12:47 +04:00
|
|
|
fmt.Println("dwelling-files ver.", version, "\nCopyright (c) 2022 Alexander \"Arav\" Andreev <me@arav.top>")
|
2022-06-27 04:38:09 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 01:07:46 +04:00
|
|
|
hand := handlers.New(directoryPath, web.Assets(), *disableFileHandler)
|
2022-06-27 04:38:09 +04:00
|
|
|
srv := server.NewHttpServer()
|
|
|
|
|
2022-06-28 00:39:14 +04:00
|
|
|
srv.GET("/*filepath", hand.Index)
|
2022-06-27 04:38:09 +04:00
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.Start(network, *listenAddress); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doneSignal := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
<-doneSignal
|
|
|
|
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|