You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Ghost
  2. Ghost is a web development library loosely inspired by node's [Connect library][connect]. It provides a number of simple, single-responsibility HTTP handlers that can be combined to build a full-featured web server, and a generic template engine integration interface.
  3. It stays close to the metal, not abstracting Go's standard library away. As a matter of fact, any stdlib handler can be used with Ghost's handlers, they simply are `net/http.Handler`'s.
  4. ## Installation and documentation
  5. `go get github.com/PuerkitoBio/ghost`
  6. [API reference][godoc]
  7. *Status* : **Unmaintained**
  8. ## Example
  9. See the /ghostest directory for a complete working example of a website built with Ghost. It shows all handlers and template support of Ghost.
  10. ## Handlers
  11. Ghost offers the following handlers:
  12. * BasicAuthHandler : basic authentication support.
  13. * ContextHandler : key-value map provider for the duration of the request.
  14. * FaviconHandler : simple and efficient favicon renderer.
  15. * GZIPHandler : gzip-compresser for the body of the response.
  16. * LogHandler : fully customizable request logger.
  17. * PanicHandler : panic-catching handler to control the error response.
  18. * SessionHandler : store-agnostic server-side session provider.
  19. * StaticHandler : convenience handler that wraps a call to `net/http.ServeFile`.
  20. Two stores are provided for the session persistence, `MemoryStore`, an in-memory map that is not suited for production environment, and `RedisStore`, a more robust and scalable [redigo][]-based Redis store. Because of the generic `SessionStore` interface, custom stores can easily be created as needed.
  21. The `handlers` package also offers the `ChainableHandler` interface, which supports combining HTTP handlers in a sequential fashion, and the `ChainHandlers()` function that creates a new handler from the sequential combination of any number of handlers.
  22. As a convenience, all functions that take a `http.Handler` as argument also have a corresponding function with the `Func` suffix that take a `http.HandlerFunc` instead as argument. This saves the type-cast when a simple handler function is passed (for example, `SessionHandler()` and `SessionHandlerFunc()`).
  23. ### Handlers Design
  24. The HTTP handlers such as Basic Auth and Context need to store some state information to provide their functionality. Instead of using variables and a mutex to control shared access, Ghost augments the `http.ResponseWriter` interface that is part of the Handler's `ServeHTTP()` function signature. Because this instance is unique for each request and is not shared, there is no locking involved to access the state information.
  25. However, when combining such handlers, Ghost needs a way to move through the chain of augmented ResponseWriters. This is why these *augmented writers* need to implement the `WrapWriter` interface. A single method is required, `WrappedWriter() http.ResponseWriter`, which returns the wrapped ResponseWriter.
  26. And to get back a specific augmented writer, the `GetResponseWriter()` function is provided. It takes a ResponseWriter and a predicate function as argument, and returns the requested specific writer using the *comma-ok* pattern. Example, for the session writer:
  27. ```Go
  28. func getSessionWriter(w http.ResponseWriter) (*sessResponseWriter, bool) {
  29. ss, ok := GetResponseWriter(w, func(tst http.ResponseWriter) bool {
  30. _, ok := tst.(*sessResponseWriter)
  31. return ok
  32. })
  33. if ok {
  34. return ss.(*sessResponseWriter), true
  35. }
  36. return nil, false
  37. }
  38. ```
  39. Ghost does not provide a muxer, there are already many great ones available, but I would recommend Go's native `http.ServeMux` or [pat][] because it has great features and plays well with Ghost's design. Gorilla's muxer is very popular, but since it depends on Gorilla's (mutex-based) context provider, this is redundant with Ghost's context.
  40. ## Templates
  41. Ghost supports the following template engines:
  42. * Go's native templates (needs work, at the moment does not work with nested templates)
  43. * [Amber][]
  44. TODO : Go's mustache implementation.
  45. ### Templates Design
  46. The template engines can be registered much in the same way as database drivers, just by importing for side effects (using `_ "import/path"`). The `init()` function of the template engine's package registers the template compiler with the correct file extension, and the engine can be used.
  47. ## License
  48. The [BSD 3-Clause license][lic].
  49. [connect]: https://github.com/senchalabs/connect
  50. [godoc]: http://godoc.org/github.com/PuerkitoBio/ghost
  51. [lic]: http://opensource.org/licenses/BSD-3-Clause
  52. [redigo]: https://github.com/garyburd/redigo
  53. [pat]: https://github.com/bmizerany/pat
  54. [amber]: https://github.com/eknkc/amber