Skip to content

Commit

Permalink
update Blocks module
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Sep 24, 2023
1 parent e0ac28c commit e7b4039
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 105 deletions.
1 change: 1 addition & 0 deletions _examples/view/layout/blocks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ func main() {
// Note, in Blocks engine, layouts
// are used by their base names, the
// blocks.LayoutDir(layoutDir) defaults to "./layouts".
// .Blocks(...).Layout("main") for default layout for all views, it can be modified through ctx.ViewLayout though.

app.Get("/", index)

Expand Down
5 changes: 0 additions & 5 deletions _examples/view/template_blocks_0/views/500.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<!-- You can define more than one block.
The default one is "content" which should be the main template's body.
So, even if it's missing (see index.html), it's added automatically by the view engine.
When you need to define more than one block, you have to be more specific:
-->
{{ define "content" }}
<h1>Internal Server Error</h1>
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions _examples/view/template_blocks_0/views/layouts/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<title>{{.Code}}</title>
</head>
<body>
{{ template "content" .}}
{{ template "content" . }}

{{block "message" .}}{{end}}
{{ block "message" . }}Default Error Message{{ end }}
</body>
</html>
38 changes: 38 additions & 0 deletions _examples/view/template_blocks_2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import "github.com/kataras/iris/v12"

// Based on https://github.com/kataras/iris/issues/2214.
func main() {
app := initApp()
app.Listen(":8080")
}

func initApp() *iris.Application {
app := iris.New()
app.Logger().SetLevel("debug")

tmpl := iris.Blocks("./src/public/html", ".html")
tmpl.Layout("main")
app.RegisterView(tmpl)

app.Get("/list", func(ctx iris.Context) {
ctx.View("files/list")
})

app.Get("/menu", func(ctx iris.Context) {
ctx.View("menu/menu")
})

app.Get("/list2", func(ctx iris.Context) {
ctx.ViewLayout("secondary")
ctx.View("files/list")
})

app.Get("/menu2", func(ctx iris.Context) {
ctx.ViewLayout("secondary")
ctx.View("menu/menu")
})

return app
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ define "title"}}
<title>222</title>
{{ end }}

{{ define "content" }}
<h1>List Content</h1>
{{ end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
{{ block "title" .}}<title>Main Default Title</title>{{end}}
</head>
<body>
{{ block "content" .}}<h1>Main Default Content</h1>{{end}}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
{{ block "title" .}}<title>Secondary Default Title</title>{{end}}
</head>
<body>
<h1>Secondary Layout</h1>
{{ block "content" .}}<h1>Secondary Default Content</h1>{{end}}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ define "title" }} <title>111</title>{{ end }}

{{ define "content" }}<h1>Menu Content</h1>{{ end }}
22 changes: 11 additions & 11 deletions aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,18 @@ func ConfigureMiddleware(handlers ...Handler) router.PartyConfigurator {
return &partyConfiguratorMiddleware{handlers: handlers}
}

var (
// Compression is a middleware which enables
// writing and reading using the best offered compression.
// Usage:
// app.Use (for matched routes)
// app.UseRouter (for both matched and 404s or other HTTP errors).
Compression = func(ctx Context) {
ctx.CompressWriter(true)
ctx.CompressReader(true)
ctx.Next()
}
// Compression is a middleware which enables
// writing and reading using the best offered compression.
// Usage:
// app.Use (for matched routes)
// app.UseRouter (for both matched and 404s or other HTTP errors).
func Compression(ctx Context) {
ctx.CompressWriter(true)
ctx.CompressReader(true)
ctx.Next()
}

var (
// AllowQuerySemicolons returns a middleware that serves requests by converting any
// unescaped semicolons(;) in the URL query to ampersands(&).
//
Expand Down
7 changes: 7 additions & 0 deletions core/router/api_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,13 @@ func (api *APIBuilder) createRoutes(errorCode int, methods []string, relativePat

mainHandlerFileName, mainHandlerFileNumber := context.HandlerFileLineRel(handlers[mainHandlerIndex])

// TODO: think of it.
if mainHandlerFileName == "<autogenerated>" {
// At PartyConfigure, 2nd+ level of routes it will get <autogenerated> but in reallity will be the same as the caller.
mainHandlerFileName = filename
mainHandlerFileNumber = line
}

// re-calculate mainHandlerIndex in favor of the middlewares.
mainHandlerIndex = len(beginHandlers) + mainHandlerIndex

Expand Down
85 changes: 1 addition & 84 deletions core/router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ package router

import (
"errors"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/core/errgroup"
"github.com/kataras/iris/v12/core/netutil"
macroHandler "github.com/kataras/iris/v12/macro/handler"

"github.com/kataras/golog"
"github.com/kataras/pio"
)

type (
Expand Down Expand Up @@ -371,87 +368,7 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
}
}

// TODO: move this and make it easier to read when all cases are, visually, tested.
if logger := h.logger; logger != nil && logger.Level == golog.DebugLevel && noLogCount < len(registeredRoutes) {
// group routes by method and print them without the [DBUG] and time info,
// the route logs are colorful.
// Note: don't use map, we need to keep registered order, use
// different slices for each method.

collect := func(method string) (methodRoutes []*Route) {
for _, r := range registeredRoutes {
if r.NoLog {
continue
}
if r.Method == method {
methodRoutes = append(methodRoutes, r)
}
}

return
}

type MethodRoutes struct {
method string
routes []*Route
}

allMethods := append(AllMethods, []string{MethodNone, ""}...)
methodRoutes := make([]MethodRoutes, 0, len(allMethods))

for _, method := range allMethods {
routes := collect(method)
if len(routes) > 0 {
methodRoutes = append(methodRoutes, MethodRoutes{method, routes})
}
}

if n := len(methodRoutes); n > 0 {
tr := "routes"
if len(registeredRoutes) == 1 {
tr = tr[0 : len(tr)-1]
}

bckpNewLine := logger.NewLine
logger.NewLine = false
debugLevel := golog.Levels[golog.DebugLevel]
// Replace that in order to not transfer it to the log handler (e.g. json)
// logger.Debugf("API: %d registered %s (", len(registeredRoutes), tr)
// with:
pio.WriteRich(logger.Printer, debugLevel.Title, debugLevel.ColorCode, debugLevel.Style...)
fmt.Fprintf(logger.Printer, " %s %sAPI: %d registered %s (", time.Now().Format(logger.TimeFormat), logger.Prefix, len(registeredRoutes)-noLogCount, tr)
//
logger.NewLine = bckpNewLine

for i, m := range methodRoutes {
// @method: @count
if i > 0 {
if i == n-1 {
fmt.Fprint(logger.Printer, " and ")
} else {
fmt.Fprint(logger.Printer, ", ")
}
}
if m.method == "" {
m.method = "ERROR"
}
fmt.Fprintf(logger.Printer, "%d ", len(m.routes))
pio.WriteRich(logger.Printer, m.method, TraceTitleColorCode(m.method))
}

fmt.Fprint(logger.Printer, ")\n")
}

for i, m := range methodRoutes {
for _, r := range m.routes {
r.Trace(logger.Printer, -1)
}

if i != len(allMethods)-1 {
logger.Printer.Write(pio.NewLine)
}
}
}
printRoutesInfo(h.logger, registeredRoutes, noLogCount)

return errgroup.Check(rp)
}
Expand Down
94 changes: 94 additions & 0 deletions core/router/handler_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package router

import (
"fmt"
"time"

"github.com/kataras/golog"
"github.com/kataras/pio"
)

func printRoutesInfo(logger *golog.Logger, registeredRoutes []*Route, noLogCount int) {
if !(logger != nil && logger.Level == golog.DebugLevel && noLogCount < len(registeredRoutes)) {
return
}

// group routes by method and print them without the [DBUG] and time info,
// the route logs are colorful.
// Note: don't use map, we need to keep registered order, use
// different slices for each method.

collect := func(method string) (methodRoutes []*Route) {
for _, r := range registeredRoutes {
if r.NoLog {
continue
}
if r.Method == method {
methodRoutes = append(methodRoutes, r)
}
}

return
}

type MethodRoutes struct {
method string
routes []*Route
}

allMethods := append(AllMethods, []string{MethodNone, ""}...)
methodRoutes := make([]MethodRoutes, 0, len(allMethods))

for _, method := range allMethods {
routes := collect(method)
if len(routes) > 0 {
methodRoutes = append(methodRoutes, MethodRoutes{method, routes})
}
}

if n := len(methodRoutes); n > 0 {
tr := "routes"
if len(registeredRoutes) == 1 {
tr = tr[0 : len(tr)-1]
}

bckpNewLine := logger.NewLine
logger.NewLine = false
debugLevel := golog.Levels[golog.DebugLevel]
// Replace that in order to not transfer it to the log handler (e.g. json)
// logger.Debugf("API: %d registered %s (", len(registeredRoutes), tr)
// with:
pio.WriteRich(logger.Printer, debugLevel.Title, debugLevel.ColorCode, debugLevel.Style...)
fmt.Fprintf(logger.Printer, " %s %sAPI: %d registered %s (", time.Now().Format(logger.TimeFormat), logger.Prefix, len(registeredRoutes)-noLogCount, tr)
//
logger.NewLine = bckpNewLine

for i, m := range methodRoutes {
// @method: @count
if i > 0 {
if i == n-1 {
fmt.Fprint(logger.Printer, " and ")
} else {
fmt.Fprint(logger.Printer, ", ")
}
}
if m.method == "" {
m.method = "ERROR"
}
fmt.Fprintf(logger.Printer, "%d ", len(m.routes))
pio.WriteRich(logger.Printer, m.method, TraceTitleColorCode(m.method))
}

fmt.Fprint(logger.Printer, ")\n")
}

for i, m := range methodRoutes {
for _, r := range m.routes {
r.Trace(logger.Printer, -1)
}

if i != len(allMethods)-1 {
logger.Printer.Write(pio.NewLine)
}
}
}
8 changes: 8 additions & 0 deletions core/router/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ func (r *Route) Trace(w io.Writer, stoppedIndex int) {
file, line = context.HandlerFileLineRel(h)
// If a middleware, e.g (macro) which changes the main handler index,
// skip it.

// TODO: think of it.
if file == "<autogenerated>" {
// At PartyConfigure, 2nd+ level of routes it will get <autogenerated> but in reallity will be the same as the caller.
file = r.RegisterFileName
line = r.RegisterLineNumber
}

if file == r.SourceFileName && line == r.SourceLineNumber {
continue
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/iris-contrib/httpexpect/v2 v2.15.2
github.com/iris-contrib/schema v0.0.6
github.com/json-iterator/go v1.1.12
github.com/kataras/blocks v0.0.7
github.com/kataras/blocks v0.0.8
github.com/kataras/golog v0.1.9
github.com/kataras/jwt v0.1.10
github.com/kataras/neffos v0.0.22
Expand Down Expand Up @@ -107,6 +107,7 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
moul.io/http2curl/v2 v2.3.0 // indirect
)
Loading

0 comments on commit e7b4039

Please sign in to comment.