Coder Social home page Coder Social logo

gin-boilerplate's Introduction

Gin boilerplate

A fast way to develop gin rest apis with golang

This includes authorization middleware and JWt middleware. It already has user service, but it's up to you to add other things.

What you need

Make sure you have all these golang packages.

go get github.com/jinzhu/gorm
go get github.com/dgrijalva/jwt-go
go get github.com/gin-gonic/gin
go get golang.org/x/crypto/bcrypt

After that you need a .env file which includes the connection string for the database and the port. Like so:

DB_CONFIG=user:password@/dbname?charset=utf8&parseTime=True&loc=Local
PORT=8080

You can change the database from the default MySQL to whatever, in the /database/database.go file. All the migrations are handled by gorm upon running main.go. After all this you can type go run main.go to start the api.

Adding things

To add things like models can be done by adding a new file to /database/models. For example let's add a post:

package models

import (
    "github.com/jinzhu/gorm"
    "github.com/nireo/gin-boilerplate/lib/common" // import the json data type alias
)

// Post data model
type Post struct {
    gorm.Model
    Title   string `sql:"type:text;"`
    Content string `sql:"type:text;"`
}

// Serialize the post data
func (p Post) Serialize() common.JSON {
    return common.JSON{
        "id":         p.ID,
        "content":    p.Content,
        "title":      p.Title,
        "created_at": p.CreatedAt,
    }
}

gin-boilerplate's People

Contributors

nireo avatar

Watchers

James Cloos 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.