All Projects → dhax → Go Base

dhax / Go Base

Licence: mit
Go RESTful API Boilerplate with JWT Authentication backed by PostgreSQL

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Go Base

Python Api Development Fundamentals
Develop a full-stack web application with Python and Flask
Stars: ✭ 44 (-95.26%)
Mutual labels:  api, restful, jwt
Node Express Postgresql Sequelize
Node.js, Express.js, Sequelize.js and PostgreSQL RESTful API
Stars: ✭ 148 (-84.05%)
Mutual labels:  api, restful, postgres
Golang Gin Realworld Example App
Exemplary real world application built with Golang + Gin
Stars: ✭ 1,780 (+91.81%)
Mutual labels:  api, restful, jwt
Supra Api Nodejs
❤️ Node.js REST API boilerplate
Stars: ✭ 182 (-80.39%)
Mutual labels:  api, restful, jwt
Node Express Mongoose Passport Jwt Rest Api Auth
Node, express, mongoose, passport and JWT REST API authentication example
Stars: ✭ 146 (-84.27%)
Mutual labels:  api, restful, jwt
Snake
🐍 一款小巧的基于Go构建的开发框架,可以快速构建API服务或者Web网站进行业务开发,遵循SOLID设计原则
Stars: ✭ 615 (-33.73%)
Mutual labels:  api, restful, jwt
Full Stack
Full stack, modern web application generator. Using Flask, PostgreSQL DB, Docker, Swagger, automatic HTTPS and more.
Stars: ✭ 451 (-51.4%)
Mutual labels:  api, jwt
Typescript Rest
This is a lightweight annotation-based expressjs extension for typescript.
Stars: ✭ 458 (-50.65%)
Mutual labels:  api, restful
V8 Archive
Directus Database API — Wraps Custom SQL Databases with a REST/GraphQL API
Stars: ✭ 486 (-47.63%)
Mutual labels:  api, restful
Node Express Mongodb Jwt Rest Api Skeleton
This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API). Demo of frontend in VueJS here: https://github.com/davellanedam/vue-skeleton-mvp
Stars: ✭ 603 (-35.02%)
Mutual labels:  api, jwt
Koa Rest Api Boilerplate
💯 Boilerplate for Node.js Koa RESTful API application with Docker, Swagger, Jest, CodeCov and CircleCI
Stars: ✭ 420 (-54.74%)
Mutual labels:  api, restful
Fastapi React
🚀 Cookiecutter Template for FastAPI + React Projects. Using PostgreSQL, SQLAlchemy, and Docker
Stars: ✭ 501 (-46.01%)
Mutual labels:  postgres, jwt
Yet Another Rest Client
YARC (Yet Another REST Client) is an easy-to-use REST Client for Google Chrome.
Stars: ✭ 23 (-97.52%)
Mutual labels:  api, restful
Go Gin Example
An example of gin
Stars: ✭ 4,992 (+437.93%)
Mutual labels:  api, jwt
Jwt sessions
XSS/CSRF safe JWT auth designed for SPA
Stars: ✭ 431 (-53.56%)
Mutual labels:  api, jwt
Ngx Starter Kit
🅰️ Angular 🚀 NestJS 🐒 Starter Kit
Stars: ✭ 479 (-48.38%)
Mutual labels:  postgres, jwt
Jwtrefreshtokenbundle
Implements a Refresh Token system over Json Web Tokens in Symfony
Stars: ✭ 425 (-54.2%)
Mutual labels:  api, jwt
Beauty Vuejs Boilerplate
❤️ Real world base Vue.js app. Access/refresh tokens auth, api services, http client, vuex modules
Stars: ✭ 583 (-37.18%)
Mutual labels:  api, jwt
Nodeapi
Simple RESTful API implementation on Node.js + MongoDB.
Stars: ✭ 719 (-22.52%)
Mutual labels:  api, restful
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (-25.22%)
Mutual labels:  api, restful

Go Restful API Boilerplate

GoDoc Badge GoReportCard Badge

Easily extendible RESTful API boilerplate aiming to follow idiomatic go and best practice.

The goal of this boiler is to have a solid and structured foundation to build upon on.

Any feedback and pull requests are welcome and highly appreciated. Feel free to open issues just for comments and discussions.

Features

The following feature set is a minimal selection of typical Web API requirements:

  • Configuration using viper
  • CLI features using cobra
  • PostgreSQL support including migrations using go-pg
  • Structured logging with Logrus
  • Routing with chi router and middleware
  • JWT Authentication using jwt-go with example passwordless email authentication
  • Request data validation using ozzo-validation
  • HTML emails with gomail

Start Application

  • Clone this repository
  • Create a postgres database and set environment variables for your database accordingly if not using same as default
  • Run the application to see available commands: go run main.go
  • First initialize the database running all migrations found in ./database/migrate at once with command migrate: go run main.go migrate
  • Run the application with command serve: go run main.go serve

API Routes

Authentication

For passwordless login following routes are available:

Path Method Required JSON Header Description
/auth/login POST email the email you want to login with (see below)
/auth/token POST token the token you received via email (or printed to stdout if smtp not set)
/auth/refresh POST Authorization: "Bearer refresh_token" refresh JWTs
/auth/logout POST Authorizaiton: "Bearer refresh_token" logout from this device

Example API

Besides /auth/* the API provides two main routes /api/* and /admin/*, as an example to separate application and administration context. The latter requires to be logged in as administrator by providing the respective JWT in Authorization Header.

Check routes.md for a generated overview of the provided API routes.

Client API Access and CORS

The server is configured to serve a Progressive Web App (PWA) client from ./public folder (this repo only serves an example index.html, see below for a demo PWA client to put here). In this case enabling CORS is not required, because the client is served from the same host as the api.

If you want to access the api from a client that is serverd from a different host, including e.g. a development live reloading server with below demo client, you must enable CORS on the server first by setting environment variable ENABLE_CORS=true on the server to allow api connections from clients serverd by other hosts.

Demo client application

For demonstration of the login and account management features this API serves a demo Vue.js PWA. The client's source code can be found here. Build and put it into the api's ./public folder, or use the live development server (requires CORS enabled).

Outgoing emails containing the login token will be print to stdout if no valid email smtp settings are provided by environment variables (see table below). If EMAIL_SMTP_HOST is set but the host can not be reached the application will exit immediately at start.

Use one of the following bootstrapped users for login:

A deployed version can also be found on Heroku

Environment Variables

By default viper will look at $HOME/.go-base.yaml for a config file. Setting your config as Environment Variables is recommended as by 12-Factor App.

Name Type Default Description
PORT string localhost:3000 http address (accepts also port number only for heroku compability)
LOG_LEVEL string debug log level
LOG_TEXTLOGGING bool false defaults to json logging
DB_NETWORK string tcp database 'tcp' or 'unix' connection
DB_ADDR string localhost:5432 database tcp address or unix socket
DB_USER string postgres database user name
DB_PASSWORD string postgres database user password
DB_DATABASE string gobase database shema name
AUTH_LOGIN_URL string http://localhost:3000/login client login url as sent in login token email
AUTH_LOGIN_TOKEN_LENGTH int 8 length of login token
AUTH_LOGIN_TOKEN_EXPIRY time.Duration 11m login token expiry
AUTH_JWT_SECRET string random jwt sign and verify key - value "random" creates random 32 char secret at startup (and automatically invalidates existing tokens on app restarts, so during dev you might want to set a fixed value here)
AUTH_JWT_EXPIRY time.Duration 15m jwt access token expiry
AUTH_JWT_REFRESH_EXPIRY time.Duration 1h jwt refresh token expiry
EMAIL_SMTP_HOST string email smtp host (if set and connection can't be established then app exits)
EMAIL_SMTP_PORT int email smtp port
EMAIL_SMTP_USER string email smtp username
EMAIL_SMTP_PASSWORD string email smtp password
EMAIL_FROM_ADDRESS string from address used in sending emails
EMAIL_FROM_NAME string from name used in sending emails
ENABLE_CORS bool false enable CORS requests

Testing

Package auth/pwdless contains example api tests using a mocked database.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].