Browse Source

Don't include port if port is 80

master
Dylan Baker 5 years ago
parent
commit
01cc55fa86
2 changed files with 13 additions and 9 deletions
  1. 4
    4
      src/cli.rs
  2. 9
    5
      src/client.rs

+ 4
- 4
src/cli.rs View File

@@ -2,7 +2,7 @@ use clap::ArgMatches;
2 2
 use std::fs;
3 3
 use std::io;
4 4
 
5
-use crate::client::{create_snippet, OutgoingSnippet, ServerConfig};
5
+use crate::client::{build_url_from_config, create_snippet, OutgoingSnippet, ServerConfig};
6 6
 
7 7
 fn get_snippet_from_matches(matches: &ArgMatches) -> io::Result<OutgoingSnippet> {
8 8
     let title = matches.value_of("name").unwrap();
@@ -38,13 +38,13 @@ fn get_server_config_from_matches(matches: &ArgMatches) -> ServerConfig {
38 38
 
39 39
 pub fn new_snippet(matches: &ArgMatches) -> io::Result<()> {
40 40
     let snippet = get_snippet_from_matches(matches)?;
41
-    println!("{:?}", snippet);
42 41
     let config = get_server_config_from_matches(matches);
42
+    let url = build_url_from_config(&config);
43 43
 
44 44
     match create_snippet(snippet, &config) {
45 45
         Ok(resp) => println!(
46
-            "Success! Your new snippet is available at {}://{}:{}/snippets/{}",
47
-            config.protocol, config.hostname, config.port, resp.id
46
+            "Success! Your new snippet is available at {}/snippets/{}",
47
+            url, resp.id
48 48
         ),
49 49
         Err(err) => println!("{}", err),
50 50
     }

+ 9
- 5
src/client.rs View File

@@ -24,19 +24,23 @@ pub struct ServerConfig {
24 24
     pub port: i32,
25 25
 }
26 26
 
27
+pub fn build_url_from_config(config: &ServerConfig) -> String {
28
+    match config.port {
29
+        80 => format!("{}://{}", config.protocol, config.hostname),
30
+        _ => format!("{}://{}:{}", config.protocol, config.hostname, config.port),
31
+    }
32
+}
33
+
27 34
 pub fn create_snippet(
28 35
     snippet: OutgoingSnippet,
29 36
     config: &ServerConfig,
30 37
 ) -> Result<IncomingSnippet, Box<std::error::Error>> {
31 38
     let client = reqwest::Client::new();
39
+    let url = build_url_from_config(config);
32 40
     let resp: IncomingSnippet = client
33
-        .post(&format!(
34
-            "{}://{}:{}/api/snippets",
35
-            config.protocol, config.hostname, config.port,
36
-        ))
41
+        .post(&format!("{}/api/snippets", url))
37 42
         .json(&snippet)
38 43
         .send()?
39 44
         .json()?;
40
-
41 45
     Ok(resp)
42 46
 }

Loading…
Cancel
Save