1. 程式人生 > >從golang-gin-realworld-example-app專案學寫httpapi (七)

從golang-gin-realworld-example-app專案學寫httpapi (七)

https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/hello.go

main呼叫

package main

import (
    "fmt"

    "gopkg.in/gin-gonic/gin.v1"

    "github.com/jinzhu/gorm"
    "github.com/wangzitian0/golang-gin-starter-kit/common"
    "github.com/wangzitian0/golang-gin-starter-kit/users"
)

func Migrate(db *gorm.DB) {
    //users.AutoMigrate()
    db.AutoMigrate(&users.UserModel{})
    db.AutoMigrate(&users.FollowModel{})
}

func main() {
    db := common.Init()
    Migrate(db)
    defer db.Close()

    r := gin.Default()

    v1 := r.Group("/api")
    v1.Use(users.AuthMiddleware(true))
    users.UsersRegister(v1.Group("/users"))
    users.UserRegister(v1.Group("/user"))
    users.ProfileRegister(v1.Group("/profiles"))

    r.Run() // listen and serve on 0.0.0.0:8080
}