11

Learning Go – Miniblog #13 – More On Server Objects | Late Developer

 3 years ago
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.
neoserver,ios ssh client

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
Report this ad

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK