Giter VIP home page Giter VIP logo

base-golang-api's Introduction

base-golang-api

Base skeleton to create REST API using Golang

DISCLAIMER

This repo only provide base skeleton to create a REST API using
Golang, to save your time from building REST API from scratch.
You can modify all of processes based on your needs!

Dependencies

  • gorilla/mux : For routing
  • gorilla/handlers : For http middleware
  • alexsasharegan/dotenv : For env variables

Command Line Usages

go run --race *.go

Optional parameters:

  • -debug : Enable debug mode
  • -enablePrintStack : Enable to print stacktraces when panic happened
  • -address : Set your ip and port
  • -timeout : Set your write & read
  • -env : A path to your env file

Examples:

go run --race *.go -address=127.0.0.1:7000

Routes

All of your api controllers should be placed at handler/, then you need to register your controller in routes.go, example:

routes = append(routes, Route{"GET", "/hello", controller.HelloWorld})

And that's it. For now, i'm not think about sub router or others, but you can modify this route's process based on your needs.

If you area clone this repo into your own project's folder, you need to change routes.go at line 4 :

controller "base-golang-api/handler"

Example:

controller "myproject/handler"

Middlewares

Default middlewares:

  • Access logger : To log any of request and response access to your api
  • Content negotiator : To filter and spesify http header content types
  • Recovery : To prevent your api from crash if any panic happened

You can create your own middleware, just make sure that your function signature follow gorilla's middleware function:

type MiddlewareFunc func(http.Handler) http.Handler

base-golang-api's People

Contributors

hiraq avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.