This commit is contained in:
Jay 2018-04-12 17:41:21 +08:00
parent 078461b8cd
commit 5484da1212
5 changed files with 65 additions and 4 deletions

0
config.yml Normal file
View File

View File

@ -1,6 +1,9 @@
package main
import (
"log"
"git.trj.tw/golang/go-gallery/models"
"git.trj.tw/golang/go-gallery/routers/routes"
"github.com/gin-gonic/gin"
)
@ -9,7 +12,10 @@ var server *gin.Engine
func main() {
server = routes.NewServ()
_, err := models.NewDB()
if err != nil {
log.Fatal(err)
}
routes.SetDefaultRoutes(server)
server.Run(":10230")

29
models/account.go Normal file
View File

@ -0,0 +1,29 @@
package models
import (
"time"
)
// Account - Account table struct
type Account struct {
ID string `xorm:"id"`
Account string `xorm:"account"`
Password string `xorm:"password"`
Nick string `xorm:"nick"`
Email string `xorm:"email"`
Ctime time.Time `xorm:"ctime"`
Mtime time.Time `xorm:"mtime"`
}
// GetAllAccount - all account
func GetAllAccount() ([]Account, error) {
var accs []Account
err := x.Table("account").Find(&accs)
return accs, err
}
// Get - get account info
func (c *Account) Get() error {
_, err := x.Table("account").Get(c)
return err
}

17
models/models.go Normal file
View File

@ -0,0 +1,17 @@
package models
import (
"fmt"
"github.com/go-xorm/xorm"
_ "github.com/lib/pq"
)
var x *xorm.Engine
// NewDB - db object
func NewDB() (*xorm.Engine, error) {
var err error
x, err = xorm.NewEngine("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", "postgres", "localhost", "gallery"))
return x, err
}

View File

@ -1,6 +1,9 @@
package routes
import (
"fmt"
"git.trj.tw/golang/go-gallery/models"
"github.com/gin-gonic/gin"
)
@ -13,8 +16,14 @@ func NewServ() *gin.Engine {
// SetDefaultRoutes -
func SetDefaultRoutes(r *gin.Engine) {
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "test",
})
accs, err := models.GetAllAccount()
if err != nil {
c.JSON(500, gin.H{
"message": "db error",
})
return
}
fmt.Println(accs)
c.JSON(200, accs)
})
}