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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <a href="#"><img alt="rex" src="https://raw.githubusercontent.com/go-rex/rex/assets/images/rex.png" width="160px" height="64px"></a>
  2. ===
  3. Rex is a library for modular web development in [Go](http://golang.org/), designed to work directly with net/http.
  4. <img alt="wrk" src="https://raw.githubusercontent.com/goanywhere/rex/assets/images/wrk.png">
  5. ## Getting Started
  6. Install the package, along with executable binary helper (**go 1.4** and greater is required):
  7. ```shell
  8. $ go get -v github.com/goanywhere/rex/...
  9. ```
  10. ## Features
  11. * Flexible Env-based configurations.
  12. * Awesome routing system provided by [Gorilla/Mux](//github.com/gorilla/mux).
  13. * Group routing system with middleware modules supports
  14. * Non-intrusive/Modular design, extremely easy to use.
  15. * Standard & modular system based on [http.Handler](http://godoc.org/net/http#Handler) interface.
  16. * Command line tools
  17. * Auto-compile/reload for .go & .html sources
  18. * Browser-based Live reload supports for HTML templates
  19. * **Fully compatible with the [http.Handler](http://godoc.org/net/http#Handler)/[http.HandlerFunc](http://godoc.org/net/http#HandlerFunc) interface.**
  20. After installing Go and setting up your [GOPATH](http://golang.org/doc/code.html#GOPATH), create your first server.
  21. ``` go
  22. package main
  23. import (
  24. "io"
  25. "net/http"
  26. "github.com/goanywhere/rex"
  27. )
  28. func main() {
  29. rex.Get("/", func(w http.ResponseWriter, r *http.Request) {
  30. io.WriteString(w, "Hello World")
  31. })
  32. rex.Run()
  33. }
  34. ```
  35. Then start your server:
  36. ``` shell
  37. rex run
  38. ```
  39. You will now have a HTTP server running on `localhost:5000`.
  40. ## Settings
  41. All settings on Rex can be accessed via `env`, which essentially stored in `os.Environ`. By using this approach you can compile your own settings files into the binary package for deployment without exposing the sensitive settings, it also makes configuration extremly easy & flexible via both command line & application.
  42. ``` go
  43. package main
  44. import (
  45. "github.com/goanywhere/rex"
  46. "github.com/goanywhere/x/env"
  47. )
  48. func index (ctx *rex.Context) {
  49. ctx.Render("index.html")
  50. }
  51. func main() {
  52. // Override default 5000 port here.
  53. env.Set("PORT", 9394)
  54. rex.Get("/", index)
  55. rex.Run()
  56. }
  57. ```
  58. You will now have the HTTP server running on `0.0.0.0:9394`.
  59. Hey, dude, why not just use those popular approaches, like file-based config? We know you'll be asking & we have the answer as well, [here](http://12factor.net/config).
  60. ## Modules
  61. Modules (aka. middleware) work between http requests and the router, they are no different than the standard http.Handler. Existing modules from other frameworks like logging, authorization, session, gzipping are very easy to integrate into Rex. As long as it complies the standard `func(http.Handler) http.Handler` signature, you can simply add one like this:
  62. ``` go
  63. app.Use(modules.XSRF)
  64. ```
  65. Since a module is just the standard http.Handler, writing a custom module is also pretty straightforward:
  66. ``` go
  67. app.Use(func(next http.Handler) http.Handler {
  68. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  69. log.Printf("Custom Middleware Module Started")
  70. next.ServeHTTP(writer, request)
  71. log.Printf("Custom Middleware Module Ended")
  72. })
  73. })
  74. ```
  75. Using prefixed (aka. subrouter) router is exactly same as the main one:
  76. ```go
  77. app := rex.new()
  78. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  79. io.WriteString(w, "index page")
  80. })
  81. user := app.Group("/users")
  82. user.Use(func(next http.Handler) http.Handler {
  83. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  84. log.Printf("this is a protected page")
  85. next.ServeHTTP(writer, request)
  86. })
  87. })
  88. ```
  89. ## Frameworks comes & dies, will this be supported?
  90. Positive! Rex is an internal/fundamental project at GoAnywhere. We developed it and we are going to continue using/improving it.
  91. ##Roadmap for v1.0
  92. - [X] Env-Based Configurations
  93. - [X] Test Suite
  94. - [X] New Project Template
  95. - [X] CLI Apps Integrations
  96. - [X] Performance Boost
  97. - [X] Hot-Compile Runner
  98. - [X] Live Reload Integration
  99. - [X] Common Modules
  100. - [ ] Full Test Converage
  101. - [ ] Improved Template Rendering
  102. - [ ] Project Wiki
  103. - [ ] Continuous Integration
  104. - [ ] Stable API