all repos — nimhttpd @ fe30ff083d62419870a952677f2e8a372d32f270

A useful static file web server.

Added extra files for release management; other minor updates.
h3rald h3rald@h3rald.com
Sat, 05 May 2018 16:11:03 +0200
commit

fe30ff083d62419870a952677f2e8a372d32f270

parent

3f640b344f49652f0bc2c9c81e870c824d997130

6 files changed, 171 insertions(+), 15 deletions(-)

jump to
M .gitignore.gitignore

@@ -1,2 +1,4 @@

nimcache/ nimhttpd +*.zip +nakefile
M README.mdREADME.md

@@ -1,2 +1,20 @@

-# nimhttpd -A tiny static file web server written in Nim +[![Nimble](https://raw.githubusercontent.com/yglukhov/nimble-tag/master/nimble.png)](https://github.com/h3rald/nimhttpd) + +![release](https://img.shields.io/github/release/h3rald/nimhttpd/all.svg) +![build](https://img.shields.io/travis/h3rald/nimhttpd.svg) +![license](https://img.shields.io/github/license/h3rald/nimhttpd.svg) + +# NimHTTPd + +_NimHHTPd_ is a minimal web server that can be used to serve static files. + +## Usage + +**nimhttpd** **[** **-p:**_port_ **]** **[** _directory_ **]** + +Where: + +* _directory_ is the directory to serve (default: current directory). +* _port_ is the port to listen to (default: 1337). + +
A nakefile.nim

@@ -0,0 +1,58 @@

+import + nake + +import + nimhttpd + +const + compile = "nim c -d:release" + linux_x86 = "--cpu:i386 --os:linux" + linux_x64 = "--cpu:amd64 --os:linux" + linux_arm = "--cpu:arm --os:linux" + windows_x64 = "--cpu:amd64 --os:windows" + macosx_x64 = "" + #parallel = "--parallelBuild:1 --verbosity:3" + hs = "nimhttpd" + hs_file = "nimhttpd.nim" + zip = "zip -X" + +proc filename_for(os: string, arch: string): string = + return "nimhttpd" & "_v" & version & "_" & os & "_" & arch & ".zip" + +task "windows-x64-build", "Build NimHTTPd for Windows (x64)": + direshell compile, windows_x64, hs_file + +task "linux-x86-build", "Build NimHTTPd for Linux (x86)": + direshell compile, linux_x86, hs_file + +task "linux-x64-build", "Build NimHTTPd for Linux (x64)": + direshell compile, linux_x64, hs_file + +task "linux-arm-build", "Build NimHTTPd for Linux (ARM)": + direshell compile, linux_arm, hs_file + +task "macosx-x64-build", "Build NimHTTPd for Mac OS X (x64)": + direshell compile, macosx_x64, hs_file + +task "release", "Release NimHTTPd": + echo "\n\n\n WINDOWS - x64:\n\n" + runTask "windows-x64-build" + direshell zip, filename_for("windows", "x64"), hs & ".exe" + direshell "rm", hs & ".exe" + echo "\n\n\n LINUX - x64:\n\n" + runTask "linux-x64-build" + direshell zip, filename_for("linux", "x64"), hs + direshell "rm", hs + echo "\n\n\n LINUX - x86:\n\n" + runTask "linux-x86-build" + direshell zip, filename_for("linux", "x86"), hs + direshell "rm", hs + echo "\n\n\n LINUX - ARM:\n\n" + runTask "linux-arm-build" + direshell zip, filename_for("linux", "arm"), hs + direshell "rm", hs + echo "\n\n\n MAC OS X - x64:\n\n" + runTask "macosx-x64-build" + direshell zip, filename_for("macosx", "x64"), hs + direshell "rm", hs + echo "\n\n\n ALL DONE!"
M nimhttpd.nimnimhttpd.nim

@@ -1,11 +1,55 @@

-import asynchttpserver, asyncdispatch, asyncnet, os, strutils, mimetypes, times, parseopt +import + asynchttpserver, + asyncdispatch, + asyncnet, + os, strutils, + mimetypes, + times, + parseopt, + parsecfg, + streams, + strutils from httpcore import HttpMethod, HttpHeaders -const style = "style.css".slurp +const + style = "style.css".slurp + cfgfile = "nimhttpd.nimble".slurp -let appname = "NimHTTPd Web Server" -let appversion = "1.0.3" -let usage = appname & " v" & appversion & " - Tiny Static File Web Server" & """ +var + name* = "NimHTTPd" + version*: string + description*: string + f = newStringStream(cfgfile) + +if f != nil: + var p: CfgParser + open(p, f, "../litestore.nimble") + while true: + var e = next(p) + case e.kind + of cfgEof: + break + of cfgKeyValuePair: + case e.key: + of "version": + version = e.value + of "description": + description = e.value + else: + discard + of cfgError: + stderr.writeLine("Configuration error.") + quit(1) + else: + discard + close(p) +else: + stderr.writeLine("Cannot process configuration file.") + quit(2) + + + +let usage = name & " v" & version & " - " & description & """ (c) 2014-2018 Fabio Cevasco

@@ -14,7 +58,9 @@ nimhttpd [-p:port] [directory]

Arguments: directory The directory to serve (default: current directory). - port Listen to port (default: 1337). + + Options: + -p, --port The port to listen to (default: 1337). """

@@ -29,11 +75,11 @@ directory*: string

mimes*: MimeDb port*: Port address*: string - appname: string - appversion*: string + name: string + version*: string proc h_page(settings:NimHttpSettings, content: string, title=""): string = - var footer = """<div id="footer">$1 v$2</div>""" % [settings.appname, settings.appversion] + var footer = """<div id="footer">$1 v$2</div>""" % [settings.name, settings.version] result = """ <!DOCTYPE html> <html>

@@ -134,7 +180,7 @@ res = sendStaticFile(settings, path)

else: res = sendNotFound(settings, path) await req.respond(res.code, res.content, res.headers) - echo settings.appname, " v", settings.appversion, " started on port ", int(settings.port), "." + echo settings.name, " v", settings.version, " started on port ", int(settings.port), "." echo "Serving directory ", settings.directory asyncCheck server.serve(settings.port, handleHttpRequest, settings.address)

@@ -155,7 +201,7 @@ of "help", "h":

echo usage quit(0) of "version", "v": - echo appversion + echo version quit(0) of "port", "p": try:

@@ -188,8 +234,8 @@ settings.directory = www

settings.logging = logging settings.mimes = newMimeTypes() settings.address = address - settings.appname = appname - settings.appversion = appversion + settings.name = name + settings.version = version settings.port = port serve(settings)
A nimhttpd.nim.cfg

@@ -0,0 +1,21 @@

+define:release + +# https://gist.github.com/Drakulix/9881160 +amd64.windows.gcc.path = "/usr/local/mingw/bin" +amd64.windows.gcc.exe = "x86_64-w64-mingw32-gcc" +amd64.windows.gcc.linkerexe = "x86_64-w64-mingw32-gcc" + +# http://crossgcc.rts-software.org/doku.php?id=compiling_for_linux +i386.linux.gcc.path = "/usr/local/gcc-4.8.1-for-linux32/bin" +i386.linux.gcc.exe = "i586-pc-linux-gcc" +i386.linux.gcc.linkerexe = "i586-pc-linux-gcc" + +# http://crossgcc.rts-software.org/doku.php?id=compiling_for_linux +amd64.linux.gcc.path = "/usr/local/gcc-4.8.1-for-linux64/bin" +amd64.linux.gcc.exe = "x86_64-pc-linux-gcc" +amd64.linux.gcc.linkerexe = "x86_64-pc-linux-gcc" + +# http://www.jaredwolff.com/toolchains/ +arm.linux.gcc.path = "/usr/local/arm-none-linux-gnueabi/bin" +arm.linux.gcc.exe = "arm-none-linux-gnueabi-gcc" +arm.linux.gcc.linkerexe = "arm-none-linux-gnueabi-gcc"
A nimhttpd.nimble

@@ -0,0 +1,11 @@

+[Package] +name = "nimhttpd" +version = "1.0.4" +author = "Fabio Cevasco" +description = "A tiny static file web server." +license = "MIT" +bin = "nimhttpd" +skipFiles = @["nakefile.nim"] + +[Deps] +requires: "nim >= 0.18.0"