Need help with gohttplib?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

shazow
209 Stars 18 Forks MIT License 47 Commits 5 Opened issues

Description

Go buildmodes prototype: Expose net/http.Server as a library with C bindings

Services available

!
?

Need anything else?

Contributors list

# 16,529
React N...
React
python-...
viml
43 commits
# 359,400
Haskell
C
2 commits
# 703,239
Go
C
1 commit

gohttplib

Shared library that exposes Go's

net/http.Server
with externally-bindable handlers.

This is a silly project for experimenting with Go buildmodes. I gave a talk about this at PyCon 2016.

Status: Tiny subset of the

http.HandlerFunc
callback gets passed to a C handler callback. Python bindings are working, too.

Getting Started

Requirements:

  • Go 1.5 or newer.
  • C example: make, gcc
  • Python example: cffi, python-cffi
$ git clone https://github.com/shazow/gohttplib/
$ cd gohttplib
$ make examples

Example: C

C example can be linked against a shared object (

libgohttp.so
generated by
go build -buildmode=c-shared
) or against a static library archive (
libgohttp.a
generated by
go build -buildmode=c-archive
). By default, we link against the shared object because that's what the Python example uses too.

Linked against our shared object:

$ make example-c
$ DYLD_LIBRARY_PATH=build/ ./build/gohttp-c

Note that you'll need to make sure that

libgohttp.so
is findable at runtime.

Now you can request

http://localhost:8000/hello
and the C handler in
examples/c/main.c
will handle it!

Linked against our static library archive:

$ make example-c-static
$ ./build/gohttp-c-static

The static archive gets built into the binary so it's more portable during runtime. Note the size differences:

8.8K  gohttp-c
5.1M  gohttp-c-static
7.5M  libgohttp.a
6.9M  libgohttp.so

Example: Python

The Python example uses python-cffi (you'll need python-cffi installed) to link against the shared object.

$ make example-python
$ cd examples/python
$ python -m gohttp
 * Running on http://127.0.0.1:5000/

Or write your own handler using this library:

from gohttp import route, run

@route('/') def index(w, req): w.write("%s %s %s\n" % (req.method, req.host, req.url)) w.write("Hello, world.\n")

run(host='127.0.0.1', port=5000)

References & Credit

Sponsors

This project was made possible thanks to Glider Labs.

License

MIT

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.