diff --git a/README.md b/README.md index e7fd9d0..8ff9ecb 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,19 @@ # miniws -miniws (minimalist web server) is a very simple web server written in golang. its purpose is to be lightweight, easy to configure and easily expandable for personal use. +miniws (minimal web server) is a very simple web server written in golang. its purpose is to be lightweight, easy to configure and easily expandable for personal use. ## command line arguments ``` - -h --help Print help information + -h --help Print help information + -s --signal runs the executable in command mode, meaning it will + just send a command to an already running miniws server + process, then terminate. Default: -p --port what port miniws will run on. Default: 8040 -l --logs-folder the logs folder. Default: logs -c --config-folder the configurations folder. Default: config -w --www-folder the www folder where miniws will look for files to serve. Default: . + -b --max-log-bytes the maximum bytes after which the log files get split. + Default: 1048576 ``` ## how to configure diff --git a/main.go b/main.go index 1ad8296..e765b85 100644 --- a/main.go +++ b/main.go @@ -11,18 +11,23 @@ import ( ) const ( + HELP_LONGDESCRIPTION string = "minimal web server - lightweight and easy to configure web " + + "server.\n\nYou can specify the following options via command line arguments: the server port, " + + "where to put the configuration files (they are auto-generated on first run), the folder with your " + + "web content, the folder to put the access and error logs inside, and more (see below)." HELP_PORT string = "what port miniws will run on" HELP_LOGFOLDER string = "the logs folder" HELP_CONFIGFOLDER string = "the configurations folder" HELP_WWWFOLDER string = "the www folder where miniws will look for files to serve" HELP_MAXLOGBYTES string = "the maximum bytes after which the log files get split" - HELP_SIGNAL string = "runs the executable in command mode, meaning it will just send a command to the already running miniws server" + HELP_SIGNAL string = "runs the executable in command mode, meaning it will just " + + "send a command to an already running miniws server process, then terminate" ) func main() { - parser := argparse.NewParser("miniws", "") + parser := argparse.NewParser("miniws", HELP_LONGDESCRIPTION) - signal := parser.String("s", "signal", &argparse.Options{Default: "", Help: HELP_SIGNAL}) + signal := parser.String("s", "signal", &argparse.Options{Help: HELP_SIGNAL}) port := parser.Int("p", "port", &argparse.Options{Default: 8040, Help: HELP_PORT}) logFolder := parser.String("l", "logs-folder", &argparse.Options{Default: "logs", Help: HELP_LOGFOLDER}) configFolder := parser.String("c", "config-folder", &argparse.Options{Default: "config", Help: HELP_CONFIGFOLDER}) @@ -37,12 +42,14 @@ func main() { return } + // signal mode if *signal != "" { client := sockets.Client{} client.OneShotWrite("unix", miniws.SOCKET_PATH, []byte(*signal)) return } + // webserver mode webserver := miniws.NewWebServer(*port, *logFolder, *configFolder, *wwwFolder, int64(*maxLogBytes)) webserver.Run() diff --git a/miniws/sockets/buffer.go b/miniws/sockets/buffer.go index f1b89f9..b803a3d 100644 --- a/miniws/sockets/buffer.go +++ b/miniws/sockets/buffer.go @@ -3,5 +3,6 @@ package sockets type buffer []byte func (b *buffer) Zero() { + // shoutout to The C Programming Language by Kernighan & Ritchie *b = make(buffer, len(*b)) } diff --git a/miniws/sockets/server.go b/miniws/sockets/server.go index 653467d..fc269b6 100644 --- a/miniws/sockets/server.go +++ b/miniws/sockets/server.go @@ -12,7 +12,7 @@ import ( type Server struct{} -func (s *Server) Start(RecvBind func(string, []string) byte, network, address string) int { +func (s *Server) Start(recvBind func(string, []string) bool, network, address string) int { socket, err := net.Listen(network, address) logplus.LogIfErrorFatal(err) @@ -49,10 +49,17 @@ func (s *Server) Start(RecvBind func(string, []string) byte, network, address st logplus.LogIfErrorFatal(err) fullstring := string(buffer) arguments := strings.Split(fullstring, " ") - ret := RecvBind(arguments[0], arguments[1:]) - conn.Write([]byte{ret}) + ret := recvBind(arguments[0], arguments[1:]) + conn.Write([]byte{bool2byte(ret)}) buffer.Zero() } }(conn) } } + +func bool2byte(b bool) byte { + if b { + return 1 + } + return 0 +} diff --git a/miniws/webserver.go b/miniws/webserver.go index be01de7..59244a2 100644 --- a/miniws/webserver.go +++ b/miniws/webserver.go @@ -70,6 +70,7 @@ func (ws *WebServer) Run() { ws.ipFilterMode, ws.ipFilter = ws.parseFilterPanics(FILENAME_IPFILTER) ws.userAgentFilterMode, ws.userAgentFilter = ws.parseFilterPanics(FILENAME_USERAGENTFILTER) + // create and start a unix socket server (to accept signal from another process using -s ) socketserver := sockets.Server{} go socketserver.Start(ws.recvBind, "unix", SOCKET_PATH) @@ -78,16 +79,16 @@ func (ws *WebServer) Run() { http.ListenAndServe(":"+strconv.Itoa(ws.port), nil) } -func (ws *WebServer) recvBind(command string, arguments []string) byte { +func (ws *WebServer) recvBind(command string, arguments []string) bool { command = string(bytes.Trim([]byte(command), "\x00")) switch command { case "reload": ws.parseFilterPanics(FILENAME_IPFILTER) ws.parseFilterPanics(FILENAME_USERAGENTFILTER) - return byte(1) + return true default: log.Println("Error: unknown command", command, arguments) - return byte(0) + return false } }