Neocrates
Neocrates is a feature-gated Rust utility facade crate that bundles practical building blocks for web services, storage, PostgreSQL, Redis, SMS, captcha, auth, and crypto workflows without forcing every dependency into every binary.
Chinese overview: README.zh-CN.md
Why Neocrates
Neocrates is useful when you want one crate to provide a consistent set of infrastructure helpers:
- Feature-gated modules so you can keep binaries smaller than an “everything on” toolkit.
- Facade-style re-exports for common crates such as Tokio, Axum, Diesel, SQLx, Redis, Serde, tracing, and more.
- Opinionated service helpers for recurring backend work: Redis OTPs, request middleware, STS credentials, captcha storage, SQL logging, and password hashing.
- Runnable examples in
examples/for Axum extractors, SMS flows, and captcha APIs.
This repository is a single crate, not a workspace. The root API is defined in src/lib.rs.
Module map
| Feature | Public module(s) | What you get | Module guide |
|---|---|---|---|
| default | helper |
IDs, retry, pagination, serde helpers, config loading, utility validators | src/helper/README.md |
logger |
logger |
tracing-subscriber bootstrap with local timestamps |
src/logger/README.md |
web |
middlewares, response |
Axum middleware, request auth, unified API errors and responses | src/middlewares/README.md, src/response/README.md |
diesel |
dieselhelper |
PostgreSQL pool, auto-create DB, Diesel SQL logging macros | src/dieselhelper/README.md |
sqlx |
sqlxhelper |
PostgreSQL pool, migrations, SQLx SQL logging macros | src/sqlxhelper/README.md |
redis |
rediscache |
Redis pool, pipelines, key helpers, distributed locks | src/rediscache/README.md |
auth |
auth |
Redis-backed token lifecycle and fingerprint helpers | src/auth/README.md |
captcha |
captcha |
Slider, numeric, and alphanumeric captcha flows | src/captcha/README.md |
awss3 |
awss3 |
Low-level S3-compatible object client | src/awss3/README.md |
awssts |
awssts |
Low-level Aliyun/Tencent STS clients | src/awssts/README.md |
aws |
aws, awss3, awssts |
Higher-level storage/STS service layer | src/aws/README.md |
sms |
sms |
Aliyun/Tencent SMS providers and OTP workflow | src/sms/README.md |
crypto |
crypto |
Argon2 password hashing and misc crypto helpers | src/crypto/README.md |
Practical feature combinations
The crate has a few practical combinations that are safer than turning on isolated flags blindly:
[]
# Explore everything
= { = "0.1", = ["full"] }
# Web API foundation
= { = "0.1", = false, = ["web", "logger"] }
# Redis-backed auth + captcha
= { = "0.1", = false, = ["web", "redis", "auth", "captcha"] }
# PostgreSQL with Diesel
= { = "0.1", = false, = ["diesel"] }
# PostgreSQL with SQLx
= { = "0.1", = false, = ["sqlx"] }
# Low-level S3-compatible client
= { = "0.1", = false, = ["awss3"] }
# SMS OTP flow
= { = "0.1", = false, = ["sms", "redis", "web"] }
Notes:
authis currently useful withwebandredis.captchais currently useful withwebandredis.middlewaresis currently safest to use withwebandcrypto.smsis typically used withwebbecause provider implementations rely on the HTTP stack.awss3is the cleanest low-level storage entry point. The broaderawsmodule adds higher-level configuration-driven helpers.
Quick start
1. Bootstrap logging
async
2. Return typed API errors in an Axum handler
use ;
async
async
3. Initialize Redis and read/write a key
async
4. Hash and verify a password
Detailed usage tutorials
1. Build an Axum service foundation
Recommended features:
= { = "0.1", = false, = ["web", "logger", "crypto"] }
Suggested flow:
- Initialize logging with
logger::run()orlogger::init(LogConfig). - Return
AppResult<T>from handlers soAppErrorcan become a consistent JSON response. - Use
LoggedJson<T>orDetailedJson<T>for request parsing when you want structured JSON error output. - Add
middlewares::interceptorwhen you want Redis-backed or in-memory token validation and creator/updater injection.
Example:
use Arc;
use ;
use DetailedJson;
use ;
use ;
use Deserialize;
async
async
async
Related module docs:
src/helper/README.mdsrc/middlewares/README.mdsrc/response/README.mdexamples/axum_extractor_example.rs
2. Choose a PostgreSQL integration
You currently have two helper paths:
| If you want... | Use... |
|---|---|
| Diesel DSL + deadpool-diesel + logged Diesel macros | dieselhelper |
| SQLx + migrations + logged SQLx macros | sqlxhelper |
Diesel path
use ;
async
SQLx path
use ;
async
Related module docs:
3. Use Redis for cache, auth, and captcha
Recommended features:
= { = "0.1", = false, = ["web", "redis", "auth", "captcha"] }
Step-by-step:
- Initialize a
RedisPool. - Use
AuthHelperto issue or rotate tokens. - Use
CaptchaServiceto generate a numeric or slider challenge. - Reuse the same Redis deployment for OTPs, sessions, or lock keys.
Example:
use Arc;
use AuthHelper;
use CaptchaService;
use AuthModel;
use RedisPool;
async
Related module docs:
4. Work with object storage and temporary credentials
There are two layers:
awss3for a direct S3-compatible clientawsfor the higher-level, config-driven service wrapper
Low-level storage client
use Duration;
use AwsClient;
async
High-level service wrapper
Use AwsService when your application already owns an AwsConfig and wants a singleton-like helper for object download/upload/presigned URLs. Use CosService when you want config-driven STS output and Redis caching for Aliyun credentials.
Related module docs:
5. Send SMS verification codes
Recommended features:
= { = "0.1", = false, = ["sms", "redis", "web"] }
Suggested flow:
- Build a
SmsConfigfor Aliyun or Tencent. - Set
debug: truelocally to skip real SMS sending. - Call
SmsService::send_captcha(...). - Validate later with
SmsService::valid_auth_captcha(...).
Example:
use Arc;
use RedisPool;
use ;
async
Related docs:
Environment variables
Core/library environment variables
| Variable | Used by | Notes |
|---|---|---|
ENV |
helper::core::loader |
Controls environment-specific YAML config file search. |
RUST_LOG |
logger |
Overrides configured tracing level. |
DATABASE_URL |
sqlxhelper::from_env, examples |
PostgreSQL connection string. |
DATABASE_POOL_SIZE |
sqlxhelper::from_env, examples |
SQLx pool size. |
REDIS_URL |
rediscache::from_env, examples |
Redis connection string. |
REDIS_MAX_SIZE |
rediscache::from_env |
Max pool size. |
REDIS_MIN_IDLE |
rediscache::from_env |
Min idle connections. |
REDIS_CONNECTION_TIMEOUT |
rediscache::from_env |
Seconds. |
REDIS_IDLE_TIMEOUT |
rediscache::from_env |
Seconds. |
REDIS_MAX_LIFETIME |
rediscache::from_env |
Seconds. |
Example-focused environment variables
These are used by example binaries rather than read automatically by the library:
| Variable | Example | Notes |
|---|---|---|
SMS_PROVIDER |
sms_example |
aliyun or tencent. |
SMS_DEBUG |
sms_example |
true/1 skips real SMS sending. |
MOBILE |
sms_example |
Demo mobile number. |
ALIYUN_SMS_ACCESS_KEY_ID |
sms_example |
Aliyun SMS credential. |
ALIYUN_SMS_ACCESS_KEY_SECRET |
sms_example |
Aliyun SMS credential. |
ALIYUN_SMS_SIGN_NAME |
sms_example |
Aliyun SMS sign name. |
ALIYUN_SMS_TEMPLATE_CODE |
sms_example |
Aliyun template code. |
TENCENT_SMS_SECRET_ID |
sms_example |
Tencent SMS credential. |
TENCENT_SMS_SECRET_KEY |
sms_example |
Tencent SMS credential. |
TENCENT_SMS_APP_ID |
sms_example |
Tencent SMS app ID. |
TENCENT_SMS_REGION |
sms_example |
Example region such as ap-beijing. |
TENCENT_SMS_SIGN_NAME |
sms_example |
Tencent sign name. |
TENCENT_SMS_TEMPLATE_ID |
sms_example |
Tencent template ID. |
Examples and module guides
Runnable examples
Scenario guides
Module guides
src/helper/README.mdsrc/logger/README.mdsrc/middlewares/README.mdsrc/response/README.mdsrc/dieselhelper/README.mdsrc/sqlxhelper/README.mdsrc/rediscache/README.mdsrc/auth/README.mdsrc/captcha/README.mdsrc/aws/README.mdsrc/awss3/README.mdsrc/awssts/README.mdsrc/sms/README.mdsrc/crypto/README.md
Development commands
Direct Cargo equivalents:
Roadmap
Neocrates already covers a broad backend toolkit surface. The most meaningful next steps are:
- Tighten feature wiring so practical dependency sets such as
web + auth + redis,web + captcha + redis, andweb + sms + redisare clearer and safer. - Expand module-level examples for selective-feature builds, not only
full. - Improve typed configuration builders for storage, STS, SMS, and auth workflows.
- Expose more cache ergonomics around the existing
redis+mokadependency set. - Harden security-sensitive edges such as STS HTTP client configuration and crypto helper naming/output clarity.
- Add deeper docs.rs coverage with end-to-end examples for each major module.
License
MIT. See LICENSE.