No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

README.md 4.1KB

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