選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. [![Build Status](https://travis-ci.org/goanywhere/rex.svg?branch=master)](https://travis-ci.org/goanywhere/rex)
  4. Rex is a library for modular web development in [Go](http://golang.org/), designed to work directly with net/http.
  5. <img alt="wrk" src="https://raw.githubusercontent.com/goanywhere/rex/assets/images/wrk.png">
  6. ## Intro
  7. Nah, not another **Web Framework**, we have that enough.The more we spend on [Go](http://golang.org/), the more clearly we realize that most lightweight, pure-stdlib conventions really do scale to large groups of developers and diverse project ecosystems. You absolutely don’t need a *Web Framework* like you normally do in other languages, simply because your code base has grown beyond a certain size. Or you believe it might grow beyond a certain size! You truly ain’t gonna need it. What we really need is just a suitable routing system, along with some common toolkits for web development, the standard idioms and practices will continue to function beautifully at scale.
  8. ## Getting Started
  9. Install the package, along with executable binary helper (**go 1.4** and greater is required):
  10. ```shell
  11. $ go get -v github.com/goanywhere/rex/...
  12. ```
  13. ## Features
  14. * Flexible Env-based configurations.
  15. * Awesome routing system provided by [Gorilla/Mux](//github.com/gorilla/mux).
  16. * Group routing system with middleware modules supports
  17. * Non-intrusive/Modular design, extremely easy to use.
  18. * Standard & modular system based on [http.Handler](http://godoc.org/net/http#Handler) interface.
  19. * Command line tools
  20. * Auto-compile/reload for .go & .html sources
  21. * Browser-based Live reload supports for HTML templates
  22. * **Fully compatible with the [http.Handler](http://godoc.org/net/http#Handler)/[http.HandlerFunc](http://godoc.org/net/http#HandlerFunc) interface.**
  23. After installing Go and setting up your [GOPATH](http://golang.org/doc/code.html#GOPATH), create your first server.
  24. ``` go
  25. package main
  26. import (
  27. "io"
  28. "net/http"
  29. "github.com/goanywhere/rex"
  30. )
  31. func main() {
  32. rex.Get("/", func(w http.ResponseWriter, r *http.Request) {
  33. io.WriteString(w, "Hello World")
  34. })
  35. rex.Run()
  36. }
  37. ```
  38. Then start your server:
  39. ``` shell
  40. rex run
  41. ```
  42. You will now have a HTTP server running on `localhost:5000`.
  43. ## Settings
  44. 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.
  45. ``` go
  46. package main
  47. import (
  48. "io"
  49. "github.com/goanywhere/rex"
  50. "github.com/goanywhere/x/env"
  51. )
  52. func index(w http.ResponseWriter, r *http.Request) {
  53. io.WriteString("Hey you")
  54. }
  55. func main() {
  56. // Override default 5000 port here.
  57. env.Set("PORT", 9394)
  58. rex.Get("/", index)
  59. rex.Run()
  60. }
  61. ```
  62. You will now have the HTTP server running on `0.0.0.0:9394`.
  63. 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).
  64. ## Middleware
  65. Middlware modules work between http requests and the router, they are no different than the standard http.Handler. Existing middleware 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:
  66. ``` go
  67. app.Use(middleware.XSRF)
  68. ```
  69. Since a middleware module is just the standard http.Handler, writing custom middleware is also pretty straightforward:
  70. ``` go
  71. app.Use(func(next http.Handler) http.Handler {
  72. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  73. log.Printf("Custom Middleware Module Started")
  74. next.ServeHTTP(writer, request)
  75. log.Printf("Custom Middleware Module Ended")
  76. })
  77. })
  78. ```
  79. Using prefixed (aka. subrouter) router is exactly same as the main one:
  80. ```go
  81. app := rex.new()
  82. app.Get("/", func(w http.ResponseWriter, r *http.Request) {
  83. io.WriteString(w, "index page")
  84. })
  85. user := app.Group("/users")
  86. user.Use(func(next http.Handler) http.Handler {
  87. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  88. log.Printf("this is a protected page")
  89. next.ServeHTTP(writer, request)
  90. })
  91. })
  92. ```
  93. ## Frameworks comes & dies, will this be supported?
  94. Positive! Rex is an internal/fundamental project at GoAnywhere. We developed it and we are going to continue using/improving it.
  95. ##Roadmap for v1.0
  96. - [X] Env-Based Configurations
  97. - [X] CLI Apps Integrations
  98. - [X] Performance Boost
  99. - [X] Hot-Compile Runner
  100. - [X] Live Reload Integration
  101. - [X] Common Middleware Modules
  102. - [X] Continuous Integration
  103. - [ ] Full Test Converage
  104. - [ ] Improved Template Rendering
  105. - [ ] Unified Rendering
  106. - [ ] Project Wiki
  107. - [ ] Stable API