Skip to content

jaymorelli96/Snippetbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snippetbox

Snippetbox share the same concept as the famous GitHub Gist where people can share snippets of text.

Table of Contents

Motivation

This application was created with the purpose of learning Go. I believe that only by combining theoretical knowledge with practical experience you can you truly understand it.

I find quite hard to find technical books that focus more on the pratical side, that is why when I came across Alex Edwards books I was over the moon.

After reading some reviews on r/golang about his books I was confident that would be a good investment and now I must say that I completely agree with the popular opnion!

Key Takeaways

  • HTML Templating
  • RESTful Routing
  • Configuration Management
  • Level logging and centralized error handling
  • MySQL Database
  • Middlewares
  • Session Management
  • Security (HTTPS,OWASP Secure Heards and CSRF)
  • Authentication and Authorization
  • Contexts
  • Testing

Setup

The easiest way to get the application up and running is using Docker (see Running on Docker for more details).

Assuming you wan't to run the application with Go and use your own database please follow the required steps in the next section Running with Go.

Running with Go

Requirements

  • Go 1.20
  • MySQL

For the database connection there are two options:

  1. Use the default configuration (expect to have MySQL running on port 3306 at localhost). When the application starts it will bootstrap the initial data, creating the necessary tables and user.
  2. Use your own DSN and set the flag dsn to point to a valid DSN value.

Running Snippetbox

At the root of the project run the following commands:

$ go mod tidy
$ go run ./cmd/web

Usage

The application is running on HTTPS. Eg: https://localhost:4000

Configuration Management

Snippetbox supports configuration management for the following flags:

  • addr - Port Address (default is ':4000')
  • dsn - DSN (default is 'web:lua@/snippetbox?parseTime=true')*
  • debug - Debug mode (default is false)

*It is very important to set 'parseTime=true', otherwise the mapping for dates may not work properly

Example:

$ go run ./cmd/web -debug -port=:8080 -dsn=john:pass@/mybox?parseTime=true

It will run on port 8080 with debug mode on and also the database connection will point to localhost user 'john' with a password of 'pass' for the schema 'mybox'

API

Method Pattern Action
GET / Display the home page
GET /snippet/view/:id Display a specific snippet
GET /snippet/create Display a HTML form for creating a new snippet
POST /snippet/create Create a new snippet
GET /user/signup Display a HTML form for signing up a new user
POST /user/signup Create a new user
GET /user/login Display a HTML form for logging in a user
POST /user/login Authenticate and login the user
POST /user/logout Logout the user
GET /static/*filepath Serve a specific static file

Running on Docker

---- SECTION IN PROGRESS ---

Assuming you have Docker daemon running you just need to run the following command:

$ docker-compose up

The application will be running on port :4000 by default.

Credits

Snippetbox is part of the book Let's Go by Alex Edwards. I highly recommend this book for someone who is coming from another programming language to Go as the intention of the book is to show a real example of how to implement an application using the idiomatic Go.

License

MIT License

Copyright (c) 2023 Jean Morelli

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

From the book Let's Go by Alex Edwards

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published