Skip to content

Learning Go – Miniblog #13 – More On Server Objects

January 24, 2013

This carries on from here, and started here.

My original server code used the "default" HTTP server instance provided by Go’s library. Unfortunately, this (as far as I can tell) only allows you to listen on a siungle specific port, and only supports one set of request handlers. I want  to create multiple servers, capable of listening on multiple ports, and (possibly) having different sets of request handlers.

The Go library does support an http.Server type, so I modified my struct definition to contain a pointer to one:

type Server struct {
    port, title, datadir string
    https *http.Server
}

I then modified my NewServer function to create an http.Server (note despite similar names, csvsrv.Server and http.Server are distinct types):

s := &Server{port, title,datadir,&http.Server{Addr: ":"+port, Handler: nil } 

This allows me to create an http.Server instance listening on a specified port. It doesn’t solve the request handling problem however. To do that I had to dig through the documentation (aargh!) to find that the handling is performed by a type called ServeMux. As with the server type, the Go library provides a default, but I wanted a custom one. The http.Server class has a member called Handler (set to nil in the code above) to support a custom ServeMux, so I created one and added my existing handlers to it. The resulting implementation of my NewServer method looks like this:

func NewServer( port, title, datadir string ) (*Server) { 
    s := &Server{port, title,datadir,&http.Server{Addr: ":"+port, Handler: nil } }
    mux := http.NewServeMux()
    mux.HandleFunc( CSVROOT + IDXSTR + "/", csvIndexHandler )
    mux.HandleFunc( CSVROOT + DATASTR + "/", csvDataHandler )
    s.https.Handler = mux
    return s
}

I also needed to add a member function to make the server actually do something:

func (s *Server) Serve() {
    s.https.ListenAndServe()
}

With those in place, I actually had a working custom server! This code creates a server and starts it serving:

s1 := csvsrv.NewServer( "8080", "My Little Server", "csvdata" )
fmt.Printf( "Port: %s Title: %s Data: %s\n", s1.Port(), s1.Title(), s1.Datadir() )
s1.Serve()

Now only two problems remain to deal with – I need somehow  to "inject" the index title and data directory values into the request handler functions, and I need to deal with the fact that my Serve() function is blocking, so I cannot run another server instance.

Advertisements

From → golang

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: