This code shows all topics of all units of the book like:
- Sending and receiving JSON
- Working with SQL migrations
- Managing background tasks
- Performing partial updates and using optimistic locking
- Permission-based authorization
- Controlling CORS requests
- Graceful shutdowns
- Exposing application metrics
- Automating build and deployment step
To install the code on your local machine, you need to install all the dependencies with the following command:
go mod tidy
Before running the project, you must create a MySQL database with Docker:
docker run --name MySQL -e MYSQL_ROOT_PASSWORD=@dmin1234 -e MYSQL_DATABASE=snippetbox -p 3306:3306 -d mysql:latest
It is recommended to create the tables and some sample data in your database:
-- Create a new UTF-8 `snippetbox` database.
CREATE DATABASE snippetbox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Create a new UTF-8 `test_snippetbox` database.
CREATE DATABASE test_snippetbox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Switch to using the `snippetbox` database.
USE snippetbox;
-- Create a `snippets` table.
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);
-- Add some dummy records (which we'll use in the next couple of chapters).
INSERT INTO snippets (title, content, created, expires) VALUES (
'An old silent pond',
'An old silent pond...\nA frog jumps into the pond,\nsplash! Silence again.\n\nβ Matsuo BashΕ',
UTC_TIMESTAMP(),
DATE_ADD(UTC_TIMESTAMP(), INTERVAL 365 DAY)
);
INSERT INTO snippets (title, content, created, expires) VALUES (
'Over the wintry forest',
'Over the wintry\nforest, winds howl in rage\nwith no leaves to blow.\n\nβ Natsume Soseki',
UTC_TIMESTAMP(),
DATE_ADD(UTC_TIMESTAMP(), INTERVAL 365 DAY)
);
INSERT INTO snippets (title, content, created, expires) VALUES (
'First autumn morning',
'First autumn morning\nthe mirror I stare into\nshows my father''s face.\n\nβ Murakami Kijo',
UTC_TIMESTAMP(),
DATE_ADD(UTC_TIMESTAMP(), INTERVAL 7 DAY)
);
CREATE TABLE sessions (
token CHAR(43) PRIMARY KEY,
data BLOB NOT NULL,
expiry TIMESTAMP(6) NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions (expiry);
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
);
ALTER TABLE users ADD CONSTRAINT users_uc_email UNIQUE (email);
You need to create certificates to run the project in HTTPS and create the tls path:
cd tls
go run "/C/Program Files/Go/src/crypto/tls/generate_cert.go" --rsa-bits=2048 --host=localhost
Well, we are done installing everything. We must execute the following command to run the project.
go run ./cmd/web
You can send application parameters if you need to configure other parameters.
- addr: Http network address exapmple (-addr 127.0.0.1:8080)
- dsn: MySQL data source (-dsn user:pass@localhost:1234/snippetbox?parseTime=true)
- debug: To enable debug mode.
.
βββ cmd π
β βββ web πΈοΈ
β βββ context.go π
β βββ handlers.go π
β βββ helpers.go π
β βββ main.go π π (Application entry point)
β βββ middleware.go π
β βββ routes.go π
β βββ templates.go π
βββ internal π
β βββ assert β
β β βββ assert.go π
β βββ models ποΈ
β β βββ errors.go π
β β βββ snippets.go π
β β βββ users.go π
β βββ validator βοΈ
β βββ validator.go π
βββ tls π
β βββ cert.pem π
β βββ key.pem π
βββ ui π₯οΈ
β βββ html π
β β βββ pages π
β β β βββ about.gohtml π
β β β βββ account.gohtml π
β β β βββ create.gohtml π
β β β βββ home.gohtml π
β β β βββ login.gohtml π
β β β βββ password.gohtml π
β β β βββ signup.gohtml π
β β β βββ view.gohtml π
β β βββ partials π
β β β βββ nav.gohtml π
β β βββ base.gohtml π
β βββ static π
β β βββ css π¨
β β β βββ main.css π
β β βββ img πΌοΈ
β β β βββ favicon.ico π
β β β βββ logo.png π
β β βββ js β¨
β β βββ main.js π
β βββ efs.go π
βββ go.mod π
βββ go.sum π
βββ README.md π
- Go (version 1.23 o lastest)
- Fork the project
- Create a branch for your feature (git checkout -b feature/new-feature)
- Make your changes and commit (git commit -am 'Add new feature')
- Push your changes to your fork (git push origin feature/new-feature)
- Open a Pull Request