This is a toy project to test the go 1.8 plugins feature (which only works on linux at the moment). Work in progress!
The main motivation of the project is to try out go plugins building something that might, some day, become useful (or not, its ok).
go-plugserver
is an http server built that registers handlers dynamically, during the runtime, using go plugins. The server keeps polling the handlers
directory looking for files that look like compiled plugins (.so
extension), loads those plugins and then run a function that returns two things:
- A
string
, the handler pattern url, e.g,/users/
. - A
func(w http.ResponseWriter, r *http.Request))
that is the handler function to be added to the server and will handle requests for that pattern
If a file is removed from the handlers
directory, the registered pattern for that file will also be removed from the server. Without any downtime (needs to be confirmed by trusted sources :-))!
To register new handlers, compile any go file that has a function:
func Handler() (pattern string, handleFunc func(w http.ResponseWriter, r *http.Request))
using go build -buildmode=plugin -o myplugin.so myplugin.go
and place the generated .so
file
on the handlers
directory (check the examples already there).
Start the server with make run
.
- Compile the plugins during runtime
- Use fsnotify instead of polling every 5 seconds (maybe)
- Investigate a way to enable de-registering patterns without having to implement a ServerMux
- General support for muxers (depends on 3)
On the plughttp
directory there is a slightly modified version of golang/go
's http.ServerMux
featuring a DeRegister(pattern string)
method. The LICENSE on that file is the same as golang/go
and is on the plughttp
directory.