1
0
dwelling-files/cmd/dwelling-files/main.go

68 lines
1.5 KiB
Go
Raw Normal View History

2022-06-27 04:38:09 +04:00
package main
import (
"dwelling-files/internal/handlers"
"dwelling-files/internal/server"
"dwelling-files/web"
2022-06-27 04:38:09 +04:00
"flag"
"fmt"
"log"
"net/netip"
"os"
"os/signal"
"strings"
"syscall"
)
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")
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 {
fmt.Println("dwelling-files ver.", version, "\nCopyright (c) 2022 Alexander \"Arav\" Andreev <me@arav.top>")
2022-06-27 04:38:09 +04:00
return
}
hand := handlers.New(directoryPath, web.Assets(), *disableFileHandler)
2022-06-27 04:38:09 +04:00
srv := server.NewHttpServer()
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)
}
}