diff --git a/config.json b/config.json index 9b3a97a..126473a 100644 --- a/config.json +++ b/config.json @@ -1,14 +1,14 @@ { - "HOST": "127.0.0.1", - "PORT": "3333", - "QUALITY": "80", - "IMG_PATH": "/Users/benny/goLandProject/webp_server_go/pics", - "EXHAUST_PATH": "", - "ALLOWED_TYPES": [ - "jpg", - "png", - "jpeg", - "bmp", - "gif" - ] + "HOST": "127.0.0.1", + "PORT": "3333", + "QUALITY": "80", + "IMG_PATH": "/Users/benny/goLandProject/webp_server_go/pics", + "EXHAUST_PATH": "", + "ALLOWED_TYPES": [ + "jpg", + "png", + "jpeg", + "bmp", + "gif" + ] } diff --git a/webp-server.go b/webp-server.go index 05330d2..a2b7cf8 100644 --- a/webp-server.go +++ b/webp-server.go @@ -35,8 +35,43 @@ type Config struct { ExhaustPath string `json:"EXHAUST_PATH"` } +const version = "0.0.3" + var configPath string var prefetch bool +var dumpConfig bool +var dumpSystemd bool + +const sampleConfig = ` +{ + "HOST": "127.0.0.1", + "PORT": "3333", + "QUALITY": "80", + "IMG_PATH": "/Users/benny/goLandProject/webp_server_go/pics", + "EXHAUST_PATH": "", + "ALLOWED_TYPES": ["jpg", "png", "jpeg", "bmp", "gif"] +} +` +const sampleSystemd = ` +[Unit] +Description=WebP Server +Documentation=https://github.com/n0vad3v/webp_server_go +After=nginx.target + +[Service] +Type=simple +StandardError=journal +AmbientCapabilities=CAP_NET_BIND_SERVICE +WorkingDirectory=/opt/webps +ExecStart=/opt/webps/webp-server --config /opt/webps/config.json +ExecReload=/bin/kill -HUP $MAINPID +Restart=always +RestartSec=3s + + +[Install] +WantedBy=multi-user.target +` func loadConfig(path string) Config { var config Config @@ -124,6 +159,8 @@ func webpEncoder(p1, p2 string, quality float32, Log bool, c chan int) (err erro func init() { flag.StringVar(&configPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)") flag.BoolVar(&prefetch, "prefetch", false, "Prefetch and convert image to webp") + flag.BoolVar(&dumpConfig, "dump-config", false, "Print sample config.json") + flag.BoolVar(&dumpSystemd, "dump-systemd", false, "Print sample systemd service file.") flag.Parse() } @@ -289,6 +326,18 @@ func main() { ExhaustPath = config.ExhaustPath } + // process cli params + if dumpConfig { + fmt.Println(sampleConfig) + os.Exit(0) + } + if dumpSystemd { + fmt.Println(sampleSystemd) + + os.Exit(0) + + } + if prefetch { prefetchImages(confImgPath, ExhaustPath, QUALITY) } @@ -300,7 +349,7 @@ func main() { ListenAddress := HOST + ":" + PORT // Server Info - ServerInfo := "WebP Server is running at " + ListenAddress + ServerInfo := "WebP Server " + version + " is running at " + ListenAddress fmt.Println(ServerInfo) app.Get("/*", Convert(confImgPath, ExhaustPath, AllowedTypes, QUALITY))