mirror of
https://github.com/Luzifer/gziphttp.git
synced 2025-01-02 01:11:19 +00:00
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
|
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("/", httphelper.GzipHandler(http.FileServer(http.Dir(cfg.ServeDir))))
|
||
|
log.WithError(http.ListenAndServe(cfg.Listen, nil)).Error("HTTP server ended")
|
||
|
}
|