{"id":26024412,"url":"https://github.com/therealpad/rust-api-crud-template","last_synced_at":"2026-04-15T15:34:40.897Z","repository":{"id":217777620,"uuid":"744784505","full_name":"TheRealPad/rust-api-crud-template","owner":"TheRealPad","description":"CRUD API in Rust","archived":false,"fork":false,"pushed_at":"2024-01-18T19:05:29.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-06T12:50:10.247Z","etag":null,"topics":["api","bash","crud","docker","integration-testing","rust","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TheRealPad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-18T01:58:34.000Z","updated_at":"2024-01-18T19:02:51.000Z","dependencies_parsed_at":"2024-01-18T07:57:48.597Z","dependency_job_id":"7f5244b2-289f-4253-8053-1313de50066d","html_url":"https://github.com/TheRealPad/rust-api-crud-template","commit_stats":null,"previous_names":["therealpad/rust-api-crud-template"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/TheRealPad/rust-api-crud-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheRealPad%2Frust-api-crud-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheRealPad%2Frust-api-crud-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheRealPad%2Frust-api-crud-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheRealPad%2Frust-api-crud-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheRealPad","download_url":"https://codeload.github.com/TheRealPad/rust-api-crud-template/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheRealPad%2Frust-api-crud-template/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267392562,"owners_count":24079919,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","bash","crud","docker","integration-testing","rust","unit-testing"],"created_at":"2025-03-06T12:33:05.546Z","updated_at":"2026-04-15T15:34:40.819Z","avatar_url":"https://github.com/TheRealPad.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Rust CI](https://github.com/TheRealPad/rust-api-crud-template/actions/workflows/ci.yml/badge.svg)\n![Docker deploy](https://github.com/TheRealPad/rust-api-crud-template/actions/workflows/deploy.yml/badge.svg)\n\n![Rust](https://img.shields.io/badge/rust-%23000000.svg?style=for-the-badge\u0026logo=rust\u0026logoColor=white)\n![Docker](https://img.shields.io/badge/docker-%230db7ed.svg?style=for-the-badge\u0026logo=docker\u0026logoColor=white)\n![Shell Script](https://img.shields.io/badge/shell_script-%23121011.svg?style=for-the-badge\u0026logo=gnu-bash\u0026logoColor=white)\n\n# RUST API CRUD TEMPLATE\n\n## How to run\n\n### install Rust | Docker\n\n[Rust](https://www.rust-lang.org/tools/install)\n\n[Docker](https://docs.docker.com/get-docker/)\n\n### run the api\n\nSet env variable : API_PORT\n\nRun ```cargo run```\n\nIf you want to user docker, run:\n```bash\ndocker-compose up # add --build if you want to start fresh or with new modifications\n```\n\n## How to create a new module\n\n1. Create new directory\n2. Create the file ```mod.rs```\n3. Declare the module in the direct parent of the new module (like main.rs for example)\n\n## How to add routes\n\n1. Create new module for the routes\n2. In the module_name.rs, add a content like this\n    ```rust\n    lazy_static! {\n        pub static ref MODULE_NAME_HANDLERS: Vec\u003c(\u0026'static str, fn(\u0026HttpRequest) -\u003e (String, String))\u003e = {\n            vec![\n                (\"POST /users\", handle_post_request),\n                (\"GET /users\", handle_get_request),\n                (\"PUT /users\", handle_put_request),\n                (\"DELETE /users\", handle_delete_request),\n            ]\n        };\n    }\n        \n    pub fn handle_post_request(_request: \u0026HttpRequest) -\u003e (String, String) {\n        println!(\"{:?}\", _request.body[0]);\n        create_user::create_user();\n        return (api::OK_RESPONSE.to_string(), \"User created\".to_string());\n    }\n    ```\n3. in routes/initializer.rs, add you route handle here:\n    ```rust\n    pub fn initialize_handlers() -\u003e Vec\u003c(\u0026'static str, fn(\u0026HttpRequest) -\u003e (String, String))\u003e {\n        let mut handlers = Vec::new();\n    \n        handlers.extend(USER_HANDLERS.iter());\n        handlers\n    }\n    ```\n\n## How to add controllers\n1. Create a new module in ```src/controllers```\n2. Create all the functions needed in this module and make them public\n3. The function must be called in the functions of routes\n\n## How to add models\n1. Create a new file in ```src/models```\n2. export it in ```src/models/mod.rs```\n3. The content must be something like this:\n    ```rust\n    #[derive(Serialize, Deserialize)]\n    pub struct User {\n        pub id: Option\u003ci32\u003e,\n        pub name: String,\n        pub email: String,\n    }\n    ```\n\n## How to add constants\n1. If your constant is from the env -\u003e go to ```src/constants/env.rs```\n2. If your constant is relative to the API response -\u003e go to ```src/constant/api.rs```\n3. Else create a file in ```src/constants``` and export it in mod.rs\n4. To declare the constant, do the following:\n    ```rust\n    pub const OK_RESPONSE: \u0026str = \"HTTP/1.1 200 OK\\r\\nContent-Type: application/json\\r\\n\\r\\n\";\n    ```\n\n## How to add middlewares\n1. Create new module in ```src/midllewares```\n2. The middleware structure must implement ```Middleware```, like this:\n    ```rust\n    pub struct LoggingMiddleware;\n    \n    impl Middleware for LoggingMiddleware {\n        fn process(\u0026self, request: \u0026HttpRequest) -\u003e Result\u003cString, String\u003e {\n            let current_datetime: DateTime\u003cLocal\u003e = Local::now();\n            let year = current_datetime.year();\n            let month = current_datetime.month();\n            let day = current_datetime.day();\n            let hour = current_datetime.hour();\n            let minute = current_datetime.minute();\n            let second = current_datetime.second();\n    \n            println!(\"[{}-{:02}-{:02} {:02}:{:02}:{:02}]: {:?} {}\", year, month, day, hour, minute, second, request.method, request.endpoint);\n            Ok(\"\".to_string())\n        }\n    }\n    ```\n3. In case of error, the middleware must throw an error with ``Err``\n4. Add your middleware in ```src/middlewares/middleware.rs```\n    ```rust\n    pub fn middlewares(handler: \u0026fn(\u0026HttpRequest) -\u003e (String, String), request: \u0026str) -\u003e (String, String) {\n        let middlewares: Vec\u003cBox\u003cdyn Middleware\u003e\u003e = vec![\n            Box::new(LoggingMiddleware), // Add your middleware here\n        ];\n        let http_request = parse_http_request(request).unwrap();\n    \n        for middleware in \u0026middlewares {\n            match middleware.process(\u0026http_request) {\n                Ok(_result) =\u003e (),\n                Err(err) =\u003e {\n                    println!(\"Middleware error: {}\", err);\n                    return (api::UNAUTHORIZED.to_string(), err);\n                }\n            }\n        }\n        handler(\u0026http_request)\n    }\n    ```\n\n## How to add libs\n1. Create a new module in ```src/libs```\n\n## How to test and run tests\n\n### Unit test\n\n```bash\ncargo test\n```\n\n### Integration test\n\n```bash\n./tests/integration.sh\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftherealpad%2Frust-api-crud-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftherealpad%2Frust-api-crud-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftherealpad%2Frust-api-crud-template/lists"}