feat: add alibaba cloud fn template
This commit is contained in:
@@ -1,3 +1,10 @@
|
|||||||
# alibabacloudfn-template
|
# alibabacloudfn-template
|
||||||
|
|
||||||
Alibaba Cloud Function Template
|
Alibaba Cloud Function Template
|
||||||
|
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
```shell
|
||||||
|
$ kickstart https://git.hatter.ink/templates/alibabacloudfn-template.git
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
49
template.toml
Normal file
49
template.toml
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
name = "Rust Alibaba Cloud Function template"
|
||||||
|
description = "Hatter's Rust Alibaba Cloud Function template"
|
||||||
|
kickstart_version = 1
|
||||||
|
ignore = [
|
||||||
|
".gitignore",
|
||||||
|
"README.md",
|
||||||
|
"LICENSE",
|
||||||
|
]
|
||||||
|
|
||||||
|
cleanup = [
|
||||||
|
]
|
||||||
|
|
||||||
|
[[variables]]
|
||||||
|
name = "project_name"
|
||||||
|
default = "My-Fn"
|
||||||
|
prompt = "What's the name of the project?"
|
||||||
|
validation = "^([a-zA-Z][a-zA-Z0-9_-]+)$"
|
||||||
|
|
||||||
|
[[variables]]
|
||||||
|
name = "bin"
|
||||||
|
default = "my-fn"
|
||||||
|
prompt = "What's the name of the executable?"
|
||||||
|
validation = "^([a-zA-Z][a-zA-Z-_]+)$"
|
||||||
|
|
||||||
|
[[variables]]
|
||||||
|
name = "author"
|
||||||
|
default = "No One"
|
||||||
|
prompt = "What is your name?"
|
||||||
|
|
||||||
|
[[variables]]
|
||||||
|
name = "email"
|
||||||
|
default = "no@example.com"
|
||||||
|
prompt = "What is your email?"
|
||||||
|
|
||||||
|
[[variables]]
|
||||||
|
name = "license"
|
||||||
|
default = "MIT"
|
||||||
|
prompt = "Which open-source license do you want to use?"
|
||||||
|
choices = [
|
||||||
|
"MIT",
|
||||||
|
"BSD",
|
||||||
|
"GPLv3",
|
||||||
|
"None",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[variables]]
|
||||||
|
name = "description"
|
||||||
|
default = "A Alibaba Cloud Function application"
|
||||||
|
prompt = "A short description of the app?"
|
||||||
19
{{project_name}}/Cargo.toml
Normal file
19
{{project_name}}/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
name = "{{project_name}}"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["{{author}} <{{email}}>"]
|
||||||
|
edition = "2018"
|
||||||
|
{% if license != "None" -%}
|
||||||
|
license = "{{license}}"
|
||||||
|
{%- endif %}
|
||||||
|
description = "{{description}}"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "{{bin}}"
|
||||||
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tokio = { version = "0.2", features = ["full"] }
|
||||||
|
hyper = "0.13"
|
||||||
2
{{project_name}}/bootstrap
Executable file
2
{{project_name}}/bootstrap
Executable file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
./alibabacloundfn
|
||||||
10
{{project_name}}/justfile
Normal file
10
{{project_name}}/justfile
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# build alibaba cloud fn
|
||||||
|
buildfn:
|
||||||
|
if [ -f "code.zip" ]; then rm code.zip; fi
|
||||||
|
dockerbuild :image:rust:1.47 :mirror:git://mirrors.ustc.edu.cn/crates.io-index \
|
||||||
|
build --release --target-dir linux_target
|
||||||
|
cp linux_target/release/{{bin}} .
|
||||||
|
chmod +x bootstrap
|
||||||
|
zip code.zip bootstrap {{bin}}
|
||||||
|
rm {{bin}}
|
||||||
|
|
||||||
39
{{project_name}}/src/main.rs
Normal file
39
{{project_name}}/src/main.rs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
use std::convert::Infallible;
|
||||||
|
use hyper::service;
|
||||||
|
use hyper::{Body, Request, Response, Server};
|
||||||
|
|
||||||
|
async fn hello(request: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||||
|
let mut buff = String::with_capacity(1024);
|
||||||
|
buff.push_str(&request.method().to_string());
|
||||||
|
buff.push(' ');
|
||||||
|
buff.push_str(&request.uri().to_string());
|
||||||
|
buff.push(' ');
|
||||||
|
buff.push_str(&format!("{:?}", request.version().to_owned()));
|
||||||
|
buff.push('\n');
|
||||||
|
for header in request.headers() {
|
||||||
|
buff.push_str(&header.0.to_string());
|
||||||
|
buff.push_str(": ");
|
||||||
|
let val = header.1.to_str().unwrap_or_else(|_| "<to_string_error>");
|
||||||
|
if header.0.to_string().contains("access-key-secret") && !val.is_empty() {
|
||||||
|
buff.push_str("******");
|
||||||
|
} else {
|
||||||
|
buff.push_str(val);
|
||||||
|
}
|
||||||
|
buff.push('\n');
|
||||||
|
}
|
||||||
|
Ok(Response::new(Body::from(buff)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
let make_svc = service::make_service_fn(|_conn| {
|
||||||
|
async { Ok::<_, Infallible>(service::service_fn(hello)) }
|
||||||
|
});
|
||||||
|
|
||||||
|
let addr = ([0, 0, 0, 0], 9000).into();
|
||||||
|
let server = Server::bind(&addr).serve(make_svc);
|
||||||
|
println!("Listening on http://{}", addr);
|
||||||
|
|
||||||
|
server.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user