Skip to content

Commit bee8e46

Browse files
committed
feat: Finish the lecture
1 parent 5f28289 commit bee8e46

File tree

12 files changed

+902
-12
lines changed

12 files changed

+902
-12
lines changed

docs/49_ko.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h1>프로젝트 준비</h1>
4242
<p><img src="https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbGZOnJ%2Fbtra8y6n31j%2FZyIuDLgVyQPqzknNrIXqSk%2Fimg.png" alt="BasicCard" /></p>
4343
<h1 id="">준비물:</h1>
4444
<ol>
45-
<li>public에서 접속 가능한 주소 (포트포워딩이나 AWS EC2를 이용)</li>
45+
<li>public에서 접속 가능한 머신 (포트포워딩이나 AWS EC2, <a href="https://fly.io" target="_blank" rel="noopener">fly.io</a> 이용)</li>
4646
<li>카카오 i 챗봇 만들기 <a href="https://i.kakao.com/" target="_blank" rel="noopener">@링크</a></li>
4747
<li>카카오톡 채널 만들기 <a href="https://center-pf.kakao.com/profiles" target="_blank" rel="noopener">@링크</a></li>
4848
</ol>

docs/50_ko.html

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,64 @@
3535
<span class="toc"><a href="TOC_ko.html">목차</a></span>
3636
</div>
3737
<div class="page">
38-
<h1>Chapter 6 - Conclusion</h1>
39-
<p>CSW</p>
38+
<h1>Hello World API</h1>
39+
<p>이번 튜토리얼에서는 Rust와 Actix-web을 이용하여 'Hello World' 메시지를 출력하는 기본적인 웹 서버를 만들어 보겠습니다.</p>
40+
<h2 id="">시작하기</h2>
41+
<p>첫 단계로, 새로운 binary 기반의 Cargo 프로젝트를 생성합니다:</p>
42+
<pre><code class="bash">cargo new hello-world
43+
cd hello-world</code></pre>
44+
<p>그 후, 프로젝트에 actix-web을 의존성으로 추가해야 합니다.</p>
45+
<p>이를 위해 <code>Cargo.toml</code> 파일을 열고 다음과 같이 입력합니다:</p>
46+
<pre><code class="toml">[dependencies]
47+
actix-web = "4"</code></pre>
48+
<h2 id="-1">핸들러 작성하기</h2>
49+
<p>웹 서버에서 요청을 처리하기 위해 핸들러 함수를 작성합니다.</p>
50+
<p>이 함수들은 비동기 함수로, 필요한 매개변수를 받아 HttpResponse를 반환합니다.</p>
51+
<p>이 HttpResponse는 웹 서버가 클라이언트에게 보낼 응답입니다.</p>
52+
<p><code>src/main.rs</code> 파일을 다음과 같이 수정합니다:</p>
53+
<pre><code class="rust">use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
54+
55+
#[get("/")]
56+
async fn hello() -> impl Responder {
57+
HttpResponse::Ok().body("Hello world!")
58+
}
59+
60+
#[post("/echo")]
61+
async fn echo(req_body: String) -> impl Responder {
62+
HttpResponse::Ok().body(req_body)
63+
}
64+
65+
async fn manual_hello() -> impl Responder {
66+
HttpResponse::Ok().body("Hey there!")
67+
}</code></pre>
68+
<p>각 핸들러는 HTTP 메소드와 경로에 따라 요청을 처리합니다.</p>
69+
<p>수동으로 경로를 설정하고 싶다면, 그에 맞는 함수를 작성하면 됩니다.</p>
70+
<h2 id="app">App 생성 및 요청 핸들러 등록</h2>
71+
<p>다음 단계로, App 인스턴스를 생성하고 요청 핸들러를 등록합니다.</p>
72+
<p>경로 정보가 있는 핸들러는 <code>App::service</code>를 사용하고, 수동으로 경로를 설정한 핸들러는 <code>App::route</code>를 사용합니다.</p>
73+
<pre><code class="rust">#[actix_web::main]
74+
async fn main() -> std::io::Result&lt;()> {
75+
HttpServer::new(|| {
76+
App::new()
77+
.service(hello)
78+
.service(echo)
79+
.route("/hey", web::get().to(manual_hello))
80+
})
81+
.bind(("127.0.0.1", 8080))?
82+
.run()
83+
.await
84+
}</code></pre>
85+
<h2 id="-2">서버 실행</h2>
86+
<p>이제 <code>cargo run</code> 명령어를 통해 프로그램을 컴파일하고 실행할 수 있습니다.</p>
87+
<p>웹 브라우저에서 http://127.0.0.1:8080/ 주소로 접속하면 'Hello World' 메시지를 확인할 수 있습니다.</p>
88+
<p>이 간단한 예제를 통해 Rust와 Actix-web을 이용하여 웹 서버를 어떻게 만드는지 배웠습니다.</p>
89+
<p>이러한 기본 원리를 이용하면 다양한 웹 서비스를 만들어볼 수 있습니다.</p>
4090
<div class="bottomnav">
4191
<span class="back"><a href="49_ko.html" rel="prev">❮ 이전</a></span>
42-
<span class="next"><a href="chapter_7_ko.html" rel="next">다음 ❯</a></span>
92+
<span class="next"><a href="51_ko.html" rel="next">다음 ❯</a></span>
4393
</div>
4494
</div>
45-
<div class="code"><center><img src="/ferris_lofi.png" alt="Rust Tutorial" width="80%" height="100%"></center></div>
95+
<div class="code"><center><img src="/8080.png" alt="Rust Tutorial" width="80%" height="100%"></center></div>
4696
</div>
4797
<script>
4898
var pres = document.querySelectorAll("pre>code");

docs/51_ko.html

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<!DOCTYPE html>
2+
<html lang="ko">
3+
<head>
4+
<title>Rust 튜토리얼 - 자기주도프로젝트</title>
5+
6+
<meta charset="UTF-8">
7+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
8+
<meta content="utf-8" http-equiv="encoding">
9+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
10+
<meta name="keywords" content="Rust, Programming, Learning">
11+
<meta name="description" content="Rust tutorial website based on tour_of_rust by 최석원">
12+
<meta name="theme-color" content="#ff6801"/>
13+
<meta http-equiv="Cache-Control" content="max-age=3600">
14+
15+
<link rel="stylesheet" href="tour.css">
16+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/night-owl.min.css">
17+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css">
18+
19+
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
20+
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
21+
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
22+
<link rel="/manifest" href="./site.webmanifest">
23+
24+
<script src="//unpkg.com/@highlightjs/cdn-assets@11.7.0/highlight.min.js"></script>
25+
26+
<script src="./tour.js" defer></script>
27+
<!-- <script>hljs.highlightAll();</script> -->
28+
<script src="./highlight.badge.min.js"></script>
29+
</head>
30+
<body>
31+
<div class="tour">
32+
<div class="header">
33+
<span class="title"><a href="index.html">Rust 튜토리얼</a></span>
34+
<span class="nav">
35+
<span class="toc"><a href="TOC_ko.html">목차</a></span>
36+
</div>
37+
<div class="page">
38+
<h1>main() && impl Responder</h1>
39+
<h2 id="actixrsmain">actix-rs main()</h2>
40+
<p>아래는 웹 서버를 시작하는 역할을 합니다. </p>
41+
<pre><code class="rust">#[actix_web::main]
42+
async fn main() -> std::io::Result&lt;()> {
43+
HttpServer::new(|| {
44+
App::new()
45+
.service(hello)
46+
.service(echo)
47+
.route("/hey", web::get().to(manual_hello))
48+
})
49+
.bind(("127.0.0.1", 8080))? // localhost:8080 or 127.0.0.1:8080
50+
.run()
51+
.await
52+
}</code></pre>
53+
<p>여기서 <code>#[actix_web::main]</code>은 Actix 웹 프레임워크에서 제공하는 매크로로, 이를 이용해 비동기 메인 함수를 생성할 수 있습니다. </p>
54+
<p>그리고 <code>HttpServer::new</code> 함수를 호출하여 새로운 HTTP 서버 인스턴스를 만듭니다.</p>
55+
<p>이 함수의 인자로는 클로저가 들어가며, 이 클로저 내부에서 App 인스턴스를 만들고,</p>
56+
<p>우리가 앞서 정의한 hello, echo, 그리고 manual_hello 함수를 각각 서비스로 등록합니다.</p>
57+
<p>또한, <code>/hey</code> 라는 경로는 수동으로 설정된 경로입니다.</p>
58+
<p><code>web::get().to(manual_hello)</code>를 통해 get 요청이 들어왔을 때 <code>manual_hello</code> 함수가 호출되도록 설정했습니다.</p>
59+
<p>그 후 <code>.bind(("127.0.0.1", 8080))?</code>를 통해 서버를 로컬 호스트의 8080 포트에 바인딩하게 됩니다.</p>
60+
<p>만약 바인딩에 실패하면 에러를 반환하며 프로그램은 종료됩니다.</p>
61+
<p>마지막으로 <code>.run().await</code>를 통해 서버를 실행시키며, 이 서버는 비동기적으로 작동하게 됩니다.</p>
62+
<h2 id="implresponder">impl Responder</h2>
63+
<p>Rust의 'Trait'에 대해 알아보고, 특히 actix-web에서 제공하는 'Responder' Trait에 대해 살펴보겠습니다.</p>
64+
<p>Responder는 웹 응답을 만드는데 굉장히 중요한 역할을 하는 Trait입니다.</p>
65+
<h2 id="trait">Trait 이란?</h2>
66+
<p>Rust에서 Trait는 특정 기능이나 행동을 정의한 것으로,</p>
67+
<p>Trait를 구현하면 해당 구조체나 열거형은 Trait에서 정의된 메소드를 사용할 수 있게 됩니다.</p>
68+
<p>Rust는 상속 대신 Trait를 사용하여 코드의 재사용성과 모듈성을 증가시킵니다.</p>
69+
<h2 id="respondertrait">Responder Trait</h2>
70+
<p>Responder는 actix-web에서 제공하는 Trait 중 하나로, HTTP 응답을 생성하는 메소드를 제공합니다.</p>
71+
<p>이 Trait를 이용하면 웹 서버가 클라이언트에게 보내는 HTTP 응답을 쉽게 만들 수 있습니다. </p>
72+
<p>Responder Trait은 두 가지 메소드를 정의합니다: </p>
73+
<ol>
74+
<li><code>respond_to</code>: HttpRequest 객체를 받아 HttpResponse를 생성하는 메소드로, 이는 핸들러에서 클라이언트의 요청을 받아 적절한 응답을 생성하는 데 사용됩니다.</li>
75+
<li><code>customize</code>: 응답을 커스터마이징 할 수 있는 메소드로, 이 메소드는 Responder가 구현되어 있는 경우 사용할 수 있습니다.</li>
76+
</ol>
77+
<h2 id="responder">Responder 사용하기</h2>
78+
<p>핸들러 함수에서는 <code>impl Responder</code>를 리턴 타입으로 사용합니다.</p>
79+
<p>이렇게 하면 어떤 값이든, 그 값이 Responder Trait를 구현하고 있다면 리턴할 수 있게 됩니다.</p>
80+
<p>예를 들어, 'String', 'HttpResponse' 등은 모두 Responder를 구현하고 있습니다.</p>
81+
<p>이를 통해 해당 값을 리턴하는 핸들러를 쉽게 만들 수 있습니다.</p>
82+
<p>Rust의 강력한 타입 시스템 덕분에 컴파일 타임에 각 핸들러가 어떤 타입의 값을 리턴하는지를 확인할 수 있게 되어,</p>
83+
<p>런타임 에러를 사전에 방지하는 데 매우 유용합니다.</p>
84+
<p>결국, Responder는 웹 응답을 만드는 과정을 추상화하고, 다양한 타입의 값을 HTTP 응답으로 쉽게 변환할 수 있게 해주는 역할을 합니다.</p>
85+
<p>이를 통해 강력하면서도 유연한 웹 응답을 만들 수 있게 됩니다.</p>
86+
<p>Responder를 활용하면 웹 서버 개발이 훨씬 편리해집니다.</p>
87+
<div class="bottomnav">
88+
<span class="back"><a href="50_ko.html" rel="prev">❮ 이전</a></span>
89+
<span class="next"><a href="52_ko.html" rel="next">다음 ❯</a></span>
90+
</div>
91+
</div>
92+
<div class="code"><center><img src="/8080.png" alt="Rust Tutorial" width="80%" height="100%"></center></div>
93+
</div>
94+
<script>
95+
var pres = document.querySelectorAll("pre>code");
96+
for (var i = 0; i < pres.length; i++) {
97+
hljs.highlightElement(pres[i]);
98+
}
99+
var options = {
100+
loadDelay: 0,
101+
copyIconClass: "far fa-clipboard",
102+
checkIconClass: "fa fa-check text-success",
103+
blogURL: "http://rust-study.ajousw.kr/"
104+
};
105+
window.highlightJsBadge(options);
106+
</script>
107+
108+
<footer>
109+
<p><a target="_blank" rel="noopener" href="https://www.youtube.com/c/SoftwareToolTime">아주대학교 Software Tool Time</a> - Rust 튜토리얼 (Basic)</p>
110+
</footer>
111+
</body>
112+
</html>

docs/52_ko.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<html lang="ko">
3+
<head>
4+
<title>Rust 튜토리얼 - 자기주도프로젝트</title>
5+
6+
<meta charset="UTF-8">
7+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
8+
<meta content="utf-8" http-equiv="encoding">
9+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
10+
<meta name="keywords" content="Rust, Programming, Learning">
11+
<meta name="description" content="Rust tutorial website based on tour_of_rust by 최석원">
12+
<meta name="theme-color" content="#ff6801"/>
13+
<meta http-equiv="Cache-Control" content="max-age=3600">
14+
15+
<link rel="stylesheet" href="tour.css">
16+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/night-owl.min.css">
17+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css">
18+
19+
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
20+
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
21+
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
22+
<link rel="/manifest" href="./site.webmanifest">
23+
24+
<script src="//unpkg.com/@highlightjs/cdn-assets@11.7.0/highlight.min.js"></script>
25+
26+
<script src="./tour.js" defer></script>
27+
<!-- <script>hljs.highlightAll();</script> -->
28+
<script src="./highlight.badge.min.js"></script>
29+
</head>
30+
<body>
31+
<div class="tour">
32+
<div class="header">
33+
<span class="title"><a href="index.html">Rust 튜토리얼</a></span>
34+
<span class="nav">
35+
<span class="toc"><a href="TOC_ko.html">목차</a></span>
36+
</div>
37+
<div class="page">
38+
<h1>kakao-rs</h1>
39+
<p>카카오톡 API 템플릿을 쉽게 만들어 주는 <code>kakao-rs</code> 라이브러리에 대해 알아보겠습니다. </p>
40+
<h2 id="kakaors">kakao-rs 라이브러리란?</h2>
41+
<p>kakao-rs는 Rust 언어로 작성된 카카오 챗봇 서버를 만들 때 사용할 수 있는 라이브러리입니다.</p>
42+
<p>이 라이브러리는 SimpleText, SimpleImage, ListCard, Carousel, BasicCard, CommerceCard, ItemCard 등의 JSON 데이터를 쉽게 생성할 수 있도록 돕는 도구들을 제공합니다.</p>
43+
<h2 id="">사용 방법</h2>
44+
<p>kakao-rs 라이브러리를 사용하려면, 먼저 프로젝트의 <code>Cargo.toml</code> 파일에 kakao-rs를 의존성으로 추가해야 합니다. </p>
45+
<pre><code class="toml">[dependencies]
46+
kakao-rs = "0.3"</code></pre>
47+
<p>이 라이브러리를 이용하면 다양한 종류의 버튼(예: 공유 버튼, 링크 버튼, 일반 메시지 버튼, 전화 버튼 등)을 쉽게 만들 수 있습니다.</p>
48+
<h2 id="json">카카오 JSON 데이터 연동</h2>
49+
<p>kakao-rs는 카카오 JSON 데이터와의 연동이 매우 간단합니다.</p>
50+
<p>유저의 발화문을 얻기 위해서는 아래와 같이 작성하면 됩니다.</p>
51+
<pre><code class="rust">#[post("/end")]
52+
pub async fn test(kakao: web::Json&lt;Value>) -> impl Responder { // actix
53+
println!("{}", kakao["userRequest"]["utterance"].as_str().unwrap()); // 발화문
54+
unimplemented!()
55+
}</code></pre>
56+
<p>이 라이브러리를 이용하면 다양한 형태의 카카오 챗봇 메시지를 쉽게 생성할 수 있습니다.</p>
57+
<p>예를 들어, ListCard를 생성하는 코드는 아래와 같습니다.</p>
58+
<pre><code class="rust">let mut list_card = ListCard::new("리스트 카드 제목!"); // 제목
59+
// ...
60+
result.add_output(list_card.build()); // moved list_card's ownership</code></pre>
61+
<p>kakao-rs 라이브러리를 통해 SimpleText, SimpleImage, BasicCard, CommerceCard, Carousel 등의</p>
62+
<p>다양한 형태의 카카오 챗봇 메시지를 쉽게 생성할 수 있습니다.</p>
63+
<p>카카오 챗봇 서버를 Rust로 구현하려는 개발자들에게 kakao-rs 라이브러리는 매우 유용한 도구가 될 것입니다.</p>
64+
<div class="bottomnav">
65+
<span class="back"><a href="51_ko.html" rel="prev">❮ 이전</a></span>
66+
<span class="next"><a href="53_ko.html" rel="next">다음 ❯</a></span>
67+
</div>
68+
</div>
69+
<div class="code"><center><img src="/ferris_lofi.png" alt="Rust Tutorial" width="80%" height="100%"></center></div>
70+
</div>
71+
<script>
72+
var pres = document.querySelectorAll("pre>code");
73+
for (var i = 0; i < pres.length; i++) {
74+
hljs.highlightElement(pres[i]);
75+
}
76+
var options = {
77+
loadDelay: 0,
78+
copyIconClass: "far fa-clipboard",
79+
checkIconClass: "fa fa-check text-success",
80+
blogURL: "http://rust-study.ajousw.kr/"
81+
};
82+
window.highlightJsBadge(options);
83+
</script>
84+
85+
<footer>
86+
<p><a target="_blank" rel="noopener" href="https://www.youtube.com/c/SoftwareToolTime">아주대학교 Software Tool Time</a> - Rust 튜토리얼 (Basic)</p>
87+
</footer>
88+
</body>
89+
</html>

0 commit comments

Comments
 (0)