Skip to content

Commit

Permalink
Implemented a nanoc server command. Fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
cdchapman committed Nov 9, 2016
1 parent 1b5e4ed commit 6a201d6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ in scope before compiling:
#### Optional dependencies
* [optipng] for optimizing PNG images
* A Java 8 runtime for running the [Nu HTML] checker.
* [OpenResty] for running the dynamic parts of the site via `nanoc server`.

### Then clone this repo (including submodules), install bower components and Ruby gems…

Expand Down Expand Up @@ -58,7 +59,8 @@ $ nanoc view # if you are only dealing with statically generated pages
$ nanoc server # if you are dealing with both the static and dynamic aspects of the site
```

And view the page in your browser.
And view the page in your browser at [port 3000](http://localhost:3000/) for
`nanoc view` or [port 4125](https://localhost:4125/) for `nanoc server`.

## Contributing

Expand Down Expand Up @@ -95,3 +97,4 @@ Also, please see [the colophon][colophon] for more raving attributions!
[pygments]: <http://pygments.org/>
[bundler]: <http://bundler.io/>
[colophon]: <https://pentandra.com/colophon/>
[OpenResty]: <http://openresty.org/>
98 changes: 98 additions & 0 deletions commands/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
usage 'server [options]'
summary 'start the dynamic web server (nginx/OpenResty)'
description <<-EOS
Start the dynamic web server. Running this web server requires OpenResty
to be installed and in scope.
EOS

required :c, :conf, 'specify the configuration file to use (default: nginx.conf)'
required :g, :global, 'specify any global directives (default: daemon off;)'

module LifePreserver
class Server < ::Nanoc::CLI::CommandRunner
NGINX_BIN = 'nginx'
NGINX_SEARCH_PATHS = [
'/usr/local/openresty/nginx/sbin/',
'/usr/local/opt/openresty/bin/',
'/usr/local/bin/',
'/usr/sbin/',
'/opt/openresty/nginx/sbin/'
]

class Error < ::Nanoc::Int::Errors::Generic
def initialize(command, exit_code)
@command = command
@exit_code = exit_code
end

def message
"command exited with a nonzero status code #{@exit_code} (command: #{@command.join(' ')})"
end
end

def run
require 'open3'

load_site

conf = options[:conf] || 'nginx.conf'
directives = options[:global] || 'daemon off;'


nginx = find_nginx
output_dir = site.config[:output_dir]
cmd = [ nginx, '-p', output_dir, '-c', conf, '-g', directives ]

Open3.popen3(*cmd) do |_stdin, stdout, stderr, wait_thr|
puts "Starting OpenResty (#{nginx}) in path (#{output_dir}) with config (#{conf}) and global directives (#{directives})"

stdout_thread = Thread.new do
while (line = stdout.gets)
puts line
end
end

stderr_thread = Thread.new do
while (line = stderr.gets)
puts line
end
end

stdout_thread.join
stderr_thread.join

exit_status = wait_thr.value
unless exit_status.success?
raise Error.new(cmd, exit_status.to_i)
end
end
end

protected

def find_nginx
NGINX_SEARCH_PATHS.each do |path|
to_check = path + NGINX_BIN
if File.file?(to_check) && openresty?(to_check)
return to_check
end
end

# As a last resort, check the PATH
require 'mkmf'
to_check = find_executable0(NGINX_BIN)
if to_check && openresty?(to_check)
return to_check
end

raise "Cannot find the OpenResty executable in any of the following places: #{NGINX_SEARCH_PATHS.join(':')} or in the current path: #{ENV['PATH']}"
end

def openresty?(path_to_check)
output, _status = Open3.capture2e(path_to_check, '-v')
output && output['openresty']
end
end
end

runner LifePreserver::Server
4 changes: 0 additions & 4 deletions items/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ error_log <% if @config[:production] %>logs/error.log warn<% else %>stderr info<
# The file storing the process ID of the main process
pid <%= @config[:server][:pid_file] %>;

<% unless @config[:production] %>
daemon off;
<% end %>

http {

# Hide nginx version information.
Expand Down

0 comments on commit 6a201d6

Please sign in to comment.