Spaces:
Sleeping
Sleeping
Gusti Adli Anshari
commited on
Commit
•
e4dfce5
0
Parent(s):
first commit
Browse files- .dockerignore +18 -0
- .gitignore +16 -0
- Cargo.toml +11 -0
- LICENSE +21 -0
- README.md +1 -0
- config.yaml +1 -0
- src/main.rs +55 -0
.dockerignore
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Generated by Cargo
|
2 |
+
# will have compiled files and executables
|
3 |
+
debug/
|
4 |
+
target/
|
5 |
+
|
6 |
+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
7 |
+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
8 |
+
Cargo.lock
|
9 |
+
|
10 |
+
# These are backup files generated by rustfmt
|
11 |
+
**/*.rs.bk
|
12 |
+
|
13 |
+
# MSVC Windows builds of rustc generate these, which store debugging information
|
14 |
+
*.pdb
|
15 |
+
|
16 |
+
.vscode
|
17 |
+
|
18 |
+
config.yaml
|
.gitignore
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Generated by Cargo
|
2 |
+
# will have compiled files and executables
|
3 |
+
debug/
|
4 |
+
target/
|
5 |
+
|
6 |
+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
7 |
+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
8 |
+
Cargo.lock
|
9 |
+
|
10 |
+
# These are backup files generated by rustfmt
|
11 |
+
**/*.rs.bk
|
12 |
+
|
13 |
+
# MSVC Windows builds of rustc generate these, which store debugging information
|
14 |
+
*.pdb
|
15 |
+
|
16 |
+
.vscode
|
Cargo.toml
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[package]
|
2 |
+
name = "rust-rocket-counter-api"
|
3 |
+
version = "0.1.0"
|
4 |
+
edition = "2021"
|
5 |
+
|
6 |
+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
7 |
+
|
8 |
+
[dependencies]
|
9 |
+
rocket = "0.5.0-rc.2"
|
10 |
+
serde_yaml = "0.9.33"
|
11 |
+
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 Gusti Adli Anshari
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
simple counter api written in rust used as hands-on docker
|
config.yaml
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
username: John Mayer
|
src/main.rs
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
use rocket::{get, launch, routes};
|
2 |
+
use rocket::State;
|
3 |
+
use std::env;
|
4 |
+
use std::fs::File;
|
5 |
+
use std::io::Read;
|
6 |
+
use std::sync::{Arc, Mutex};
|
7 |
+
|
8 |
+
struct Counter {
|
9 |
+
value: i32,
|
10 |
+
username: String,
|
11 |
+
}
|
12 |
+
|
13 |
+
#[get("/")]
|
14 |
+
fn index(counter: &State<Arc<Mutex<Counter>>>) -> String {
|
15 |
+
let counter_lock = counter.lock().unwrap();
|
16 |
+
format!("Hi {}!\nCounter: {}", counter_lock.username, counter_lock.value)
|
17 |
+
}
|
18 |
+
|
19 |
+
#[get("/add")]
|
20 |
+
fn increment(counter: &State<Arc<Mutex<Counter>>>) -> String {
|
21 |
+
let mut counter_lock = counter.lock().unwrap();
|
22 |
+
counter_lock.value += 1;
|
23 |
+
format!("Hi {}!\nIncremented!\nNew value: {}", counter_lock.username, counter_lock.value)
|
24 |
+
}
|
25 |
+
|
26 |
+
#[get("/reset")]
|
27 |
+
fn reset(counter: &State<Arc<Mutex<Counter>>>) -> String {
|
28 |
+
let mut counter_lock = counter.lock().unwrap();
|
29 |
+
counter_lock.value = 0;
|
30 |
+
format!("Hi {}!\nReset Success!\nCounter: {}", counter_lock.username, counter_lock.value)
|
31 |
+
}
|
32 |
+
|
33 |
+
#[launch]
|
34 |
+
fn rocket() -> _ {
|
35 |
+
let config_path: String = match env::var("CONFIG_PATH") {
|
36 |
+
Ok(val) => val,
|
37 |
+
Err(_) => "config.yaml".to_string(),
|
38 |
+
};
|
39 |
+
println!("Using config file: {config_path}");
|
40 |
+
|
41 |
+
let mut f: File = std::fs::File::open(config_path).expect("Can't open config.yaml");
|
42 |
+
|
43 |
+
let mut str: String = String::new();
|
44 |
+
f.read_to_string(&mut str).expect("Can't read config.yaml");
|
45 |
+
|
46 |
+
let data: serde_yaml::Value = serde_yaml::from_str(&str).expect("Can't parse config.yaml");
|
47 |
+
let username: String = data["username"]
|
48 |
+
.as_str()
|
49 |
+
.map(|s| s.to_string())
|
50 |
+
.expect("Can't find username in config.yaml");
|
51 |
+
|
52 |
+
rocket::build()
|
53 |
+
.manage(Arc::new(Mutex::new(Counter { value: 0, username: username })))
|
54 |
+
.mount("/", routes![index, increment, reset])
|
55 |
+
}
|