mirror of
https://github.com/kataras/iris.git
synced 2026-01-02 17:57:11 +00:00
Former-commit-id: 23f795a6b6b64745311d0630d8e630edc3f65244
This commit is contained in:
123
_examples/bootstrapper/bootstrap/bootstrapper.go
Normal file
123
_examples/bootstrapper/bootstrap/bootstrapper.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/securecookie"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/logger"
|
||||
"github.com/kataras/iris/v12/middleware/recover"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
"github.com/kataras/iris/v12/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, handler websocket.ConnHandler) {
|
||||
ws := websocket.New(websocket.DefaultGorillaUpgrader, handler)
|
||||
|
||||
b.Get(endpoint, websocket.Handler(ws))
|
||||
}
|
||||
|
||||
// SetupErrorHandlers prepares the http error handlers
|
||||
// `(context.StatusCodeNotSuccessful`, which defaults to >=400 (but you can change it).
|
||||
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"
|
||||
)
|
||||
|
||||
// Configure accepts configurations and runs them inside the Bootstraper's context.
|
||||
func (b *Bootstrapper) Configure(cs ...Configurator) {
|
||||
for _, c := range cs {
|
||||
c(b)
|
||||
}
|
||||
}
|
||||
|
||||
// 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.HandleDir(StaticAssets[1:len(StaticAssets)-1], StaticAssets)
|
||||
|
||||
// middleware, after static files
|
||||
b.Use(recover.New())
|
||||
b.Use(logger.New())
|
||||
|
||||
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...)
|
||||
}
|
||||
BIN
_examples/bootstrapper/folder_structure.png
Normal file
BIN
_examples/bootstrapper/folder_structure.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
19
_examples/bootstrapper/main.go
Normal file
19
_examples/bootstrapper/main.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12/_examples/bootstrap/bootstrap"
|
||||
"github.com/kataras/iris/v12/_examples/bootstrap/middleware/identity"
|
||||
"github.com/kataras/iris/v12/_examples/bootstrap/routes"
|
||||
)
|
||||
|
||||
func newApp() *bootstrap.Bootstrapper {
|
||||
app := bootstrap.New("Awesome App", "kataras2006@hotmail.com")
|
||||
app.Bootstrap()
|
||||
app.Configure(identity.Configure, routes.Configure)
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Listen(":8080")
|
||||
}
|
||||
32
_examples/bootstrapper/main_test.go
Normal file
32
_examples/bootstrapper/main_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
)
|
||||
|
||||
// go test -v
|
||||
func TestApp(t *testing.T) {
|
||||
app := newApp()
|
||||
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:int64} with ID: 42")
|
||||
e.GET("/following/52").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("from /following/{id:int64} with ID: 52")
|
||||
e.GET("/like/64").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("from /like/{id:int64} 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)
|
||||
}
|
||||
33
_examples/bootstrapper/middleware/identity/identity.go
Normal file
33
_examples/bootstrapper/middleware/identity/identity.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
|
||||
"github.com/kataras/iris/v12/_examples/bootstrap/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/bootstrapper/public/favicon.ico
Normal file
BIN
_examples/bootstrapper/public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
11
_examples/bootstrapper/routes/follower.go
Normal file
11
_examples/bootstrapper/routes/follower.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// 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/bootstrapper/routes/following.go
Normal file
11
_examples/bootstrapper/routes/following.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// 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/bootstrapper/routes/index.go
Normal file
11
_examples/bootstrapper/routes/index.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// GetIndexHandler handles the GET: /
|
||||
func GetIndexHandler(ctx iris.Context) {
|
||||
ctx.ViewData("Title", "Index Page")
|
||||
ctx.View("index.html")
|
||||
}
|
||||
11
_examples/bootstrapper/routes/like.go
Normal file
11
_examples/bootstrapper/routes/like.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
13
_examples/bootstrapper/routes/routes.go
Normal file
13
_examples/bootstrapper/routes/routes.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12/_examples/bootstrap/bootstrap"
|
||||
)
|
||||
|
||||
// Configure registers the necessary routes to the app.
|
||||
func Configure(b *bootstrap.Bootstrapper) {
|
||||
b.Get("/", GetIndexHandler)
|
||||
b.Get("/follower/{id:int64}", GetFollowerHandler)
|
||||
b.Get("/following/{id:int64}", GetFollowingHandler)
|
||||
b.Get("/like/{id:int64}", GetLikeHandler)
|
||||
}
|
||||
1
_examples/bootstrapper/views/index.html
Normal file
1
_examples/bootstrapper/views/index.html
Normal file
@@ -0,0 +1 @@
|
||||
<h1>Welcome!!</h1>
|
||||
5
_examples/bootstrapper/views/shared/error.html
Normal file
5
_examples/bootstrapper/views/shared/error.html
Normal file
@@ -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/bootstrapper/views/shared/layout.html
Normal file
23
_examples/bootstrapper/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>
|
||||
Reference in New Issue
Block a user