mirror of
https://github.com/kataras/iris.git
synced 2025-03-15 17:36:29 +01:00
Add a third, simple, example for folder structuring as requested at https://github.com/kataras/iris/issues/748
One change to code base but it will be described at the next version: Error Handlers (`app.OnErrorCode/OnAnyErrorCode`) respect the `app.UseGlobal`'s middlewares now (not the `app.Use` for reasons we can all understand, hopefully). Former-commit-id: ec97bbb04548f9932cf4d7b950be513b70747bcb
This commit is contained in:
parent
ba63031871
commit
38c6241055
|
@ -22,6 +22,7 @@ Structuring is always depends on your needs. We can't tell you how to design you
|
||||||
|
|
||||||
- [Example 1](mvc/login)
|
- [Example 1](mvc/login)
|
||||||
- [Example 2](structuring/mvc)
|
- [Example 2](structuring/mvc)
|
||||||
|
- [Example 3](structuring/handler-based)
|
||||||
|
|
||||||
### HTTP Listening
|
### HTTP Listening
|
||||||
|
|
||||||
|
|
113
_examples/structuring/handler-based/bootstrap/bootstrapper.go
Normal file
113
_examples/structuring/handler-based/bootstrap/bootstrapper.go
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/securecookie"
|
||||||
|
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
"github.com/kataras/iris/sessions"
|
||||||
|
"github.com/kataras/iris/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Configurator func(*Bootstrapper)
|
||||||
|
|
||||||
|
type Bootstrapper struct {
|
||||||
|
*iris.Application
|
||||||
|
AppName string
|
||||||
|
AppOwner string
|
||||||
|
AppSpawnDate time.Time
|
||||||
|
|
||||||
|
Sessions *sessions.Sessions
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new Bootstrapper.
|
||||||
|
func New(appName, appOwner string, cfgs ...Configurator) *Bootstrapper {
|
||||||
|
b := &Bootstrapper{
|
||||||
|
AppName: appName,
|
||||||
|
AppOwner: appOwner,
|
||||||
|
AppSpawnDate: time.Now(),
|
||||||
|
Application: iris.New(),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cfg := range cfgs {
|
||||||
|
cfg(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetupViews loads the templates.
|
||||||
|
func (b *Bootstrapper) SetupViews(viewsDir string) {
|
||||||
|
b.RegisterView(iris.HTML(viewsDir, ".html").Layout("shared/layout.html"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetupSessions initializes the sessions, optionally.
|
||||||
|
func (b *Bootstrapper) SetupSessions(expires time.Duration, cookieHashKey, cookieBlockKey []byte) {
|
||||||
|
b.Sessions = sessions.New(sessions.Config{
|
||||||
|
Cookie: "SECRET_SESS_COOKIE_" + b.AppName,
|
||||||
|
Expires: expires,
|
||||||
|
Encoding: securecookie.New(cookieHashKey, cookieBlockKey),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetupWebsockets prepares the websocket server.
|
||||||
|
func (b *Bootstrapper) SetupWebsockets(endpoint string, onConnection websocket.ConnectionFunc) {
|
||||||
|
ws := websocket.New(websocket.Config{})
|
||||||
|
ws.OnConnection(onConnection)
|
||||||
|
|
||||||
|
b.Get(endpoint, ws.Handler())
|
||||||
|
b.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||||
|
ctx.Write(websocket.ClientSource)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetupErrorHandlers prepares the http error handlers (>=400).
|
||||||
|
func (b *Bootstrapper) SetupErrorHandlers() {
|
||||||
|
b.OnAnyErrorCode(func(ctx iris.Context) {
|
||||||
|
err := iris.Map{
|
||||||
|
"app": b.AppName,
|
||||||
|
"status": ctx.GetStatusCode(),
|
||||||
|
"message": ctx.Values().GetString("message"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if jsonOutput := ctx.URLParamExists("json"); jsonOutput {
|
||||||
|
ctx.JSON(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.ViewData("Err", err)
|
||||||
|
ctx.ViewData("Title", "Error")
|
||||||
|
ctx.View("shared/error.html")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// StaticAssets is the root directory for public assets like images, css, js.
|
||||||
|
StaticAssets = "./public/"
|
||||||
|
// Favicon is the relative 9to the "StaticAssets") favicon path for our app.
|
||||||
|
Favicon = "favicon.ico"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bootstrap prepares our application.
|
||||||
|
//
|
||||||
|
// Returns itself.
|
||||||
|
func (b *Bootstrapper) Bootstrap() *Bootstrapper {
|
||||||
|
b.SetupViews("./views")
|
||||||
|
b.SetupSessions(24*time.Hour,
|
||||||
|
[]byte("the-big-and-secret-fash-key-here"),
|
||||||
|
[]byte("lot-secret-of-characters-big-too"),
|
||||||
|
)
|
||||||
|
b.SetupErrorHandlers()
|
||||||
|
|
||||||
|
// static files
|
||||||
|
b.Favicon(StaticAssets + Favicon)
|
||||||
|
b.StaticWeb(StaticAssets[1:len(StaticAssets)-1], StaticAssets)
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen starts the http server with the specified "addr".
|
||||||
|
func (b *Bootstrapper) Listen(addr string, cfgs ...iris.Configurator) {
|
||||||
|
b.Run(iris.Addr(addr), cfgs...)
|
||||||
|
}
|
20
_examples/structuring/handler-based/main.go
Normal file
20
_examples/structuring/handler-based/main.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris/_examples/structuring/handler-based/bootstrap"
|
||||||
|
"github.com/kataras/iris/_examples/structuring/handler-based/middleware/identity"
|
||||||
|
"github.com/kataras/iris/_examples/structuring/handler-based/routes"
|
||||||
|
)
|
||||||
|
|
||||||
|
var app = bootstrap.New("Awesome App", "kataras2006@hotmail.com",
|
||||||
|
identity.Configure,
|
||||||
|
routes.Configure,
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
app.Bootstrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app.Listen(":8080")
|
||||||
|
}
|
31
_examples/structuring/handler-based/main_test.go
Normal file
31
_examples/structuring/handler-based/main_test.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kataras/iris/httptest"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestApp runs by `$ go test -v`.
|
||||||
|
func TestApp(t *testing.T) {
|
||||||
|
e := httptest.New(t, app.Application)
|
||||||
|
|
||||||
|
// test our routes
|
||||||
|
e.GET("/").Expect().Status(httptest.StatusOK)
|
||||||
|
e.GET("/follower/42").Expect().Status(httptest.StatusOK).
|
||||||
|
Body().Equal("from /follower/{id:long} with ID: 42")
|
||||||
|
e.GET("/following/52").Expect().Status(httptest.StatusOK).
|
||||||
|
Body().Equal("from /following/{id:long} with ID: 52")
|
||||||
|
e.GET("/like/64").Expect().Status(httptest.StatusOK).
|
||||||
|
Body().Equal("from /like/{id:long} with ID: 64")
|
||||||
|
|
||||||
|
// test not found
|
||||||
|
e.GET("/notfound").Expect().Status(httptest.StatusNotFound)
|
||||||
|
expectedErr := map[string]interface{}{
|
||||||
|
"app": app.AppName,
|
||||||
|
"status": httptest.StatusNotFound,
|
||||||
|
"message": "",
|
||||||
|
}
|
||||||
|
e.GET("/anotfoundwithjson").WithQuery("json", nil).
|
||||||
|
Expect().Status(httptest.StatusNotFound).JSON().Equal(expectedErr)
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package identity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
|
||||||
|
"github.com/kataras/iris/_examples/structuring/handler-based/bootstrap"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns a new handler which adds some headers and view data
|
||||||
|
// describing the application, i.e the owner, the startup time.
|
||||||
|
func New(b *bootstrap.Bootstrapper) iris.Handler {
|
||||||
|
return func(ctx iris.Context) {
|
||||||
|
// response headers
|
||||||
|
ctx.Header("App-Name", b.AppName)
|
||||||
|
ctx.Header("App-Owner", b.AppOwner)
|
||||||
|
ctx.Header("App-Since", time.Since(b.AppSpawnDate).String())
|
||||||
|
|
||||||
|
ctx.Header("Server", "Iris: https://iris-go.com")
|
||||||
|
|
||||||
|
// view data if ctx.View or c.Tmpl = "$page.html" will be called next.
|
||||||
|
ctx.ViewData("AppName", b.AppName)
|
||||||
|
ctx.ViewData("AppOwner", b.AppOwner)
|
||||||
|
ctx.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure creates a new identity middleware and registers that to the app.
|
||||||
|
func Configure(b *bootstrap.Bootstrapper) {
|
||||||
|
h := New(b)
|
||||||
|
b.UseGlobal(h)
|
||||||
|
}
|
BIN
_examples/structuring/handler-based/public/favicon.ico
Normal file
BIN
_examples/structuring/handler-based/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
11
_examples/structuring/handler-based/routes/follower.go
Normal file
11
_examples/structuring/handler-based/routes/follower.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetFollowerHandler handles the GET: /follower/{id}
|
||||||
|
func GetFollowerHandler(ctx iris.Context) {
|
||||||
|
id, _ := ctx.Params().GetInt64("id")
|
||||||
|
ctx.Writef("from "+ctx.GetCurrentRoute().Path()+" with ID: %d", id)
|
||||||
|
}
|
11
_examples/structuring/handler-based/routes/following.go
Normal file
11
_examples/structuring/handler-based/routes/following.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetFollowingHandler handles the GET: /following/{id}
|
||||||
|
func GetFollowingHandler(ctx iris.Context) {
|
||||||
|
id, _ := ctx.Params().GetInt64("id")
|
||||||
|
ctx.Writef("from "+ctx.GetCurrentRoute().Path()+" with ID: %d", id)
|
||||||
|
}
|
11
_examples/structuring/handler-based/routes/index.go
Normal file
11
_examples/structuring/handler-based/routes/index.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetIndexHandler handles the GET: /
|
||||||
|
func GetIndexHandler(ctx iris.Context) {
|
||||||
|
ctx.ViewData("Title", "Index Page")
|
||||||
|
ctx.View("index.html")
|
||||||
|
}
|
11
_examples/structuring/handler-based/routes/like.go
Normal file
11
_examples/structuring/handler-based/routes/like.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetLikeHandler handles the GET: /like/{id}
|
||||||
|
func GetLikeHandler(ctx iris.Context) {
|
||||||
|
id, _ := ctx.Params().GetInt64("id")
|
||||||
|
ctx.Writef("from "+ctx.GetCurrentRoute().Path()+" with ID: %d", id)
|
||||||
|
}
|
14
_examples/structuring/handler-based/routes/routes.go
Normal file
14
_examples/structuring/handler-based/routes/routes.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kataras/iris/_examples/structuring/handler-based/bootstrap"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Configure registers the nessecary routes to the app.
|
||||||
|
func Configure(b *bootstrap.Bootstrapper) {
|
||||||
|
// routes
|
||||||
|
b.Get("/", GetIndexHandler)
|
||||||
|
b.Get("/follower/{id:long}", GetFollowerHandler)
|
||||||
|
b.Get("/following/{id:long}", GetFollowingHandler)
|
||||||
|
b.Get("/like/{id:long}", GetLikeHandler)
|
||||||
|
}
|
1
_examples/structuring/handler-based/views/index.html
Normal file
1
_examples/structuring/handler-based/views/index.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Welcome!!</h1>
|
|
@ -0,0 +1,5 @@
|
||||||
|
<h1 class="text-danger">Error.</h1>
|
||||||
|
<h2 class="text-danger">An error occurred while processing your request.</h2>
|
||||||
|
|
||||||
|
<h3>{{.Err.status}}</h3>
|
||||||
|
<h4>{{.Err.message}}</h4>
|
23
_examples/structuring/handler-based/views/shared/layout.html
Normal file
23
_examples/structuring/handler-based/views/shared/layout.html
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
|
||||||
|
<title>{{.Title}} - {{.AppName}}</title>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<!-- Render the current template here -->
|
||||||
|
{{ yield }}
|
||||||
|
<hr />
|
||||||
|
<footer>
|
||||||
|
<p>© 2017 - {{.AppOwner}}</p>
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
|
@ -364,6 +364,8 @@ type Context interface {
|
||||||
// | Various Request and Post Data |
|
// | Various Request and Post Data |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
|
||||||
|
// URLParam returns true if the url parameter exists, otherwise false.
|
||||||
|
URLParamExists(name string) bool
|
||||||
// URLParamDefault returns the get parameter from a request, if not found then "def" is returned.
|
// URLParamDefault returns the get parameter from a request, if not found then "def" is returned.
|
||||||
URLParamDefault(name string, def string) string
|
URLParamDefault(name string, def string) string
|
||||||
// URLParam returns the get parameter from a request , if any.
|
// URLParam returns the get parameter from a request , if any.
|
||||||
|
@ -1236,6 +1238,7 @@ func (ctx *context) ContentType(cType string) {
|
||||||
cType += "; charset=" + charset
|
cType += "; charset=" + charset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.writer.Header().Set(contentTypeHeaderKey, cType)
|
ctx.writer.Header().Set(contentTypeHeaderKey, cType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1273,6 +1276,16 @@ func (ctx *context) GetStatusCode() int {
|
||||||
// | Various Request and Post Data |
|
// | Various Request and Post Data |
|
||||||
// +------------------------------------------------------------+
|
// +------------------------------------------------------------+
|
||||||
|
|
||||||
|
// URLParam returns true if the url parameter exists, otherwise false.
|
||||||
|
func (ctx *context) URLParamExists(name string) bool {
|
||||||
|
if q := ctx.request.URL.Query(); q != nil {
|
||||||
|
_, exists := q[name]
|
||||||
|
return exists
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// URLParamDefault returns the get parameter from a request, if not found then "def" is returned.
|
// URLParamDefault returns the get parameter from a request, if not found then "def" is returned.
|
||||||
func (ctx *context) URLParamDefault(name string, def string) string {
|
func (ctx *context) URLParamDefault(name string, def string) string {
|
||||||
v := ctx.request.URL.Query().Get(name)
|
v := ctx.request.URL.Query().Get(name)
|
||||||
|
|
|
@ -534,6 +534,12 @@ func (api *APIBuilder) Controller(relativePath string, controller activator.Base
|
||||||
api.reporter.Add("%v for path: '%s'", err, relativePath)
|
api.reporter.Add("%v for path: '%s'", err, relativePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cInit, ok := controller.(interface {
|
||||||
|
Init(activator.RegisterFunc)
|
||||||
|
}); ok {
|
||||||
|
cInit.Init(registerFunc)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -800,6 +806,10 @@ func (api *APIBuilder) StaticWeb(requestPath string, systemPath string) *Route {
|
||||||
// and/or disable the gzip if gzip response recorder
|
// and/or disable the gzip if gzip response recorder
|
||||||
// was active.
|
// was active.
|
||||||
func (api *APIBuilder) OnErrorCode(statusCode int, handlers ...context.Handler) {
|
func (api *APIBuilder) OnErrorCode(statusCode int, handlers ...context.Handler) {
|
||||||
|
if len(api.beginGlobalHandlers) > 0 {
|
||||||
|
handlers = joinHandlers(api.beginGlobalHandlers, handlers)
|
||||||
|
}
|
||||||
|
|
||||||
api.errorCodeHandlers.Register(statusCode, handlers...)
|
api.errorCodeHandlers.Register(statusCode, handlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user