<commit_before>package examples

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"

	"github.com/kataras/iris"
	"github.com/kataras/iris/context"
	"github.com/kataras/iris/middleware/basicauth"
	"github.com/kataras/iris/sessions"
)

// IrisHandler tests iris handler
func IrisHandler() http.Handler {
	app := iris.New()

	sess := sessions.New(sessions.Config{
		Cookie: "irissessionid",
	})

	app.Get("/things", func(ctx context.Context) {
		ctx.JSON([]interface{}{
			context.Map{
				"name":        "foo",
				"description": "foo thing",
			},
			context.Map{
				"name":        "bar",
				"description": "bar thing",
			},
		})
	})

	app.Post("/redirect", func(ctx context.Context) {
		ctx.Redirect("/things", iris.StatusFound)
	})

	app.Post("/params/{x}/{y}", func(ctx context.Context) {
		ctx.JSON(context.Map{
			"x":  ctx.Params().Get("x"),
			"y":  ctx.Params().Get("y"),
			"q":  ctx.URLParam("q"),
			"p1": ctx.FormValue("p1"),
			"p2": ctx.FormValue("p2"),
		})
	})

	auth := basicauth.Default(map[string]string{
		"ford": "betelgeuse7",
	})

	app.Get("/auth", auth, func(ctx context.Context) {
		ctx.Writef("authenticated!")
	})

	app.Post("/session/set", func(ctx context.Context) {
		session := sess.Start(ctx)

		v := context.Map{}

		if err := ctx.ReadJSON(&v); err != nil {
			ctx.StatusCode(iris.StatusBadRequest)
			return
		}

		session.Set("name", v["name"])
	})

	app.Get("/session/get", func(ctx context.Context) {
		session := sess.Start(ctx)

		ctx.JSON(context.Map{
			"name": session.GetString("name"),
		})
	})

	app.Get("/stream", func(ctx context.Context) {
		ctx.StreamWriter(func(w io.Writer) bool {
			for i := 0; i < 10; i++ {
				fmt.Fprintf(w, "%d", i)
			}
			// return true to continue, return false to stop and flush
			return false
		})
		// if we had to write here then the StreamWriter callback should
		// return true
	})

	app.Post("/stream", func(ctx context.Context) {
		body, err := ioutil.ReadAll(ctx.Request().Body)
		if err != nil {
			app.Logger().Error(err)
			ctx.StatusCode(iris.StatusBadRequest)
			ctx.StopExecution()
			return
		}
		ctx.Write(body)
	})

	sub := app.Subdomain("subdomain")

	sub.Post("/set", func(ctx context.Context) {
		session := sess.Start(ctx)
		session.Set("message", "hello from subdomain")
	})

	sub.Get("/get", func(ctx context.Context) {
		session := sess.Start(ctx)
		ctx.WriteString(session.GetString("message"))
	})<commit_msg>let's use the type alias iris.Context and build it go1.12 and above<commit_after>// +build go1.12

package examples

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"

	"github.com/kataras/iris"
	"github.com/kataras/iris/middleware/basicauth"
	"github.com/kataras/iris/sessions"
)

// IrisHandler tests iris handler
func IrisHandler() http.Handler {
	app := iris.New()

	sess := sessions.New(sessions.Config{
		Cookie: "irissessionid",
	})

	app.Get("/things", func(ctx iris.Context) {
		ctx.JSON([]interface{}{
			iris.Map{
				"name":        "foo",
				"description": "foo thing",
			},
			iris.Map{
				"name":        "bar",
				"description": "bar thing",
			},
		})
	})

	app.Post("/redirect", func(ctx iris.Context) {
		ctx.Redirect("/things", iris.StatusFound)
	})

	app.Post("/params/{x}/{y}", func(ctx iris.Context) {
		ctx.JSON(iris.Map{
			"x":  ctx.Params().Get("x"),
			"y":  ctx.Params().Get("y"),
			"q":  ctx.URLParam("q"),
			"p1": ctx.FormValue("p1"),
			"p2": ctx.FormValue("p2"),
		})
	})

	auth := basicauth.Default(map[string]string{
		"ford": "betelgeuse7",
	})

	app.Get("/auth", auth, func(ctx iris.Context) {
		ctx.Writef("authenticated!")
	})

	app.Post("/session/set", func(ctx iris.Context) {
		session := sess.Start(ctx)

		v := iris.Map{}

		if err := ctx.ReadJSON(&v); err != nil {
			ctx.StatusCode(iris.StatusBadRequest)
			return
		}

		session.Set("name", v["name"])
	})

	app.Get("/session/get", func(ctx iris.Context) {
		session := sess.Start(ctx)

		ctx.JSON(iris.Map{
			"name": session.GetString("name"),
		})
	})

	app.Get("/stream", func(ctx iris.Context) {
		ctx.StreamWriter(func(w io.Writer) bool {
			for i := 0; i < 10; i++ {
				fmt.Fprintf(w, "%d", i)
			}
			// return true to continue, return false to stop and flush
			return false
		})
		// if we had to write here then the StreamWriter callback should
		// return true
	})

	app.Post("/stream", func(ctx iris.Context) {
		body, err := ioutil.ReadAll(ctx.Request().Body)
		if err != nil {
			app.Logger().Error(err)
			ctx.StatusCode(iris.StatusBadRequest)
			ctx.StopExecution()
			return
		}
		ctx.Write(body)
	})

	sub := app.Subdomain("subdomain")

	sub.Post("/set", func(ctx iris.Context) {
		session := sess.Start(ctx)
		session.Set("message", "hello from subdomain")
	})

	sub.Get("/get", func(ctx iris.Context) {
		session := sess.Start(ctx)
		ctx.WriteString(session.GetString("message"))
	})
{'fraction_non_alphanumeric': 0.19464063149148317, 'fraction_numerical': 0.004570004154549232, 'mean_word_length': 3.4377880184331797, 'pattern_counts': {'":': 22, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1408757', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1802, 'n_words': 339}