- Create, Read, Update, Delete snippets.
- Authentication and Authorization based on sessions.
- Middleware implementation for recovering from panic, logging, setting secure headers, managing sessions, preventing CSRF attacks, and authentication.
- Persisting data using a MySQL database.
- Advanced error handling.
- Displaying dynamic data from the MySQL database and creating a template cache to improve read performance.
$ make docker.image
or
$ docker pull mysql
$ make docker.run
or
$ docker run --name [mysql-name] -p 3306:3306 -e MYSQL_ROOT_PASSWORD=[my-secret-pw] -d mysql:[tag]
$ make docker.exec
or
$ docker exec -it [mysql-name] mysql -u root -p
CREATE DATABASE snippetbox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE snippets (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
created DATETIME NOT NULL,
expires DATETIME NOT NULL
);
-- Add an index on the created column
CREATE INDEX idx_snippets_created ON snippets(created);
CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
hashed_password CHAR(60) NOT NULL,
created DATETIME NOT NULL,
CONSTRAINT unique_email UNIQUE (email)
);
CREATE USER 'web'@'localhost';
GRANT SELECT, INSERT, UPDATE, DELETE ON snippetbox.* TO 'web'@'localhost';
-- Important: Make sure to swap 'pass' with a password of your own choosing
ALTER USER 'web'@'localhost' IDENTIFIED BY 'pass';
$ docker exec -it [mysql-name] mysql -D snippetbox -u web -p
Enter password: [secret]
mysql>
mysql> SELECT id, title, expires FROM snippets;
+----+------------------------+---------------------+
| id | title | expires |
+----+------------------------+---------------------+
| 1 | An old silent pond | 2023-04-05 07:20:05 |
| 2 | Over the wintry forest | 2023-04-05 07:20:05 |
| 3 | First autumn morning | 2022-04-12 07:20:05 |
+----+------------------------+---------------------+
3 rows in set (0.00 sec)
mysql> DROP TABLE snippets;
ERROR 1142 (42000): DROP command denied to user 'web'@'localhost' for table 'snippets'
Run the application on the default port :4000
$ make run
$ make test