|
1 | | -# actix-cloud |
| 1 | +# actix-cloud |
| 2 | + |
| 3 | +Actix Cloud is an all-in-one web framework based on [Actix Web](https://github.com/actix/actix-web). |
| 4 | + |
| 5 | +## Features |
| 6 | +Actix Cloud is highly configurable. You can only enable needed features, implement your own feature backend or even use other libraries. |
| 7 | + |
| 8 | +- [logger](#logger) (Default: Enable) |
| 9 | +- [i18n](#i18n) (Default: Disable) |
| 10 | +- [security](#security) (Embedded) |
| 11 | +- memorydb backend |
| 12 | + - [default](#memorydb-default) (Embedded) |
| 13 | + - [redis](#memorydb-redis) (Default: Disable) |
| 14 | +- [auth](#auth) (Embedded) |
| 15 | +- [session](#session) (Default: Disable) |
| 16 | + |
| 17 | +## Guide |
| 18 | + |
| 19 | +### Quick Start |
| 20 | +You can refer to [Hello world](examples/hello_world/) example for basic usage. |
| 21 | + |
| 22 | +### Application |
| 23 | +Since application configuration can be quite dynamic, you need to build on your own. Here are some useful middlewares: |
| 24 | + |
| 25 | +``` |
| 26 | +App::new() |
| 27 | + .wrap(middleware::Compress::default()) // compress page |
| 28 | + .wrap(SecurityHeader::default().build()) // default security header |
| 29 | + .wrap(SessionMiddleware::builder(memorydb.clone(), Key::generate()).build()) // session |
| 30 | + ... |
| 31 | + .app_data(state_cloned.clone()) |
| 32 | +``` |
| 33 | + |
| 34 | +### logger |
| 35 | +We use [tracing](https://github.com/tokio-rs/tracing) as our logger library. It is thread safe. You can use it everywhere. |
| 36 | + |
| 37 | +Start logger: |
| 38 | +``` |
| 39 | +LoggerBuilder::new().level(Level::DEBUG).start() // colorful output |
| 40 | +LoggerBuilder::new().json().start() // json output |
| 41 | +``` |
| 42 | +You can also customize the logger with `filter`, `transformer`, etc. |
| 43 | + |
| 44 | +Reinit logger (e.g., in plugins), or manually send logs: |
| 45 | +``` |
| 46 | +logger.init(LoggerBuilder::new()); |
| 47 | +logger.sender().send(...); |
| 48 | +``` |
| 49 | + |
| 50 | +### i18n |
| 51 | +We use `rust-i18n-support` from [rust-i18n](https://github.com/longbridgeapp/rust-i18n) as our i18n core. |
| 52 | + |
| 53 | +Load locale: |
| 54 | +``` |
| 55 | +let mut locale = Locale::new(String::from("en-US")); |
| 56 | +locale.add_locale(i18n!("locale")); |
| 57 | +``` |
| 58 | + |
| 59 | +Translate: |
| 60 | +``` |
| 61 | +t!(locale, "hello.world") |
| 62 | +t!(locale, "hello.name", name = "MEME") |
| 63 | +``` |
| 64 | + |
| 65 | +See [examples](examples/i18n) for more usage. |
| 66 | + |
| 67 | +### security |
| 68 | +Middleware to add security headers: |
| 69 | +``` |
| 70 | +app.wrap(SecurityHeader::default().build()) |
| 71 | +``` |
| 72 | + |
| 73 | +Default header: |
| 74 | +``` |
| 75 | +X-Content-Type-Options: nosniff |
| 76 | +Referrer-Policy: strict-origin-when-cross-origin |
| 77 | +X-Frame-Options: DENY |
| 78 | +X-XSS-Protection: 1; mode=block |
| 79 | +Cross-Origin-Opener-Policy: same-origin |
| 80 | +Content-Security-Policy: default-src 'none'; script-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none' |
| 81 | +``` |
| 82 | + |
| 83 | +Enable HSTS when using HTTPS: |
| 84 | +``` |
| 85 | +security_header.set_default_hsts(); |
| 86 | +``` |
| 87 | +``` |
| 88 | +Strict-Transport-Security: max-age=31536000; includeSubDomains; preload |
| 89 | +``` |
| 90 | + |
| 91 | +### memorydb-default |
| 92 | +Actix Cloud has a default memory database backend used for sessions. You can also use your own backend if you implement `actix_cloud::memorydb::MemoryDB`. |
| 93 | + |
| 94 | +**Note: the default backend does not have memory limitation, DDoS is possible if gateway rate limiting is not implemented** |
| 95 | + |
| 96 | +``` |
| 97 | +DefaultBackend::new().await.unwrap() |
| 98 | +``` |
| 99 | + |
| 100 | +### memorydb-redis |
| 101 | +Redis can be used as another backend for memory database. |
| 102 | + |
| 103 | +``` |
| 104 | +RedisBackend::new("redis://user:[email protected]:6379/0").await.unwrap(), |
| 105 | +``` |
| 106 | + |
| 107 | +### auth |
| 108 | +Authentication is quite simple, you only need to implement an extractor and a checker. |
| 109 | + |
| 110 | +Extractor is used to extract your own authentication type from request. For example, assume we use 0 for guest and 1 for admin. Our authentication type is just `Vec<u32>`: |
| 111 | +``` |
| 112 | +fn perm_extractor(req: &mut ServiceRequest) -> Vec<u32> { |
| 113 | + let mut ret = Vec::new(); |
| 114 | + ret.push(0); // guest permission is assigned by default. |
| 115 | +
|
| 116 | + // test if query string has `admin=1`. |
| 117 | + let qs = QString::from(req.query_string()); |
| 118 | + if qs.get("admin").is_some_and(|x| x == "1") { |
| 119 | + ret.push(1); |
| 120 | + } |
| 121 | + ret |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +Checker is used to check the permission, the server will return 403 if the return value is false: |
| 126 | +``` |
| 127 | +fn is_guest(p: Vec<u32>) -> bool { |
| 128 | + p.into_iter().find(|x| *x == 0).is_some() |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +Then build the `Router` and configure in the App using `build_router`: |
| 133 | +``` |
| 134 | +app.service(scope("/api").configure(build_router(...))) |
| 135 | +``` |
| 136 | + |
| 137 | +### session |
| 138 | +Most features and usages are based on [actix-session](https://crates.io/crates/actix-session). Except for these: |
| 139 | +- MemoryDB is the only supported storage. |
| 140 | +- Error uses `actix-cloud::error::Error`. |
| 141 | +- You can set `_ttl` in the session to override the TTL of the session. |
| 142 | + |
| 143 | +``` |
| 144 | +app.wrap(SessionMiddleware::builder(memorydb.clone(), Key::generate()).build()) |
| 145 | +``` |
| 146 | + |
| 147 | +## License |
| 148 | +This project is licensed under the [MIT license](LICENSE). |
0 commit comments