1
0
Fork 0
mirror of https://github.com/Luzifer/gziphttp.git synced 2024-11-09 16:00:04 +00:00
gziphttp/main.go

52 lines
1.3 KiB
Go
Raw Permalink Normal View History

2019-06-01 23:12:09 +00:00
package main
import (
"fmt"
"net/http"
"os"
log "github.com/sirupsen/logrus"
httphelper "github.com/Luzifer/go_helpers/v2/http"
"github.com/Luzifer/rconfig/v2"
)
var (
cfg = struct {
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"`
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
ServeDir string `flag:"serve-dir,d" default:"." description:"Directory to serve files from"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
version = "dev"
)
func init() {
rconfig.AutoEnv(true)
if err := rconfig.ParseAndValidate(&cfg); err != nil {
log.Fatalf("Unable to parse commandline options: %s", err)
}
if cfg.VersionAndExit {
fmt.Printf("gziphttp %s\n", version)
os.Exit(0)
}
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
log.WithError(err).Fatal("Unable to parse log level")
} else {
log.SetLevel(l)
}
}
func main() {
http.Handle("/", http.FileServer(http.Dir(cfg.ServeDir)))
var handler http.Handler = http.DefaultServeMux
handler = httphelper.GzipHandler(handler)
handler = httphelper.NewHTTPLogHandler(handler)
log.WithError(http.ListenAndServe(cfg.Listen, handler)).Error("HTTP server ended")
2019-06-01 23:12:09 +00:00
}