[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do I run WEBrick as a daemon?

Ashley Moran

12/13/2006 2:08:00 PM

Hi

I'm trying to make a simple web server to provide decryption to a C#
app that can't access OpenSSL libraries. It has to run on a FreeBSD
server so I need to make it run as a daemon, but I can't work out how.

The skeleton of the code is below. I've noticed there is a
WEBrick::Daemon class but I can't figure out how to use it. I've
tried using script/server from Rails as a template but it's pretty
complex. Can anyone offer some hints?

Thanks in advance
Ashley



#!/usr/bin/env ruby

class PasswordServer
include WEBrick

def initialize(port)
decrypter_proc = lambda { |request, response|
# blah
}

decrypt = HTTPServlet::ProcHandler.new(decrypter_proc)

@server = HTTPServer.new(:Port => port)
@server.mount("/decrypt", decrypt)
end

def start
trap("INT") { server.shutdown }
trap("TERM") { server.shutdown }
server.start
end
end

if $0 == __FILE__
PasswordServer.new(2999).start
end

4 Answers

Gregory Brown

12/13/2006 2:46:00 PM

0

On 12/13/06, Ashley Moran <work@ashleymoran.me.uk> wrote:

> The skeleton of the code is below. I've noticed there is a
> WEBrick::Daemon class but I can't figure out how to use it. I've
> tried using script/server from Rails as a template but it's pretty
> complex. Can anyone offer some hints?

*blows dust off of gambit*

We just pass a become_daemon flag in our subclass of
Webrick::HTTPServer, and then use this to determine whether or not to
start the Daemon. There is some extra stuff in there, but I figured
it'd be useful to see it in context.

def initialize( game_class,
port = 80, html_dir = "html", become_daemon = false )
super(:Port => port)

@html_dir = File.join(File.expand_path(File.dirname($0)), html_dir)
self.class.const_set(:SERVER, self)
@server_message = nil

@game_class = game_class
@games = Hash.new
@players = Hash.new

WEBrick::Daemon.start if become_daemon

mount_proc("/") { |req, resp| serve_file(req, resp, req.path) }
mount_proc("/view") { |req, resp| serve_view(req, resp) }
mount_proc("/event") { |req, resp| serve_event(req, resp) }
mount_proc("/server") { |req, resp| serve_server_event(req, resp) }

['INT', 'TERM'].each { |signal| trap(signal) { shutdown } }
end

Gregory Brown

12/13/2006 2:47:00 PM

0

On 12/13/06, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 12/13/06, Ashley Moran <work@ashleymoran.me.uk> wrote:
>
> > The skeleton of the code is below. I've noticed there is a
> > WEBrick::Daemon class but I can't figure out how to use it. I've
> > tried using script/server from Rails as a template but it's pretty
> > complex. Can anyone offer some hints?
>
> *blows dust off of gambit*
>
> We just pass a become_daemon flag in our subclass of
> Webrick::HTTPServer, and then use this to determine whether or not to
> start the Daemon. There is some extra stuff in there, but I figured
> it'd be useful to see it in context.

Here is the whole server, btw. http://tinyurl....

Note, bug JEG2 if you have questions, aside from light debugging, I've
only worked with the gambit server externally.

Ashley Moran

12/13/2006 3:33:00 PM

0


On 13 Dec 2006, at 14:46, Gregory Brown wrote:

>
> Here is the whole server, btw. http://tinyurl....
>
> Note, bug JEG2 if you have questions, aside from light debugging, I've
> only worked with the gambit server externally.
>


Cheers! I've extracted the important bits out of your code and it's
working fine. Thanks for replying so quick.

Ashley

Gregory Brown

12/13/2006 4:35:00 PM

0

On 12/13/06, Ashley Moran <work@ashleymoran.me.uk> wrote:

> Cheers! I've extracted the important bits out of your code and it's
> working fine. Thanks for replying so quick.

Great to hear. Best of luck!
-greg