Skip to content

AguilaMike/snippetbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Sep 4, 2024
ed4d8eb Β· Sep 4, 2024

History

27 Commits
Sep 4, 2024
Sep 4, 2024
Sep 4, 2024
Sep 4, 2024
Sep 3, 2024
Sep 4, 2024
Sep 4, 2024
Sep 3, 2024

Repository files navigation

Snippetbox: Supporting Material for the "Let's Go" Book πŸ“–


This code shows all topics of all units of the book like:

  1. Sending and receiving JSON
  2. Working with SQL migrations
  3. Managing background tasks
  4. Performing partial updates and using optimistic locking
  5. Permission-based authorization
  6. Controlling CORS requests
  7. Graceful shutdowns
  8. Exposing application metrics
  9. Automating build and deployment step

Table of Contents πŸ“‹

Installation πŸ› οΈ

Install dependencies

To install the code on your local machine, you need to install all the dependencies with the following command:

go mod tidy

Install database

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

Create tables

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);

Create certificates

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

Usage πŸš€

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.

Project Structure πŸ“‚

.
β”œβ”€β”€ 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 πŸ“„

Prerequisites βœ”οΈ

  • Go (version 1.23 o lastest)

Contribute 🀝

  • 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

About

Code of the book Let's Go. This book you can get in https://lets-go.alexedwards.net

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published