summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/database/database.rs12
-rw-r--r--src/database/sql/initialize.sql35
-rw-r--r--src/database/sql/initialize_problems.sql39
-rw-r--r--src/main.rs29
4 files changed, 71 insertions, 44 deletions
diff --git a/src/database/database.rs b/src/database/database.rs
index ee9b6b9..90a54bc 100644
--- a/src/database/database.rs
+++ b/src/database/database.rs
@@ -284,6 +284,18 @@ impl Database {
Ok(())
}
+
+ pub fn initialize_problems(&self) -> Result<(), DatabaseError> {
+ static QUERY: &str = include_str!("sql/initialize_problems.sql");
+ let conn = self.pool
+ .get()
+ .map_err(|e| DatabaseError::Connection(e.to_string()))?;
+
+ conn.execute_batch(QUERY)
+ .map_err(|e| DatabaseError::Query(e.to_string()))?;
+
+ Ok(())
+ }
}
#[cfg(test)]
diff --git a/src/database/sql/initialize.sql b/src/database/sql/initialize.sql
index 3569fd5..5cbb645 100644
--- a/src/database/sql/initialize.sql
+++ b/src/database/sql/initialize.sql
@@ -21,38 +21,3 @@ CREATE TABLE IF NOT EXISTS submission (
code TEXT NOT NULL,
code_length INTEGER NOT NULL
);
-
-INSERT INTO problem (title, description) VALUES (
- 'Prime Numbers',
- 'A prime number is a positive integer, greater than 1, which is only divisible by 1 and itself. Write a program that prints out the first 1,000,000 consecutive prime numbers.'
-);
-
-INSERT INTO problem (title, description) VALUES (
- 'The Look-and-Say Seqeunce',
- 'Starting with the number 1, the look-and-say sequence is defined so that the next number describes the previous one. Since "1" consists of one "1", the next nummber in the sequence is 11 (one-one). Since 11 has two "1"s, the next number would be 21 (two-one). The sequence continues 1, 11, 21, 1211, 111221, 312211, and so on. Write a program that prints out the first 100 consecutive numbers of the look-and-say sequence.'
-);
-
-INSERT INTO problem (title, description) VALUES (
- 'Just Printing Numbers',
- 'For this problem, you just need to print out the first 1,000,000,000 positive integers. Sounds easy right? Well you need to print them in English, not as digits. So instead of "123", you should print "one hundred twenty-three".'
-);
-
-INSERT INTO problem (title, description) VALUES (
- 'Periodic Table',
- 'Write a program that prints out the names of the first 118 elements of the periodic table in alphabetical order, then prints them again in order by atomic number.'
-);
-
-INSERT INTO problem (title, description) VALUES (
- 'Draw an ASCII Cat',
- 'Create a program that draws a picture of a cat using ASCII characters. To make this problem interesting, your ASCII art should be drawn on a grid of at least 3600 ASCII characters. This grid could be 80&times;45 or 60&times;60 or even 900&times;4 if you can somehow draw a cat in that space. Keep in mind that emojis are <b>not</b> ASCII characters so don''t get any funny ideas. For an official list of all ASCII characters, refer to <a target="_blank" href="https://www.unicode.org/charts/PDF/U0000.pdf">this document</a>.'
-);
-
-INSERT INTO problem (title, description) VALUES (
- 'As Big a Number as Possible',
- 'This challenge is a bit different from the others. Instead of trying to make as small a program as possible, you are trying to make a program which prints out as big a number as possible in a fixed amount of code. Your source code should be no longer than 512 bytes and numbers should be printed in base 10. Before you get any funny ideas, keep in mind that your program has to finish running at some point; you can''t just print "9" in an infinite loop.'
-);
-
-INSERT INTO problem (title, description) VALUES (
- 'Quines',
- 'Named after the logician and philosopher Willard Van Orman Quine, a "quine" is a program which prints out its own source code. Write a "quine", that is a program which prints out exactly its own source code. You should be able to compile or run the output of your program itself and it should behave identically to the original program you made.'
-);
diff --git a/src/database/sql/initialize_problems.sql b/src/database/sql/initialize_problems.sql
new file mode 100644
index 0000000..f14b4c6
--- /dev/null
+++ b/src/database/sql/initialize_problems.sql
@@ -0,0 +1,39 @@
+INSERT INTO problem (title, description) VALUES (
+ 'Prime Numbers',
+ 'A prime number is a positive integer, greater than 1, which is only divisible by 1 and itself. Write a program that prints out the first 1,000,000 consecutive prime numbers.'
+);
+
+INSERT INTO problem (title, description) VALUES (
+ 'Digits of pi',
+ 'Defined by the ratio of any circle''s circumference to its diameter, the transcendental constant pi has fascinated mathematicians for millennia. Your job is to write a program which takes a non-negative integer <em>n</em> as input, and prints out the <em>n</em>th digit after the decimal place of pi as output. For instance, an input of 0 should yield 3, an input of 1 should yield 1, an input of 2 should yield 4, and so on.'
+);
+
+INSERT INTO problem (title, description) VALUES (
+ 'The Look-and-Say Seqeunce',
+ 'Starting with the number 1, the look-and-say sequence is defined so that the next number describes the previous one. Since "1" consists of one "1", the next nummber in the sequence is 11 (one-one). Since 11 has two "1"s, the next number would be 21 (two-one). The sequence continues 1, 11, 21, 1211, 111221, 312211, and so on. Write a program that prints out the first 100 consecutive numbers of the look-and-say sequence.'
+);
+
+INSERT INTO problem (title, description) VALUES (
+ 'Draw an ASCII Cat',
+ 'Create a program that draws a picture of a cat using ASCII characters. To make this problem interesting, your ASCII art should be drawn on a grid of at least 3600 ASCII characters. This grid could be 80&times;45 or 60&times;60 or even 900&times;4 if you can somehow draw a cat in that space. Keep in mind that emojis are <b>not</b> ASCII characters so don''t get any funny ideas. For an official list of all ASCII characters, refer to <a target="_blank" href="https://www.unicode.org/charts/PDF/U0000.pdf">this document</a>.'
+);
+
+INSERT INTO problem (title, description) VALUES (
+ 'Just Printing Numbers',
+ 'For this problem, you just need to print out the first 1,000,000,000 positive integers. Sounds easy right? Well you need to print them in English, not as digits. So instead of "123", you should print "one hundred twenty-three".'
+);
+
+INSERT INTO problem (title, description) VALUES (
+ 'Periodic Table',
+ 'Write a program that prints out the names of the first 118 elements of the periodic table in alphabetical order, then prints them again in order by atomic number.'
+);
+
+INSERT INTO problem (title, description) VALUES (
+ 'Calculator',
+ 'Write a program which takes as input a statement of arithmetic, like "(2 * -(5.4 + 3) / 2 - 1)" and prints out the correct answer according to the typical order of operations.'
+);
+
+INSERT INTO problem (title, description) VALUES (
+ 'Quines',
+ 'Named after the logician and philosopher Willard Van Orman Quine, a "quine" is a program which prints out its own source code. Write a "quine", that is a program which prints out exactly its own source code. You should be able to compile or run the output of your program itself and it should behave identically to the original program you made.'
+);
diff --git a/src/main.rs b/src/main.rs
index b446402..0fe906a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -27,19 +27,27 @@ struct AppState {
async fn main() {
let database = Database::new_in_memory().unwrap();
+
+ // initialize tables
database.initialize().unwrap();
- // TODO: generically load environment variables, this is tedious
+ // initialize problems if none exist yet
+ if let Ok(problems) = database.fetch_problems() {
+ if problems.len() == 0 { database.initialize_problems().unwrap(); }
+ }
+
let Ok(secret) = env::var("JWT_SECRET") else {
eprintln!("missing environment variable JWT_SECRET");
return;
};
-
let Ok(register_code) = env::var("REGISTER_CODE") else {
eprintln!("missing environment variable REGISTER_CODE");
return;
};
-
+ let Ok(listen_address) = env::var("LISTEN_ADDRESS") else {
+ eprintln!("missing environment variable LISTEN_ADDRESS");
+ return;
+ };
let Ok(admin_email) = env::var("ADMIN_EMAIL") else {
eprintln!("missing environment variable ADMIN_EMAIL");
return;
@@ -52,11 +60,14 @@ async fn main() {
eprintln!("missing environment variable ADMIN_PASSWORD");
return;
};
-
- let Ok(()) = register_admin(&database, &admin_email, &admin_username, &admin_password) else {
- eprintln!("failed to register admin user");
- return;
- };
+
+ if let Ok(None) = database.fetch_user_by_email(&admin_email) {
+ // register admin if admin user does not exist yet
+ let Ok(()) = register_admin(&database, &admin_email, &admin_username, &admin_password) else {
+ eprintln!("failed to register admin user");
+ return;
+ };
+ }
let state = AppState {
secret: secret,
@@ -80,7 +91,7 @@ async fn main() {
.nest_service("/static", static_files)
.with_state(state);
- let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
+ let listener = tokio::net::TcpListener::bind(listen_address).await.unwrap();
axum::serve(listener, app).await.unwrap();
}