mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
Add xorm example
Former-commit-id: 89940d656398f0adab13b4cae921b645bab7894c
This commit is contained in:
parent
85cfaa2874
commit
6c6c6b2885
|
@ -160,6 +160,10 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) files to
|
|||
|
||||
> The `context.ResponseWriter()` returns an enchament version of a http.ResponseWriter, these examples show some places where the Context uses this object. Besides that you can use it as you did before iris.
|
||||
|
||||
### ORM
|
||||
|
||||
- [Using xorm(Mysql, MyMysql, Postgres, Tidb, **SQLite**, MsSql, MsSql, Oracle)](orm/xorm/main.go)
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
- [Request Logger](http_request/request-logger/main.go)
|
||||
|
|
75
_examples/orm/xorm/main.go
Normal file
75
_examples/orm/xorm/main.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
// 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"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"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 context.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 context.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.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
}
|
Loading…
Reference in New Issue
Block a user