Skip to content

Commit

Permalink
Initialize project
Browse files Browse the repository at this point in the history
  • Loading branch information
juunini committed Nov 6, 2021
1 parent 5623b80 commit 9a9f36d
Show file tree
Hide file tree
Showing 16 changed files with 21,275 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
node_modules
.serverless

# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# etc
.DS_Store
.idea
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run fmt
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["functions/*"]
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Rust Serverless Lambda Template

![Rust](https://img.shields.io/badge/Rust-black?style=for-the-badge&logo=rust&logoColor=#E57324)
![Serverless](https://img.shields.io/badge/Serverless-black?style=for-the-badge&logo=serverless&logoColor=#E57324)
![AWS Lambda](https://img.shields.io/badge/AWS_Lambda-232F3E?style=for-the-badge&logo=amazonaws&logoColor=white)
![NPM](https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white)

---

![Repo Size](https://img.shields.io/github/repo-size/sex-request/rust-serverless-lambda-template)
![Stars](https://img.shields.io/github/stars/sex-request/rust-serverless-lambda-template?style=social)

This setting used by [This Repository](https://github.com/sex-request/backend)

## Default Setting

- Rust
- lambda_http
- tokio
- serde_json
- Serverless
- serverless-rust
- dockerless
- default docker image's rust version is 1.43.1
- before 1.50 caused compile error
- Using husky
- pre-commit : cargo fmt

## Should Required Settings

- [Install aws cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
- setting `aws configure`
- [serverless-rust](https://www.serverless.com/plugins/serverless-rust) should setting `x86_64-unknown-linux-musl`
- if you use `dockerless` option

## [ IMPORTANT ] Must Change Here

- `package.json` line 2 - project name
- `serverless.yml` line 1 - service
- `serverless.yml` line 3 - provider
- `serverless.yml` line 21 - functions

## Usage

### Install

```sh
npm ci && npm run postinstall
```

### Test

```sh
npm run test # cargo test
```

### Deploy

```sh
npm run deploy:dev
npm run deploy:qa
npm run deploy:prod
```

### Formatting

```sh
npm run fmt:install # install rustfmt
npm run fmt # formatting
npm run fmt:check # check lint
```
9 changes: 9 additions & 0 deletions functions/health/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "health"
version = "0.1.0"
edition = "2018"

[dependencies]
tokio = { version = "^1", features = ["macros"] }
lambda_http = "^0.4"
serde_json = "^1"
18 changes: 18 additions & 0 deletions functions/health/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use lambda_http::lambda_runtime::Error;
use lambda_http::{handler, lambda_runtime, Context, IntoResponse, Request};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Error> {
lambda_runtime::run(handler(health)).await?;
Ok(())
}

async fn health(_: Request, _: Context) -> Result<impl IntoResponse, Error> {
// `serde_json::Values` impl `IntoResponse` by default
// creating an application/json response
Ok(json!({ "message": "OK" }))
}

#[cfg(test)]
mod main_test;
12 changes: 12 additions & 0 deletions functions/health/src/main_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::*;

#[tokio::test]
async fn health_handle() {
let request = Request::default();
let expected = json!({ "message": "OK" }).into_response();
let response = health(request, Context::default())
.await
.expect("expected Ok(_) value")
.into_response();
assert_eq!(response.body(), expected.body())
}
10 changes: 10 additions & 0 deletions functions/index/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "index"
version = "0.1.0"
edition = "2018"

[dependencies]
tokio = { version = "^1", features = ["macros"] }
lambda_http = "^0.4"
serde_json = "^1"
utils = { path = "../../utils" }
17 changes: 17 additions & 0 deletions functions/index/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use lambda_http::lambda_runtime::Error;
use lambda_http::{handler, lambda_runtime, Context, Request};
use serde_json::Value;
use utils::query_from_req;

#[tokio::main]
async fn main() -> Result<(), Error> {
lambda_runtime::run(handler(index)).await?;
Ok(())
}

async fn index(req: Request, _: Context) -> Result<Value, Error> {
return Ok(query_from_req(req));
}

#[cfg(test)]
mod main_test;
19 changes: 19 additions & 0 deletions functions/index/src/main_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::*;
use lambda_http::http::Request;
use lambda_http::Body;
use serde_json::json;

#[tokio::test]
async fn without_querystring() {
let req = Request::builder()
.uri("http://localhost")
.body(Body::from(()))
.unwrap();

assert_eq!(
index(req, Context::default())
.await
.expect("expected Ok(_) value"),
json!({})
)
}
Loading

0 comments on commit 9a9f36d

Please sign in to comment.