

Learning Go – Miniblog #13 – More On Server Objects | Late Developer
source link: https://latedev.wordpress.com/2013/01/24/learning-go-miniblog-13-more-on-server-objects/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Learning Go – Miniblog #13 – More On Server Objects
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.
Recommend
-
12
Twitter Follow me at @gamozolabs on Twitter if you want notifications when new blogs come up. I also do random one-off posts for cool data that doesn’t warrant an entire blog! Let...
-
9
My Windows Development Tools December 29, 2014 Introduction I haven’t done a “list of things” blog post in a while, so I thought I’d post a li...
-
4
Learning Go – Miniblog #12 – Server Objects January 23, 2013 This carries on from
-
9
Learning Go – Miniblog #14 – Closures and Goroutines January 24, 2013 This carries on from
-
7
Learning Go – Miniblog #10 – HTTP Servers January 22, 2013 This carries on from
-
7
Learning Go – Miniblog #9 – Reading CSV January 16, 2013 This carries on from he...
-
12
Learning Go – Miniblog #11 – More HTTP January 22, 2013 This carries on from ...
-
6
Learning Go – Miniblog #8 – Unit Tests January 14, 2013 This carries on from
-
5
Learning Go – Miniblog #6 – Defer January 12, 2013 This carries on from he...
-
6
Learning Go – Miniblog #7 – Creating Packages January 13, 2013 This carries on from h...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK