aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2023-07-02 14:11:36 +0200
committerDennis Eriksen <d@ennis.no>2023-07-02 14:11:36 +0200
commitb7c6db49cb0f7be20b27407e261f731c888efe77 (patch)
tree6c20f7ee6e41664117e291a79765809fef6d3799 /src
parentdo sql inline (diff)
downloadpurl-rs-b7c6db49cb0f7be20b27407e261f731c888efe77.tar.gz
trying to simplify create
Diffstat (limited to 'src')
-rw-r--r--src/main.rs56
1 files changed, 30 insertions, 26 deletions
diff --git a/src/main.rs b/src/main.rs
index ca69b7f..050a9ec 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -162,39 +162,43 @@ fn main() -> std::io::Result<()> {
}
else if action == "create" {
- match req.query() {
- Query::None => respond(303, "/purl.cgi"),
+ let url:&str = match req.query() {
+ Query::None => "Error, no url in query",
+ Query::Err(_e) => "Error reading query string",
Query::Some(map) => {
- if map.iter().count() != 1 {
- respond(400, "Incorrect number of query items");
+ if !map.contains_key("url") {
+ "Error, no url in query"
+ } else {
+ &map["url"]
}
- let url:&str = &map["url"];
- if !Url::parse(url).is_ok() {
- respond(400, "Invalid url");
- }
-
- let user:&str = req.var("REMOTE_USER").unwrap_or("none");
- let mut short:String = gen_short();
+ },
+ };
+ if url.starts_with("Error") {
+ respond(400, url);
+ } else if !Url::parse(url).is_ok() {
+ respond(400, "Invalid url");
+ }
- for i in 1..5 {
- match db.query_opt("SELECT url FROM shorts WHERE short = $1 LIMIT 1", &[&short]).unwrap() {
- Some(_row) => short = gen_short(), // If a row was returned, the short was not unique. Continue loop
- None => break, // If nothing was returned, the short IS unique. Break out of loop
- }
+ let user:&str = req.var("REMOTE_USER").unwrap_or("none");
- // Throw error if we couldn't create a unique short in fire tries
- if i == 5 { respond(500, "Could not find unique short"); }
- }
+ let mut short:String = gen_short();
- match db.execute("INSERT INTO shorts (url, short, created_by) VALUES ($1, $2, $3);", &[&url, &short, &user]) {
- Ok(_v) => respond(200, &format!("{}{}", shortprefix, short)),
- Err(_e) => respond(500, "Could not save shortened url to database"),
- };
- exit(0);
+ for i in 1..5 {
+ match db.query_opt("SELECT url FROM shorts WHERE short = $1 LIMIT 1", &[&short]).unwrap() {
+ Some(_row) => short = gen_short(), // If a row was returned, the short was not unique. Continue loop
+ None => break, // If nothing was returned, the short IS unique. Break out of loop
+ }
- },
- Query::Err(_e) => respond(400,"Error reading query string"),
+ // Throw error if we couldn't create a unique short in fire tries
+ if i == 5 { respond(500, "Could not find unique short"); }
}
+
+ match db.execute("INSERT INTO shorts (url, short, created_by) VALUES ($1, $2, $3);", &[&url, &short, &user]) {
+ Ok(_v) => respond(200, &format!("{}{}", shortprefix, short)),
+ Err(_e) => respond(500, "Could not save shortened url to database"),
+ };
+ exit(0);
+
}
else {