-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (57 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"./db"
_ "./models"
"fmt"
"github.com/gin-gonic/gin" // 只有这样才能运行其他包里的方法 这个问题看 https://github.com/gin-contrib/cors/issues/21
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"log"
//"net/http"
//"context"
"position_api/apis"
"position_api/models"
//"time"
)
func getDBConnection() *gorm.DB {
db, err := db.Connect("localhost", "position", "yunfeng_db", "pssword")
db.SingularTable(true)
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Location{})
db.AutoMigrate(&models.Comment{})
db.AutoMigrate(&models.Like{})
fmt.Println("Connect ok")
if err != nil {
log.Fatal(err)
}
if err != nil {
panic(err.Error())
}
db.AutoMigrate()
return db
}
func initRouter() *gin.Engine {
router := gin.Default()
router.GET("/", apis.IndexApi)
router.POST("/wxlogin", apis.WXLogin)
//router.GET("/addlike", apis.AddLike)
//
//router.GET("/persons", GetPersonsApi
// )
//
//router.GET("/person/:id", GetPersonApi)
//
//router.PUT("/person/:id", ModPersonApi)
//
//router.DELETE("/person/:id", DelPersonApi)
return router
}
func main() {
gin.SetMode(gin.ReleaseMode)
db := getDBConnection()
fmt.Println("ok")
//db.Create(&like)
defer db.Close()
router := initRouter()
router.Run(":8001")
}