Skip to content

millerlogic/server-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc

server-go

Server implements a generic network server in Go. This is just a simple time saver so you don't have to implement yet another server. By default the client protocol is line-delimited, but any valid split function can be used. NewConn is always called in the goroutine which called Serve (or ListenAndServe). Afterwards, each connection runs in its own goroutine, so the Handler func can be called concurrently but only for different connections.

Simple TCP server:

srv := &server.Server{ Addr: ":7111", Handler: server.HandlerFunc(func(conn net.Conn, r *server.Request) {
	fmt.Printf("From %s: %s\n", conn.RemoteAddr(), r.Data)
}) }
srv.ListenAndServe()

Function ListenIO is provided which can turn an io.ReadWriteCloser into a net.Listener. Here's an example turning standard input and output into a server. Not extremely useful but it can be beneficial to support a consistent interface.

type stream struct {
	io.Reader
	io.WriteCloser
}

s := &stream{os.Stdin, os.Stdout}
ln := server.ListenIO(s) // make a net.Listener

srv := &server.Server{ Handler: HandlerFunc(func(conn net.Conn, r *server.Request) {
	fmt.Printf("From %s: %s\n", conn.RemoteAddr(), r.Data)
}) }
srv.Serve(ln)

About

Server implements a generic network server in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages