sum28it / snippetBox

A web application for sharing text snippets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snippet Box

Introduction

This is a full stack CRUD web application written in GO. Users can create text snippets and share (like Github gists)on the web.

Features

  • Authentication: This application uses JWT to authenticate client requests and provide login/signup or logout features. Passwords are stoted securely after hashing on the client side.
  • Sessions: Session is a way of maintaining state information about a user's interactions with a website or web application. A session allows the server to keep track of information such as the user's login status and preferences. In this app we've used cookies(simple to implement) to store user sessions.
  • Middlewrare: A middleware is a function that comes in between the request-response cycle, does something and then calls the next middleware in line. In this application,they are used for authentication, logging and panic recovery.
  • Dependency injection(DI): It's one of the most common design pattern to provide dependencies to an object. Here, DI is used to provide handlers- db, logger and other dependencies.
  • Database: We've used PostgreSQL as our database to persist user and snippet data. PostgreSQL is open-source, simple and has lots of resources to get statred with.
  • Graceful Shutdown: Shutting down abruptly when something bad happens can cause unpleasant user experience, resource leakage and buffers being not flushed. So, graceful shutdown is implemented to let active requests terminate, buffer's flush and not to accept new connections.

Up and Running

Clone the repo:

git clone github.com/sum28it/snippetBox

Install postgreSQL from here. Create a database and a role for it's user. Grant privileges to the user using command

GRANT ALL PRIVILEGES ON SCHEMA public TO username;

Create the following tables to store data.


CREATE TABLE snippets (
  id SERIAL NOT NULL PRIMARY KEY,
  title VARCHAR(100) NOT NULL,
  content TEXT NOT NULL,
  created TIMESTAMP NOT NULL,
  expires TIMESTAMP NOT NULL
);

CREATE TABLE users (
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
hashed_password CHAR(60) NOT NULL,
created TIMESTAMP NOT NULL
);
ALTER TABLE users ADD CONSTRAINT users_uc_email UNIQUE (email);

Generate tls certificates by running:

go run tls_cert_gen/generate_cert.go

Update the dsn string to connect to postgres in main.go or, provide it as a command line flag.

Now, start the server using:

go run cmd/web/nmain.go --dsn="your_dsn_string"

Api Endpoints

GET: /
GET: /snippet/new
POST:/snippet/new
GET: /snippet/:id
GET: /user/signup
POST:/user/signup
GET: /user/login
POST:/user/login
POST: /user/logout

Useful Links-

About

A web application for sharing text snippets


Languages

Language:Go 65.2%Language:CSS 18.3%Language:HTML 16.2%Language:Makefile 0.2%