mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 03:47:04 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
5
_examples/database/orm/gorm/REAMDE.md
Normal file
5
_examples/database/orm/gorm/REAMDE.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# GORM
|
||||
|
||||
This example is pull by [#1275 PR](https://github.com/kataras/iris/pull/1275) by [@wuxiaoxiaoshen](https://github.com/wuxiaoxiaoshen).
|
||||
|
||||
A more complete and real-world example can be found at the <https://github.com/snowlyg/IrisApiProject> project created by [@snowlyg](https://github.com/snowlyg).
|
||||
177
_examples/database/orm/gorm/main.go
Normal file
177
_examples/database/orm/gorm/main.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/kataras/iris/v12"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Salt string `gorm:"type:varchar(255)" json:"salt"`
|
||||
Username string `gorm:"type:varchar(32)" json:"username"`
|
||||
Password string `gorm:"type:varchar(200);column:password" json:"-"`
|
||||
Languages string `gorm:"type:varchar(200);column:languages" json:"languages"`
|
||||
}
|
||||
|
||||
func (u User) TableName() string {
|
||||
return "gorm_user"
|
||||
}
|
||||
|
||||
type UserSerializer struct {
|
||||
ID uint `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Salt string `json:"salt"`
|
||||
UserName string `json:"user_name"`
|
||||
Password string `json:"-"`
|
||||
Languages string `json:"languages"`
|
||||
}
|
||||
|
||||
func (self User) Serializer() UserSerializer {
|
||||
return UserSerializer{
|
||||
ID: self.ID,
|
||||
CreatedAt: self.CreatedAt.Truncate(time.Second),
|
||||
UpdatedAt: self.UpdatedAt.Truncate(time.Second),
|
||||
Salt: self.Salt,
|
||||
Password: self.Password,
|
||||
Languages: self.Languages,
|
||||
UserName: self.Username,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.Default()
|
||||
db, err := gorm.Open("sqlite3", "test.db")
|
||||
db.LogMode(true) // show SQL logger
|
||||
if err != nil {
|
||||
app.Logger().Fatalf("connect to sqlite3 failed")
|
||||
return
|
||||
}
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
defer db.Close()
|
||||
})
|
||||
|
||||
if os.Getenv("ENV") != "" {
|
||||
db.DropTableIfExists(&User{}) // drop table
|
||||
}
|
||||
db.AutoMigrate(&User{}) // create table: // AutoMigrate run auto migration for given models, will only add missing fields, won't delete/change current data
|
||||
|
||||
app.Post("/post_user", func(ctx iris.Context) {
|
||||
var user User
|
||||
user = User{
|
||||
Username: "gorm",
|
||||
Salt: "hash---",
|
||||
Password: "admin",
|
||||
Languages: "gorm",
|
||||
}
|
||||
if err := db.FirstOrCreate(&user); err == nil {
|
||||
app.Logger().Fatalf("created one record failed: %s", err.Error)
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"error": err.Error,
|
||||
})
|
||||
return
|
||||
}
|
||||
ctx.JSON(
|
||||
iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"data": user.Serializer(),
|
||||
})
|
||||
})
|
||||
|
||||
app.Get("/get_user/{id:uint}", func(ctx iris.Context) {
|
||||
var user User
|
||||
id, _ := ctx.Params().GetUint("id")
|
||||
app.Logger().Println(id)
|
||||
if err := db.Where("id = ?", int(id)).First(&user).Error; err != nil {
|
||||
app.Logger().Fatalf("find one record failed: %t", err == nil)
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"error": err.Error,
|
||||
})
|
||||
return
|
||||
}
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"data": user.Serializer(),
|
||||
})
|
||||
})
|
||||
|
||||
app.Delete("/delete_user/{id:uint}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetUint("id")
|
||||
if id == 0 {
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"detail": "query param id should not be nil",
|
||||
})
|
||||
return
|
||||
}
|
||||
var user User
|
||||
if err := db.Where("id = ?", id).First(&user).Error; err != nil {
|
||||
app.Logger().Fatalf("record not found")
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"detail": err.Error,
|
||||
})
|
||||
return
|
||||
}
|
||||
db.Delete(&user)
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"data": user.Serializer(),
|
||||
})
|
||||
})
|
||||
|
||||
app.Patch("/patch_user/{id:uint}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetUint("id")
|
||||
if id == 0 {
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"detail": "query param id should not be nil",
|
||||
})
|
||||
return
|
||||
}
|
||||
var user User
|
||||
tx := db.Begin()
|
||||
if err := tx.Where("id = ?", id).First(&user).Error; err != nil {
|
||||
app.Logger().Fatalf("record not found")
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"detail": err.Error,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var body patchParam
|
||||
ctx.ReadJSON(&body)
|
||||
app.Logger().Println(body)
|
||||
if err := tx.Model(&user).Updates(map[string]interface{}{"username": body.Data.UserName, "password": body.Data.Password}).Error; err != nil {
|
||||
app.Logger().Fatalf("update record failed")
|
||||
tx.Rollback()
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"error": err.Error,
|
||||
})
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
ctx.JSON(iris.Map{
|
||||
"code": http.StatusOK,
|
||||
"data": user.Serializer(),
|
||||
})
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
type patchParam struct {
|
||||
Data struct {
|
||||
UserName string `json:"user_name" form:"user_name"`
|
||||
Password string `json:"password" form:"password"`
|
||||
} `json:"data"`
|
||||
}
|
||||
74
_examples/database/orm/xorm/main.go
Normal file
74
_examples/database/orm/xorm/main.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Package main shows how an orm can be used within your web app
|
||||
// it just inserts a column and select the first.
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
/*
|
||||
go get -u github.com/mattn/go-sqlite3
|
||||
go get -u github.com/go-xorm/xorm
|
||||
|
||||
If you're on win64 and you can't install go-sqlite3:
|
||||
1. Download: https://sourceforge.net/projects/mingw-w64/files/latest/download
|
||||
2. Select "x86_x64" and "posix"
|
||||
3. Add C:\Program Files\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev1\mingw64\bin
|
||||
to your PATH env variable.
|
||||
|
||||
Docs: http://xorm.io/docs/
|
||||
*/
|
||||
|
||||
// User is our user table structure.
|
||||
type User struct {
|
||||
ID int64 // auto-increment by-default by xorm
|
||||
Version string `xorm:"varchar(200)"`
|
||||
Salt string
|
||||
Username string
|
||||
Password string `xorm:"varchar(200)"`
|
||||
Languages string `xorm:"varchar(200)"`
|
||||
CreatedAt time.Time `xorm:"created"`
|
||||
UpdatedAt time.Time `xorm:"updated"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
orm, err := xorm.NewEngine("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
app.Logger().Fatalf("orm failed to initialized: %v", err)
|
||||
}
|
||||
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
orm.Close()
|
||||
})
|
||||
|
||||
err = orm.Sync2(new(User))
|
||||
|
||||
if err != nil {
|
||||
app.Logger().Fatalf("orm failed to initialized User table: %v", err)
|
||||
}
|
||||
|
||||
app.Get("/insert", func(ctx iris.Context) {
|
||||
user := &User{Username: "kataras", Salt: "hash---", Password: "hashed", CreatedAt: time.Now(), UpdatedAt: time.Now()}
|
||||
orm.Insert(user)
|
||||
|
||||
ctx.Writef("user inserted: %#v", user)
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
user := User{ID: 1}
|
||||
if ok, _ := orm.Get(&user); ok {
|
||||
ctx.Writef("user found: %#v", user)
|
||||
}
|
||||
})
|
||||
|
||||
// http://localhost:8080/insert
|
||||
// http://localhost:8080/get
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Reference in New Issue
Block a user