Browse Source

Initial skeleton

master
Toromino 5 months ago
commit
e66485545e
Signed by: Foxhkron <foxhkron@toromino.de> GPG Key ID: BF85EE6A14C3CE36
8 changed files with 354 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 224
    0
      Cargo.lock
  3. 8
    0
      Cargo.toml
  4. 21
    0
      LICENSE
  5. 3
    0
      README.md
  6. 4
    0
      sample.env.toml
  7. 77
    0
      src/main.rs
  8. 15
    0
      static/public/index

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+/target
2
+**/*.rs.bk

+ 224
- 0
Cargo.lock View File

@@ -0,0 +1,224 @@
1
+# This file is automatically @generated by Cargo.
2
+# It is not intended for manual editing.
3
+[[package]]
4
+name = "aho-corasick"
5
+version = "0.7.3"
6
+source = "registry+https://github.com/rust-lang/crates.io-index"
7
+dependencies = [
8
+ "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
9
+]
10
+
11
+[[package]]
12
+name = "config"
13
+version = "0.9.2"
14
+source = "registry+https://github.com/rust-lang/crates.io-index"
15
+dependencies = [
16
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
17
+ "nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
18
+ "rust-ini 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
19
+ "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)",
20
+ "serde-hjson 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
21
+ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
22
+ "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
23
+ "yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
24
+]
25
+
26
+[[package]]
27
+name = "itoa"
28
+version = "0.4.4"
29
+source = "registry+https://github.com/rust-lang/crates.io-index"
30
+
31
+[[package]]
32
+name = "lazy_static"
33
+version = "0.2.11"
34
+source = "registry+https://github.com/rust-lang/crates.io-index"
35
+
36
+[[package]]
37
+name = "lazy_static"
38
+version = "1.3.0"
39
+source = "registry+https://github.com/rust-lang/crates.io-index"
40
+
41
+[[package]]
42
+name = "linked-hash-map"
43
+version = "0.3.0"
44
+source = "registry+https://github.com/rust-lang/crates.io-index"
45
+dependencies = [
46
+ "serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)",
47
+ "serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)",
48
+]
49
+
50
+[[package]]
51
+name = "linked-hash-map"
52
+version = "0.5.2"
53
+source = "registry+https://github.com/rust-lang/crates.io-index"
54
+
55
+[[package]]
56
+name = "memchr"
57
+version = "2.2.0"
58
+source = "registry+https://github.com/rust-lang/crates.io-index"
59
+
60
+[[package]]
61
+name = "nom"
62
+version = "4.2.3"
63
+source = "registry+https://github.com/rust-lang/crates.io-index"
64
+dependencies = [
65
+ "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
66
+ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
67
+]
68
+
69
+[[package]]
70
+name = "num-traits"
71
+version = "0.1.43"
72
+source = "registry+https://github.com/rust-lang/crates.io-index"
73
+dependencies = [
74
+ "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
75
+]
76
+
77
+[[package]]
78
+name = "num-traits"
79
+version = "0.2.6"
80
+source = "registry+https://github.com/rust-lang/crates.io-index"
81
+
82
+[[package]]
83
+name = "phlogger"
84
+version = "0.1.0"
85
+dependencies = [
86
+ "config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
87
+]
88
+
89
+[[package]]
90
+name = "regex"
91
+version = "1.1.6"
92
+source = "registry+https://github.com/rust-lang/crates.io-index"
93
+dependencies = [
94
+ "aho-corasick 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
95
+ "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
96
+ "regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)",
97
+ "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
98
+ "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
99
+]
100
+
101
+[[package]]
102
+name = "regex-syntax"
103
+version = "0.6.6"
104
+source = "registry+https://github.com/rust-lang/crates.io-index"
105
+dependencies = [
106
+ "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
107
+]
108
+
109
+[[package]]
110
+name = "rust-ini"
111
+version = "0.13.0"
112
+source = "registry+https://github.com/rust-lang/crates.io-index"
113
+
114
+[[package]]
115
+name = "ryu"
116
+version = "0.2.8"
117
+source = "registry+https://github.com/rust-lang/crates.io-index"
118
+
119
+[[package]]
120
+name = "serde"
121
+version = "0.8.23"
122
+source = "registry+https://github.com/rust-lang/crates.io-index"
123
+
124
+[[package]]
125
+name = "serde"
126
+version = "1.0.91"
127
+source = "registry+https://github.com/rust-lang/crates.io-index"
128
+
129
+[[package]]
130
+name = "serde-hjson"
131
+version = "0.8.2"
132
+source = "registry+https://github.com/rust-lang/crates.io-index"
133
+dependencies = [
134
+ "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
135
+ "linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
136
+ "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
137
+ "regex 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
138
+ "serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)",
139
+]
140
+
141
+[[package]]
142
+name = "serde_json"
143
+version = "1.0.39"
144
+source = "registry+https://github.com/rust-lang/crates.io-index"
145
+dependencies = [
146
+ "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
147
+ "ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
148
+ "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)",
149
+]
150
+
151
+[[package]]
152
+name = "serde_test"
153
+version = "0.8.23"
154
+source = "registry+https://github.com/rust-lang/crates.io-index"
155
+dependencies = [
156
+ "serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)",
157
+]
158
+
159
+[[package]]
160
+name = "thread_local"
161
+version = "0.3.6"
162
+source = "registry+https://github.com/rust-lang/crates.io-index"
163
+dependencies = [
164
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
165
+]
166
+
167
+[[package]]
168
+name = "toml"
169
+version = "0.4.10"
170
+source = "registry+https://github.com/rust-lang/crates.io-index"
171
+dependencies = [
172
+ "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)",
173
+]
174
+
175
+[[package]]
176
+name = "ucd-util"
177
+version = "0.1.3"
178
+source = "registry+https://github.com/rust-lang/crates.io-index"
179
+
180
+[[package]]
181
+name = "utf8-ranges"
182
+version = "1.0.2"
183
+source = "registry+https://github.com/rust-lang/crates.io-index"
184
+
185
+[[package]]
186
+name = "version_check"
187
+version = "0.1.5"
188
+source = "registry+https://github.com/rust-lang/crates.io-index"
189
+
190
+[[package]]
191
+name = "yaml-rust"
192
+version = "0.4.3"
193
+source = "registry+https://github.com/rust-lang/crates.io-index"
194
+dependencies = [
195
+ "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
196
+]
197
+
198
+[metadata]
199
+"checksum aho-corasick 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e6f484ae0c99fec2e858eb6134949117399f222608d84cadb3f58c1f97c2364c"
200
+"checksum config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e82d07fac0a5eeaa9d959b5194d01bb66e414665f547416958d2b430f8f4852"
201
+"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
202
+"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
203
+"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
204
+"checksum linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd"
205
+"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
206
+"checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39"
207
+"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
208
+"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
209
+"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
210
+"checksum regex 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8f0a0bcab2fd7d1d7c54fa9eae6f43eddeb9ce2e7352f8518a814a4f65d60c58"
211
+"checksum regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dcfd8681eebe297b81d98498869d4aae052137651ad7b96822f09ceb690d0a96"
212
+"checksum rust-ini 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2"
213
+"checksum ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b96a9549dc8d48f2c283938303c4b5a77aa29bfbc5b54b084fb1630408899a8f"
214
+"checksum serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8"
215
+"checksum serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)" = "a72e9b96fa45ce22a4bc23da3858dfccfd60acd28a25bcd328a98fdd6bea43fd"
216
+"checksum serde-hjson 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0b833c5ad67d52ced5f5938b2980f32a9c1c5ef047f0b4fb3127e7a423c76153"
217
+"checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d"
218
+"checksum serde_test 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "110b3dbdf8607ec493c22d5d947753282f3bae73c0f56d322af1e8c78e4c23d5"
219
+"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
220
+"checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f"
221
+"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
222
+"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
223
+"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
224
+"checksum yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d"

+ 8
- 0
Cargo.toml View File

@@ -0,0 +1,8 @@
1
+[package]
2
+name = "phlogger"
3
+version = "0.1.0"
4
+authors = ["Toromino <foxhkron@toromino.de>"]
5
+edition = "2018"
6
+
7
+[dependencies]
8
+config = "0.9.2"

+ 21
- 0
LICENSE View File

@@ -0,0 +1,21 @@
1
+MIT License
2
+
3
+Copyright (c) 2018 Dennis Buchholz
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.

+ 3
- 0
README.md View File

@@ -0,0 +1,3 @@
1
+# Phlogger
2
+
3
+Phlogger is a gopher server for Phlogs (Gopher Blogs).

+ 4
- 0
sample.env.toml View File

@@ -0,0 +1,4 @@
1
+[endpoint]
2
+address = "localhost"
3
+port = "70"
4
+

+ 77
- 0
src/main.rs View File

@@ -0,0 +1,77 @@
1
+extern crate config;
2
+
3
+use std::io::{Read, Write};
4
+use std::net::TcpListener;
5
+
6
+fn main() {
7
+    let listener = TcpListener::bind(format!(
8
+        "{domain}:{port}",
9
+        domain = get_config_value("endpoint.address"),
10
+        port = get_config_value("endpoint.port")
11
+    ))
12
+    .unwrap_or_else(|_| panic!("Listener could not be started!"));
13
+
14
+    for connection in listener.incoming() {
15
+        let mut handler = connection.unwrap();
16
+        let mut buffer = [0; 2048];
17
+
18
+        handler.read(&mut buffer).unwrap();
19
+        let mut route = String::from_utf8_lossy(&buffer[..]).to_string();
20
+        route = route[1..]
21
+            .replace("\r", "")
22
+            .replace("\n", "")
23
+            .replace(" ", "")
24
+            .trim_matches(char::from(0))
25
+            .to_string();
26
+
27
+        if route == "".to_string() {
28
+            route = String::from("index")
29
+        }
30
+
31
+        let file = match std::fs::read_to_string(format!("static/public/{}", &route)) {
32
+            Ok(body) => {
33
+                println!(
34
+                    "Matched route /{uri} with file 'static/public/{uri}'",
35
+                    uri = &route
36
+                );
37
+                body
38
+            }
39
+            Err(_) => std::fs::read_to_string(format!("static/public/{}/index", &route))
40
+                .unwrap_or_else(|_| String::from("Error: Page not found")),
41
+        };
42
+
43
+        let mut response: String = String::new();
44
+        for line in file.lines() {
45
+            let mut body: String = line.to_string();
46
+
47
+            if line.chars().next().unwrap() == '0' || line.chars().next().unwrap() == '1' {
48
+                body = line.replacen("/", "\t/", 1);
49
+            }
50
+
51
+            response = format!(
52
+                "{ancestors}{body}\t{address}\t{port}\r\n",
53
+                ancestors = response,
54
+                body = body,
55
+                address = get_config_value("endpoint.address"),
56
+                port = get_config_value("endpoint.port")
57
+            );
58
+        }
59
+
60
+        handler.write(response.as_bytes()).unwrap();
61
+        handler.flush().unwrap();
62
+    }
63
+}
64
+
65
+fn get_config_value(key: &str) -> String {
66
+    let mut config = config::Config::default();
67
+
68
+    config.merge(config::File::with_name("env.toml"));
69
+
70
+    match config.get_str(key) {
71
+        Ok(value) => value,
72
+        Err(_) => {
73
+            eprintln!("Config value '{}' not found", key);
74
+            return String::from("");
75
+        }
76
+    }
77
+}

+ 15
- 0
static/public/index View File

@@ -0,0 +1,15 @@
1
+i                 _____                          _
2
+i                |  ___|                        | |
3
+i                | |____  ____ _ _ __ ___  _ __ | | ___
4
+i                |  __\ \/ / _` | '_ ` _ \| '_ \| |/ _ \
5
+i                | |___>  < (_| | | | | | | |_) | |  __/
6
+i                \____/_/\_\__,_|_| |_| |_| .__/|_|\___|
7
+i                                             | |
8
+i                                             |_|
9
+i                 An example gopher blog run by $person
10
+i                     Last updated on: Jan 01, 1970
11
+i
12
+i..........SITEMAP..........
13
+0About /about
14
+0Blog /blog
15
+i

Loading…
Cancel
Save