mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add a query helper on x/sqlx sub-package and fix the example for basicauth
This commit is contained in:
parent
ad80a14b8f
commit
5a7485124c
|
@ -2,6 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
|
"github.com/kataras/iris/v12/x/errors"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12/middleware/basicauth"
|
"github.com/kataras/iris/v12/middleware/basicauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,8 +33,22 @@ func newApp() *iris.Application {
|
||||||
"mySecondusername": "mySecondpassword",
|
"mySecondusername": "mySecondpassword",
|
||||||
})
|
})
|
||||||
|
|
||||||
// to global app.Use(auth) (or app.UseGlobal before the .Run)
|
// To the next routes of a party (group of routes):
|
||||||
// to routes
|
/*
|
||||||
|
app.Use(auth)
|
||||||
|
*/
|
||||||
|
|
||||||
|
// For global effect, including not founds:
|
||||||
|
/*
|
||||||
|
app.UseRouter(auth)
|
||||||
|
*/
|
||||||
|
|
||||||
|
// For global effect, excluding http errors such as not founds:
|
||||||
|
/*
|
||||||
|
app.UseGlobal(auth) or app.Use(auth) before any route registered.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// For single/per routes:
|
||||||
/*
|
/*
|
||||||
app.Get("/mysecret", auth, h)
|
app.Get("/mysecret", auth, h)
|
||||||
*/
|
*/
|
||||||
|
@ -73,9 +89,11 @@ func handler(ctx iris.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func logout(ctx iris.Context) {
|
func logout(ctx iris.Context) {
|
||||||
err := ctx.Logout() // fires 401, invalidates the basic auth.
|
// fires 401, invalidates the basic auth,
|
||||||
|
// logout through javascript and ajax is a better solution though.
|
||||||
|
err := ctx.Logout()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Application().Logger().Errorf("Logout error: %v", err)
|
errors.Internal.Err(ctx, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
ctx.Redirect("/admin", iris.StatusTemporaryRedirect)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,17 +123,22 @@ func listEvents(ctx context.Context, db *sql.DB) ([]Event, error) {
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEvent(ctx context.Context, db *sql.DB, id string) (Event, error) {
|
func getEvent(ctx context.Context, db *sql.DB, id string) (evt Event, err error) {
|
||||||
query := `SELECT * FROM events WHERE id = $1 LIMIT 1;`
|
query := `SELECT * FROM events WHERE id = $1 LIMIT 1;`
|
||||||
rows, err := db.QueryContext(ctx, query, id)
|
err = sqlx.Query(ctx, db, &evt, query, id)
|
||||||
if err != nil {
|
return
|
||||||
return Event{}, err
|
//
|
||||||
}
|
// Same as:
|
||||||
|
//
|
||||||
var evt Event
|
// rows, err := db.QueryContext(ctx, query, id)
|
||||||
err = sqlx.Bind(&evt, rows)
|
// if err != nil {
|
||||||
|
// return Event{}, err
|
||||||
return evt, err
|
// }
|
||||||
|
//
|
||||||
|
// var evt Event
|
||||||
|
// err = sqlx.Bind(&evt, rows)
|
||||||
|
//
|
||||||
|
// return evt, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func insert(db *sql.DB) iris.Handler {
|
func insert(db *sql.DB) iris.Handler {
|
||||||
|
|
|
@ -2160,6 +2160,15 @@ func (ctx *Context) AbsoluteURI(s string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userInfo := ""
|
||||||
|
if s[0] == '@' {
|
||||||
|
endUserInfoIdx := strings.IndexByte(s, '/')
|
||||||
|
if endUserInfoIdx > 0 && len(s) > endUserInfoIdx {
|
||||||
|
userInfo = s[1:endUserInfoIdx] + "@"
|
||||||
|
s = s[endUserInfoIdx:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if s[0] == '/' {
|
if s[0] == '/' {
|
||||||
scheme := ctx.request.URL.Scheme
|
scheme := ctx.request.URL.Scheme
|
||||||
if scheme == "" {
|
if scheme == "" {
|
||||||
|
@ -2172,7 +2181,7 @@ func (ctx *Context) AbsoluteURI(s string) string {
|
||||||
|
|
||||||
host := ctx.Host()
|
host := ctx.Host()
|
||||||
|
|
||||||
return scheme + "//" + host + path.Clean(s)
|
return scheme + "//" + userInfo + host + path.Clean(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
if u, err := url.Parse(s); err == nil {
|
if u, err := url.Parse(s); err == nil {
|
||||||
|
|
|
@ -493,6 +493,12 @@ func (b *BasicAuth) logout(ctx *context.Context) {
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
delete(b.credentials, fullUser)
|
delete(b.credentials, fullUser)
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
|
|
||||||
|
if b.opts.MaxTries > 0 {
|
||||||
|
b.setCurrentTries(ctx, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.StatusCode(http.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package sqlx
|
package sqlx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -54,6 +55,11 @@ func Register(tableName string, value interface{}) *Schema {
|
||||||
return DefaultSchema.Register(tableName, value)
|
return DefaultSchema.Register(tableName, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Query is a shortcut of executing a query and bind the result to "dst".
|
||||||
|
func Query(ctx context.Context, db *sql.DB, dst interface{}, query string, args ...interface{}) error {
|
||||||
|
return DefaultSchema.Query(ctx, db, dst, query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
// Bind sets "dst" to the result of "src" and reports any errors.
|
// Bind sets "dst" to the result of "src" and reports any errors.
|
||||||
func Bind(dst interface{}, src *sql.Rows) error {
|
func Bind(dst interface{}, src *sql.Rows) error {
|
||||||
return DefaultSchema.Bind(dst, src)
|
return DefaultSchema.Bind(dst, src)
|
||||||
|
@ -90,6 +96,21 @@ func (s *Schema) Register(tableName string, value interface{}) *Schema {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Query is a shortcut of executing a query and bind the result to "dst".
|
||||||
|
func (s *Schema) Query(ctx context.Context, db *sql.DB, dst interface{}, query string, args ...interface{}) error {
|
||||||
|
rows, err := db.QueryContext(ctx, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !s.AutoCloseRows { // if not close on bind, we must close it here.
|
||||||
|
defer rows.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.Bind(dst, rows)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Bind sets "dst" to the result of "src" and reports any errors.
|
// Bind sets "dst" to the result of "src" and reports any errors.
|
||||||
func (s *Schema) Bind(dst interface{}, src *sql.Rows) error {
|
func (s *Schema) Bind(dst interface{}, src *sql.Rows) error {
|
||||||
typ := reflect.TypeOf(dst)
|
typ := reflect.TypeOf(dst)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user