diff --git a/_examples/README.md b/_examples/README.md index 09912183..1729a5df 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -16,17 +16,18 @@ It doesn't always contain the "best ways" but it does cover each important featu - [POC: Convert the medium-sized project "Parrot" from native to Iris](https://github.com/iris-contrib/parrot) - [POC: Isomorphic react/hot reloadable/redux/css-modules starter kit](https://github.com/kataras/iris-starter-kit) - [Tutorial: DropzoneJS Uploader](tutorial/dropzonejs) +- [Tutorial: Caddy](tutorial/caddy) ### Structuring Nothing stops you from using your favorite folder structure. Iris is a low level web framework, it has got MVC first-class support but it doesn't limit your folder structure, this is your choice. -Structuring depends on your own needs. We can't tell you how to design your own application for sure but you're free to take a closer look to the examples below; you may find something useful that you can borrow for your app +Structuring depends on your own needs. We can't tell you how to design your own application for sure but you're free to take a closer look to the examples below; you may find something useful that you can borrow for your app; -- [Example 1](mvc/login) -- [Example 2](structuring/mvc) -- [Example 3](structuring/handler-based) -- [Example 4](mvc/overview) +- [Bootstrapper](structuring/bootstrap) +- [MVC with Repository and Service layer Overview](structuring/mvc-plus-repository-and-service-layers) +- [Login (MVC with Single Responsibility package)](structuring/login-mvc-single-responsible-package) +- [Login (MVC with Datamodels, Datasource, Repository and Service layer)](structuring/login-mvc) ### HTTP Listening diff --git a/_examples/mvc/login/folder_structure.png b/_examples/mvc/login/folder_structure.png new file mode 100644 index 00000000..c89b273d Binary files /dev/null and b/_examples/mvc/login/folder_structure.png differ diff --git a/_examples/structuring/handler-based/bootstrap/bootstrapper.go b/_examples/structuring/bootstrap/bootstrap/bootstrapper.go similarity index 100% rename from _examples/structuring/handler-based/bootstrap/bootstrapper.go rename to _examples/structuring/bootstrap/bootstrap/bootstrapper.go diff --git a/_examples/structuring/bootstrap/folder_structure.png b/_examples/structuring/bootstrap/folder_structure.png new file mode 100644 index 00000000..8dfba29f Binary files /dev/null and b/_examples/structuring/bootstrap/folder_structure.png differ diff --git a/_examples/structuring/bootstrap/main.go b/_examples/structuring/bootstrap/main.go new file mode 100644 index 00000000..e299cb48 --- /dev/null +++ b/_examples/structuring/bootstrap/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/kataras/iris/_examples/structuring/bootstrap/bootstrap" + "github.com/kataras/iris/_examples/structuring/bootstrap/middleware/identity" + "github.com/kataras/iris/_examples/structuring/bootstrap/routes" +) + +var app = bootstrap.New("Awesome App", "kataras2006@hotmail.com", + identity.Configure, + routes.Configure, +) + +func init() { + app.Bootstrap() +} + +func main() { + app.Listen(":8080") +} diff --git a/_examples/structuring/handler-based/main_test.go b/_examples/structuring/bootstrap/main_test.go similarity index 96% rename from _examples/structuring/handler-based/main_test.go rename to _examples/structuring/bootstrap/main_test.go index ba51a081..e9d5065e 100644 --- a/_examples/structuring/handler-based/main_test.go +++ b/_examples/structuring/bootstrap/main_test.go @@ -6,7 +6,7 @@ import ( "github.com/kataras/iris/httptest" ) -// TestApp runs by `$ go test -v`. +// go test -v func TestApp(t *testing.T) { e := httptest.New(t, app.Application) diff --git a/_examples/structuring/handler-based/middleware/identity/identity.go b/_examples/structuring/bootstrap/middleware/identity/identity.go similarity index 91% rename from _examples/structuring/handler-based/middleware/identity/identity.go rename to _examples/structuring/bootstrap/middleware/identity/identity.go index 496d0de8..a163eb53 100644 --- a/_examples/structuring/handler-based/middleware/identity/identity.go +++ b/_examples/structuring/bootstrap/middleware/identity/identity.go @@ -5,7 +5,7 @@ import ( "github.com/kataras/iris" - "github.com/kataras/iris/_examples/structuring/handler-based/bootstrap" + "github.com/kataras/iris/_examples/structuring/bootstrap/bootstrap" ) // New returns a new handler which adds some headers and view data diff --git a/_examples/structuring/handler-based/public/favicon.ico b/_examples/structuring/bootstrap/public/favicon.ico similarity index 100% rename from _examples/structuring/handler-based/public/favicon.ico rename to _examples/structuring/bootstrap/public/favicon.ico diff --git a/_examples/structuring/handler-based/routes/follower.go b/_examples/structuring/bootstrap/routes/follower.go similarity index 100% rename from _examples/structuring/handler-based/routes/follower.go rename to _examples/structuring/bootstrap/routes/follower.go diff --git a/_examples/structuring/handler-based/routes/following.go b/_examples/structuring/bootstrap/routes/following.go similarity index 100% rename from _examples/structuring/handler-based/routes/following.go rename to _examples/structuring/bootstrap/routes/following.go diff --git a/_examples/structuring/handler-based/routes/index.go b/_examples/structuring/bootstrap/routes/index.go similarity index 100% rename from _examples/structuring/handler-based/routes/index.go rename to _examples/structuring/bootstrap/routes/index.go diff --git a/_examples/structuring/handler-based/routes/like.go b/_examples/structuring/bootstrap/routes/like.go similarity index 100% rename from _examples/structuring/handler-based/routes/like.go rename to _examples/structuring/bootstrap/routes/like.go diff --git a/_examples/structuring/handler-based/routes/routes.go b/_examples/structuring/bootstrap/routes/routes.go similarity index 80% rename from _examples/structuring/handler-based/routes/routes.go rename to _examples/structuring/bootstrap/routes/routes.go index 7d6c51c3..714dba5b 100644 --- a/_examples/structuring/handler-based/routes/routes.go +++ b/_examples/structuring/bootstrap/routes/routes.go @@ -1,7 +1,7 @@ package routes import ( - "github.com/kataras/iris/_examples/structuring/handler-based/bootstrap" + "github.com/kataras/iris/_examples/structuring/bootstrap/bootstrap" ) // Configure registers the necessary routes to the app. diff --git a/_examples/structuring/handler-based/views/index.html b/_examples/structuring/bootstrap/views/index.html similarity index 100% rename from _examples/structuring/handler-based/views/index.html rename to _examples/structuring/bootstrap/views/index.html diff --git a/_examples/structuring/handler-based/views/shared/error.html b/_examples/structuring/bootstrap/views/shared/error.html similarity index 100% rename from _examples/structuring/handler-based/views/shared/error.html rename to _examples/structuring/bootstrap/views/shared/error.html diff --git a/_examples/structuring/handler-based/views/shared/layout.html b/_examples/structuring/bootstrap/views/shared/layout.html similarity index 100% rename from _examples/structuring/handler-based/views/shared/layout.html rename to _examples/structuring/bootstrap/views/shared/layout.html diff --git a/_examples/structuring/handler-based/main.go b/_examples/structuring/handler-based/main.go deleted file mode 100644 index 06704fc5..00000000 --- a/_examples/structuring/handler-based/main.go +++ /dev/null @@ -1,20 +0,0 @@ -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") -} diff --git a/_examples/structuring/login-mvc-single-responsibility-package/folder_structure.png b/_examples/structuring/login-mvc-single-responsibility-package/folder_structure.png new file mode 100644 index 00000000..955e436e Binary files /dev/null and b/_examples/structuring/login-mvc-single-responsibility-package/folder_structure.png differ diff --git a/_examples/mvc/login/_ugly/main.go b/_examples/structuring/login-mvc-single-responsibility-package/main.go similarity index 91% rename from _examples/mvc/login/_ugly/main.go rename to _examples/structuring/login-mvc-single-responsibility-package/main.go index ea0f2851..f0e4eb14 100644 --- a/_examples/mvc/login/_ugly/main.go +++ b/_examples/structuring/login-mvc-single-responsibility-package/main.go @@ -3,7 +3,7 @@ package main import ( "time" - "github.com/kataras/iris/_examples/mvc/login/_ugly/user" + "github.com/kataras/iris/_examples/structuring/login-single-responsibility-package/user" "github.com/kataras/iris" "github.com/kataras/iris/sessions" diff --git a/_examples/mvc/login/_ugly/public/css/site.css b/_examples/structuring/login-mvc-single-responsibility-package/public/css/site.css similarity index 100% rename from _examples/mvc/login/_ugly/public/css/site.css rename to _examples/structuring/login-mvc-single-responsibility-package/public/css/site.css diff --git a/_examples/mvc/login/_ugly/user/auth.go b/_examples/structuring/login-mvc-single-responsibility-package/user/auth.go similarity index 100% rename from _examples/mvc/login/_ugly/user/auth.go rename to _examples/structuring/login-mvc-single-responsibility-package/user/auth.go diff --git a/_examples/mvc/login/_ugly/user/controller.go b/_examples/structuring/login-mvc-single-responsibility-package/user/controller.go similarity index 100% rename from _examples/mvc/login/_ugly/user/controller.go rename to _examples/structuring/login-mvc-single-responsibility-package/user/controller.go diff --git a/_examples/mvc/login/_ugly/user/datasource.go b/_examples/structuring/login-mvc-single-responsibility-package/user/datasource.go similarity index 100% rename from _examples/mvc/login/_ugly/user/datasource.go rename to _examples/structuring/login-mvc-single-responsibility-package/user/datasource.go diff --git a/_examples/mvc/login/_ugly/user/model.go b/_examples/structuring/login-mvc-single-responsibility-package/user/model.go similarity index 100% rename from _examples/mvc/login/_ugly/user/model.go rename to _examples/structuring/login-mvc-single-responsibility-package/user/model.go diff --git a/_examples/mvc/login/_ugly/views/shared/error.html b/_examples/structuring/login-mvc-single-responsibility-package/views/shared/error.html similarity index 100% rename from _examples/mvc/login/_ugly/views/shared/error.html rename to _examples/structuring/login-mvc-single-responsibility-package/views/shared/error.html diff --git a/_examples/mvc/login/_ugly/views/shared/layout.html b/_examples/structuring/login-mvc-single-responsibility-package/views/shared/layout.html similarity index 100% rename from _examples/mvc/login/_ugly/views/shared/layout.html rename to _examples/structuring/login-mvc-single-responsibility-package/views/shared/layout.html diff --git a/_examples/mvc/login/_ugly/views/user/login.html b/_examples/structuring/login-mvc-single-responsibility-package/views/user/login.html similarity index 100% rename from _examples/mvc/login/_ugly/views/user/login.html rename to _examples/structuring/login-mvc-single-responsibility-package/views/user/login.html diff --git a/_examples/mvc/login/_ugly/views/user/me.html b/_examples/structuring/login-mvc-single-responsibility-package/views/user/me.html similarity index 100% rename from _examples/mvc/login/_ugly/views/user/me.html rename to _examples/structuring/login-mvc-single-responsibility-package/views/user/me.html diff --git a/_examples/mvc/login/_ugly/views/user/notfound.html b/_examples/structuring/login-mvc-single-responsibility-package/views/user/notfound.html similarity index 100% rename from _examples/mvc/login/_ugly/views/user/notfound.html rename to _examples/structuring/login-mvc-single-responsibility-package/views/user/notfound.html diff --git a/_examples/mvc/login/_ugly/views/user/register.html b/_examples/structuring/login-mvc-single-responsibility-package/views/user/register.html similarity index 100% rename from _examples/mvc/login/_ugly/views/user/register.html rename to _examples/structuring/login-mvc-single-responsibility-package/views/user/register.html diff --git a/_examples/structuring/login-mvc/README.md b/_examples/structuring/login-mvc/README.md new file mode 100644 index 00000000..c6cbf0a5 --- /dev/null +++ b/_examples/structuring/login-mvc/README.md @@ -0,0 +1 @@ +# Please navigate to the [_examples/mvc/login](https://github.com/kataras/iris/tree/master/_examples/mvc/login) \ No newline at end of file diff --git a/_examples/structuring/mvc-plus-repository-and-service-layers/README.md b/_examples/structuring/mvc-plus-repository-and-service-layers/README.md new file mode 100644 index 00000000..692e63c6 --- /dev/null +++ b/_examples/structuring/mvc-plus-repository-and-service-layers/README.md @@ -0,0 +1 @@ +# Please navigate to the [_examples/mvc/overview](https://github.com/kataras/iris/tree/master/_examples/mvc/overview) \ No newline at end of file diff --git a/_examples/structuring/mvc/app/app.go b/_examples/structuring/mvc/app/app.go deleted file mode 100644 index cc778676..00000000 --- a/_examples/structuring/mvc/app/app.go +++ /dev/null @@ -1,144 +0,0 @@ -package app - -import ( - "fmt" - "time" - - "github.com/gorilla/securecookie" - - "github.com/kataras/iris" - "github.com/kataras/iris/middleware/logger" - "github.com/kataras/iris/middleware/recover" - "github.com/kataras/iris/sessions" - - "github.com/kataras/iris/_examples/structuring/mvc/app/controllers/follower" - "github.com/kataras/iris/_examples/structuring/mvc/app/controllers/following" - "github.com/kataras/iris/_examples/structuring/mvc/app/controllers/index" - "github.com/kataras/iris/_examples/structuring/mvc/app/controllers/like" -) - -// Application is our application wrapper and bootstrapper, keeps our settings. -type Application struct { - *iris.Application - - Name string - Owner string - SpawnDate time.Time - - Sessions *sessions.Sessions -} - -// NewApplication returns a new named Application. -func NewApplication(name, owner string) *Application { - return &Application{ - Name: name, - Owner: owner, - Application: iris.New(), - SpawnDate: time.Now(), - } -} - -// begin sends the app's identification info. -func (app *Application) begin(ctx iris.Context) { - // response headers - ctx.Header("App-Name", app.Name) - ctx.Header("App-Owner", app.Owner) - ctx.Header("App-Since", time.Since(app.SpawnDate).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", app.Name) - ctx.ViewData("AppOwner", app.Owner) - ctx.Next() -} - -// SetupViews loads the templates. -func (app *Application) SetupViews(viewsDir string) { - app.RegisterView(iris.HTML(viewsDir, ".html").Layout("shared/layout.html")) -} - -// SetupSessions initializes the sessions, optionally. -func (app *Application) SetupSessions(expires time.Duration, cookieHashKey, cookieBlockKey []byte) { - app.Sessions = sessions.New(sessions.Config{ - Cookie: "SECRET_SESS_COOKIE_" + app.Name, - Expires: expires, - Encoding: securecookie.New(cookieHashKey, cookieBlockKey), - }) -} - -// SetupErrorHandlers prepares the http error handlers (>=400). -// Remember that error handlers in Iris have their own middleware ecosystem -// so the route's middlewares are not running when an http error happened. -// So if we want a logger we have to re-create one, here we will customize that logger as well. -func (app *Application) SetupErrorHandlers() { - httpErrStatusLogger := logger.New(logger.Config{ - Status: true, - IP: true, - Method: true, - Path: true, - MessageContextKey: "message", - LogFunc: func(now time.Time, latency time.Duration, - status, ip, method, path string, - message interface{}) { - - line := fmt.Sprintf("%v %4v %s %s %s", status, latency, ip, method, path) - - if message != nil { - line += fmt.Sprintf(" %v", message) - } - app.Logger().Warn(line) - }, - }) - - app.OnAnyErrorCode(app.begin, httpErrStatusLogger, func(ctx iris.Context) { - err := iris.Map{ - "app": app.Name, - "status": ctx.GetStatusCode(), - "message": ctx.Values().GetString("message"), - } - - if jsonOutput, _ := ctx.URLParamBool("json"); jsonOutput { - ctx.JSON(err) - return - } - - ctx.ViewData("Err", err) - ctx.ViewData("Title", "Error") - ctx.View("shared/error.html") - }) -} - -// SetupRouter registers the available routes from the "controllers" package. -func (app *Application) SetupRouter() { - app.Use(recover.New()) - app.Use(app.begin) - app.Use(iris.Gzip) - - app.Favicon("./public/favicon.ico") - app.StaticWeb("/public", "./public") - - app.Use(logger.New()) - - app.Controller("/", new(index.Controller)) - app.Controller("/follower", new(follower.Controller)) - app.Controller("/following", new(following.Controller)) - app.Controller("/like", new(like.Controller)) -} - -// Instance is our global application bootstrap instance. -var Instance = NewApplication("My Awesome App", "kataras2006@hotmail.com") - -// Boot starts our default instance appolication. -func Boot(runner iris.Runner, configurators ...iris.Configurator) { - Instance.SetupViews("./app/views") - Instance.SetupSessions(24*time.Hour, - []byte("the-big-and-secret-fash-key-here"), - []byte("lot-secret-of-characters-big-too"), - ) - - Instance.SetupErrorHandlers() - Instance.SetupRouter() - - Instance.Run(runner, configurators...) -} diff --git a/_examples/structuring/mvc/app/controllers/follower/controller.go b/_examples/structuring/mvc/app/controllers/follower/controller.go deleted file mode 100644 index dd06ad13..00000000 --- a/_examples/structuring/mvc/app/controllers/follower/controller.go +++ /dev/null @@ -1,13 +0,0 @@ -package follower - -import ( - "github.com/kataras/iris" -) - -type Controller struct { - iris.Controller -} - -func (c *Controller) GetBy(id int64) { - c.Ctx.Writef("from "+c.Route().Path()+" with ID: %d", id) -} diff --git a/_examples/structuring/mvc/app/controllers/following/controller.go b/_examples/structuring/mvc/app/controllers/following/controller.go deleted file mode 100644 index 9de54d05..00000000 --- a/_examples/structuring/mvc/app/controllers/following/controller.go +++ /dev/null @@ -1,13 +0,0 @@ -package following - -import ( - "github.com/kataras/iris" -) - -type Controller struct { - iris.Controller -} - -func (c *Controller) GetBy(id int64) { - c.Ctx.Writef("from "+c.Route().Path()+" with ID: %d", id) -} diff --git a/_examples/structuring/mvc/app/controllers/index/controller.go b/_examples/structuring/mvc/app/controllers/index/controller.go deleted file mode 100644 index 40101fbb..00000000 --- a/_examples/structuring/mvc/app/controllers/index/controller.go +++ /dev/null @@ -1,14 +0,0 @@ -package index - -import ( - "github.com/kataras/iris" -) - -type Controller struct { - iris.Controller -} - -func (c *Controller) Get() { - c.Data["Title"] = "Index" - c.Tmpl = "index.html" -} diff --git a/_examples/structuring/mvc/app/controllers/like/controller.go b/_examples/structuring/mvc/app/controllers/like/controller.go deleted file mode 100644 index 851c3267..00000000 --- a/_examples/structuring/mvc/app/controllers/like/controller.go +++ /dev/null @@ -1,13 +0,0 @@ -package like - -import ( - "github.com/kataras/iris" -) - -type Controller struct { - iris.Controller -} - -func (c *Controller) GetBy(id int64) { - c.Ctx.Writef("from "+c.Route().Path()+" with ID: %d", id) -} diff --git a/_examples/structuring/mvc/app/views/index.html b/_examples/structuring/mvc/app/views/index.html deleted file mode 100644 index 4a813af6..00000000 --- a/_examples/structuring/mvc/app/views/index.html +++ /dev/null @@ -1 +0,0 @@ -