add command-line flag options

This commit is contained in:
David 2020-09-16 16:45:36 -04:00
parent 9d731726e9
commit 0a25e1a89c

12
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"bufio"
"crypto/tls"
"flag"
"mime"
"net"
"net/url"
@ -28,6 +29,11 @@ func handleConnection(log logger.Logger, conn net.Conn, h handlers.Handler) {
}
func main() {
hostname := flag.String("hostname", "localhost", "comma-separated list of hostnames to generate certificates for")
regenCerts := flag.Bool("regenerate-certs", false, "set to true to regenerate cert files")
serveFrom := flag.String("path", "./root/", "filepath to serve files from; defaults to ./root/")
flag.Parse()
log := logger.NewLogger(true)
mime.AddExtensionType(".gemini", "text/gemini")
@ -35,9 +41,9 @@ func main() {
err := certificates.TestCertificateExists("./certs/cert.pem", "./certs/key.pem")
var cer tls.Certificate
if err != nil {
if err != nil || *regenCerts {
log.Info("Generating new certificate...")
key, cert, err := certificates.GenerateKeyPair("localhost")
key, cert, err := certificates.GenerateKeyPair(*hostname)
if err != nil {
log.Info("error generating certificates", err)
return
@ -73,6 +79,6 @@ func main() {
continue
}
go handleConnection(log, conn, handlers.NewFileHandler("./root/"))
go handleConnection(log, conn, handlers.NewFileHandler(*serveFrom))
}
}