1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 21:45:57 +00:00

add a query helper on x/sqlx sub-package and fix the example for basicauth

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-03-08 19:45:25 +02:00
parent ad80a14b8f
commit 5a7485124c
5 changed files with 75 additions and 16 deletions

View File

@@ -2,6 +2,8 @@ package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/x/errors"
"github.com/kataras/iris/v12/middleware/basicauth"
)
@@ -31,8 +33,22 @@ func newApp() *iris.Application {
"mySecondusername": "mySecondpassword",
})
// to global app.Use(auth) (or app.UseGlobal before the .Run)
// to routes
// To the next routes of a party (group of 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)
*/
@@ -73,9 +89,11 @@ func handler(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 {
ctx.Application().Logger().Errorf("Logout error: %v", err)
errors.Internal.Err(ctx, err)
return
}
ctx.Redirect("/admin", iris.StatusTemporaryRedirect)
}

View File

@@ -123,17 +123,22 @@ func listEvents(ctx context.Context, db *sql.DB) ([]Event, error) {
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;`
rows, err := db.QueryContext(ctx, query, id)
if err != nil {
return Event{}, err
}
var evt Event
err = sqlx.Bind(&evt, rows)
return evt, err
err = sqlx.Query(ctx, db, &evt, query, id)
return
//
// Same as:
//
// rows, err := db.QueryContext(ctx, query, id)
// if err != nil {
// return Event{}, err
// }
//
// var evt Event
// err = sqlx.Bind(&evt, rows)
//
// return evt, err
}
func insert(db *sql.DB) iris.Handler {