need to refresh on go modules. implemented rudimentary config and cli arguments.

This commit is contained in:
Joshua Ashton
2024-08-31 13:03:52 -06:00
parent 053e50c248
commit 2f8d435b77
7 changed files with 606 additions and 84 deletions
@@ -0,0 +1,31 @@
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
// Serve a directory to a port.
func ServePage(router *mux.Router, dest string, path string, port int) {
router.PathPrefix(path).Handler(http.FileServer(http.Dir(dest)))
srv := &http.Server{
Addr: "0.0.0.0: " + string(port),
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: router, // Pass our instance of gorilla/mux in.
}
// Run our server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Println(err)
}
}()
log.Println("Web server is running on port " + string(port))
}