|
1 | 1 | /*
|
2 |
| -Package core is the perfect foundation for any web app as it's designed to have the best balance between readability, flexibility and performance. |
| 2 | +Package core provides a pure handlers (or middlewares) stack so you can perform actions downstream, then filter and manipulate the response upstream. |
3 | 3 |
|
4 |
| -It provides a pure handlers (or "middlewares") stack so you can perform actions downstream, then filter and manipulate the response upstream. |
| 4 | +The handlers stack |
5 | 5 |
|
6 |
| -No handlers or helpers are bundled in the Core: it does one thing and does it well. |
7 |
| -You can find official packages below. |
8 |
| -
|
9 |
| -For a complete documentation, see the Volatile website (http://volatile.whitedevops.com). |
10 |
| -You can also read all the code (~100 LOC) within minutes. |
11 |
| -
|
12 |
| -Installation |
13 |
| -
|
14 |
| -In the terminal: |
15 |
| -
|
16 |
| - $ go get github.com/volatile/core |
17 |
| -
|
18 |
| -Usage |
| 6 | +A handler is a function that receives a context. |
| 7 | +It can be registered with Use and has the possibility to break the sream or to continue with the next handler in the stack. |
19 | 8 |
|
20 | 9 | Example of a logger followed by the response writing:
|
21 | 10 |
|
22 |
| - package main |
| 11 | + // Log |
| 12 | + core.Use(func(c *core.Context) { |
| 13 | + start := time.Now() |
| 14 | + c.Next() |
| 15 | + log.Printf(" %s %s %s", c.Request.Method, c.Request.URL, time.Since(start)) |
| 16 | + }) |
| 17 | +
|
| 18 | + // Response |
| 19 | + core.Use(func(c *core.Context) { |
| 20 | + fmt.Fprint(c.ResponseWriter, "Hello, World!") |
| 21 | + }) |
23 | 22 |
|
24 |
| - import ( |
25 |
| - "fmt" |
26 |
| - "log" |
27 |
| - "time" |
| 23 | + // Run server |
| 24 | + core.Run() |
28 | 25 |
|
29 |
| - "github.com/volatile/core" |
30 |
| - ) |
| 26 | +When using Run, your app is reachable at http://localhost:8080 by default. |
31 | 27 |
|
32 |
| - func main() { |
33 |
| - // Log |
34 |
| - core.Use(func(c *core.Context) { |
35 |
| - start := time.Now() |
36 |
| - c.Next() |
37 |
| - log.Printf(" %s %s %s", c.Request.Method, c.Request.URL, time.Since(start)) |
38 |
| - }) |
| 28 | +If you need more flexibility, you can make a new handlers stack, which is fully compatible with the net/http.Handler interface: |
39 | 29 |
|
40 |
| - // Response |
41 |
| - core.Use(func(c *core.Context) { |
42 |
| - fmt.Fprint(c.ResponseWriter, "Hello, World!") |
43 |
| - }) |
| 30 | + hs := core.NewHandlersStack() |
44 | 31 |
|
45 |
| - core.Run() |
46 |
| - } |
| 32 | + hs.Use(func(c *core.Context) { |
| 33 | + fmt.Fprint(c.ResponseWriter, "Hello, World!") |
| 34 | + }) |
47 | 35 |
|
48 |
| -By default, your app is reachable at http://localhost:8080. |
| 36 | + http.ListenAndServe(":8080", hs) |
49 | 37 |
|
50 | 38 | Flags
|
51 | 39 |
|
52 | 40 | These flags are preset:
|
53 | 41 |
|
54 |
| -● -address to set a custom listening address. |
55 |
| -The value is saved in Address. |
| 42 | + -address The address to listen and serving on. |
| 43 | + Value is saved in Address. |
| 44 | + -production Run the server in production environment. |
| 45 | + Some third-party handlers may have different behaviors depending on the environment. |
| 46 | + Value is saved in Production. |
| 47 | +
|
| 48 | +It's up to you to call |
56 | 49 |
|
57 |
| -● -production to switch on production environment settings. |
58 |
| -Some third-party handlers may have different behaviors depending on the environment. |
59 |
| -The value is saved in Production. |
| 50 | + flag.Parse() |
60 | 51 |
|
61 |
| -It's up to you to call flag.Parse() in your main function if you want to use them. |
| 52 | +in your main function if you want to use them. |
62 | 53 |
|
63 | 54 | Panic recovering
|
64 | 55 |
|
65 |
| -Volatile Core recovers your server from any panic, logs the error with stack, and sends a 500 Internal Server Error. |
| 56 | +When using Run, your server always recovers from panics, logs the error with stack, and sends a 500 Internal Server Error. |
66 | 57 | If you want to use a custom handler on panic, give one to HandlePanic.
|
67 | 58 |
|
68 |
| -Compatibility |
69 |
| -
|
70 |
| -Volatile Core is fully compatible with the net/http.Handler interface. Just use NewHandlersStack: |
71 |
| -
|
72 |
| - package main |
73 |
| -
|
74 |
| - import ( |
75 |
| - "fmt" |
76 |
| - "net/http" |
77 |
| -
|
78 |
| - "github.com/volatile/core" |
79 |
| - ) |
80 |
| -
|
81 |
| - func main() { |
82 |
| - hs := core.NewHandlersStack() |
83 |
| -
|
84 |
| - hs.Use(func(c *core.Context) { |
85 |
| - fmt.Fprint(c.ResponseWriter, "Hello, World!") |
86 |
| - }) |
87 |
| -
|
88 |
| - http.ListenAndServe(":8080", hs) |
89 |
| - } |
90 |
| -
|
91 |
| -Official handlers |
92 |
| -
|
93 |
| -These handlers are ready to be integrated in any of your app: |
94 |
| -
|
95 |
| -● Compress — Clever response compressing — https://github.com/volatile/compress |
96 |
| -
|
97 |
| -● CORS — Cross-Origin Resource Sharing support — https://github.com/volatile/cors |
98 |
| -
|
99 |
| -● Log — Requests logging — https://github.com/volatile/log |
100 |
| -
|
101 |
| -● Secure — Quick security wins — https://github.com/volatile/secure |
102 |
| -
|
103 |
| -● Static — Simple assets serving — https://github.com/volatile/static |
104 |
| -
|
105 |
| -Official helpers |
106 |
| -
|
107 |
| -These helpers provide syntactic sugar to ease repetitive code: |
108 |
| -
|
109 |
| -● I18n — Simple internationalization — https://github.com/volatile/i18n |
110 |
| -
|
111 |
| -● Response — Readable response helper — https://github.com/volatile/response |
112 |
| -
|
113 |
| -● Route — Flexible routing helper — https://github.com/volatile/route |
| 59 | +Handlers & Helpers |
| 60 | +
|
| 61 | +No handlers or helpers are bundled in the core: it does one thing and does it well. |
| 62 | +That's why you need to import all and only the handlers or helpers you need: |
| 63 | +
|
| 64 | + compress Clever response compressing |
| 65 | + https://godoc.org/github.com/volatile/compress |
| 66 | + cors Cross-Origin Resource Sharing support |
| 67 | + https://godoc.org/github.com/volatile/cors |
| 68 | + i18n Simple internationalization |
| 69 | + https://godoc.org/github.com/volatile/i18n |
| 70 | + log Requests logging |
| 71 | + https://godoc.org/github.com/volatile/log |
| 72 | + response Readable response helper |
| 73 | + https://godoc.org/github.com/volatile/response |
| 74 | + route Flexible routing helper |
| 75 | + https://godoc.org/github.com/volatile/route |
| 76 | + secure Quick security wins |
| 77 | + https://godoc.org/github.com/volatile/secure |
| 78 | + static Simple assets serving |
| 79 | + https://godoc.org/github.com/volatile/static |
114 | 80 | */
|
115 | 81 | package core
|
0 commit comments