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

68 lines
1.5 KiB
Go

package main
import (
"dwelling-files/internal/handlers"
"dwelling-files/internal/server"
"dwelling-files/web"
"flag"
"fmt"
"log"
"net/netip"
"os"
"os/signal"
"strings"
"syscall"
)
var version string
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)")
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>")
return
}
hand := handlers.New(directoryPath, web.Assets(), *disableFileHandler)
srv := server.NewHttpServer()
srv.GET("/*filepath", hand.Index)
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)
}
}