diff --git a/README.md b/README.md index 55247214..595e36ff 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Feature Overview - Automatically install and serve certificates from https://letsencrypt.org - Robust routing and middleware ecosystem - Build RESTful APIs -- Choose your favorite routes' path syntax between [httprouter](https://github.com/kataras/iris/blob/v6/adaptors/httprouter/_example/main.go) and [gorillamux](https://github.com/kataras/iris/blob/v6/adaptors/gorillamux/_example/main.go) +- Choose your favorite routes' path syntax between [httprouter](https://github.com/kataras/iris/blob/v6/_examples/beginner/routes-using-httprouter/main.go) and [gorillamux](https://github.com/kataras/iris/blob/v6/_examples/beginner/routes-using-gorillamux/main.go) - Request-Scoped Transactions - Group API's and subdomains with wildcard support - Body binding for JSON, XML, Forms, can be extended to use your own custom binders @@ -275,12 +275,9 @@ FAQ & Documentation 4. [Creating A URL Shortener Service Using Go, Iris, and Bolt](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7) -5. [./adaptors](https://github.com/kataras/iris/tree/v6/adaptors) and [./middleware](https://github.com/kataras/iris/tree/v6/middleware) contains examples of their usage +5. [Godocs](https://godoc.org/gopkg.in/kataras/iris.v6) for deep documentation -6. [Godocs](https://godoc.org/gopkg.in/kataras/iris.v6) for deep documentation - - -7. [HISTORY.md](https://github.com//kataras/iris/tree/v6/HISTORY.md) is your best friend, version migrations are released there +6. [HISTORY.md](https://github.com//kataras/iris/tree/v6/HISTORY.md) is your best friend, version migrations are released there I'll be glad to talk with you about **your awesome feature requests**, @@ -480,14 +477,14 @@ i.e: - [transaction_test.go](https://github.com/kataras/iris/blob/v6/transaction_test.go) - [serializer_test.go](https://github.com/kataras/iris/blob/v6/serializer_test.go) -A simple test is located to [./httptest/_example/main_test.go](https://github.com/kataras/iris/blob/v6/httptest/_example/main_test.go) +A simple test is located to [./_examples/advanced/httptest/main_test.go](https://github.com/kataras/iris/blob/v6/_examples/advanced/httptest/main_test.go) Philosophy ------------ The Iris philosophy is to provide robust tooling for HTTP, making it a great solution for single page applications, web sites, hybrids, or public HTTP APIs. Keep note that, today, iris is faster than nginx itself. -Iris does not force you to use any specific ORM or template engine. Iris is routerless which means you can adapt any router you like, [httprouter](https://github.com/kataras/iris/blob/v6/adaptors/httprouter/_example/main.go) is the fastest, [gorillamux](https://github.com/kataras/iris/blob/v6/adaptors/gorillamux/_example/main.go) has more features. With support for the most used template engines (5), you can quickly craft the perfect application. +Iris does not force you to use any specific ORM or template engine. Iris is routerless which means you can adapt any router you like, [httprouter](https://github.com/kataras/iris/blob/v6/_examples/beginner/routes-using-httprouter/main.go) is the fastest, [gorillamux](https://github.com/kataras/iris/blob/v6/_examples/beginner/routes-using-gorillamux/main.go) has more features. With support for the most used template engines (5), you can quickly craft the perfect application. People diff --git a/_examples/README.md b/_examples/README.md index 04fb335a..555403d7 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -1,4 +1,4 @@ -## Examples +# Examples This folder provides easy to understand code snippets on how to get started with web development with the Go programming language using the [Iris](https://github.com/kataras/iris) web framework. @@ -8,26 +8,60 @@ It doesn't contains "best ways" neither explains all its features. It's just a s Developers should read the official [documentation](https://godoc.org/gopkg.in/kataras/iris.v6) in depth. - + ## Table of Contents -* [Hello World](examples/hello-world/main.go) -* [Routes (using httprouter)](examples/routes-using-httprouter/main.go) -* [Routes (using gorillamux)](examples/routes-using-gorillamux/main.go) -* [Templates](examples/templates/main.go) -* [Forms](examples/forms/main.go) -* [JSON](examples/json/main.go) -* [Upload Files](examples/upload-files/main.go) -* [Static Files](examples/static-files/main.go) -* [Favicon](examples/favicon/main.go) -* [Password Hashing](examples/password-hashing/main.go) -* [Sessions](examples/sessions/main.go) -* [Websockets](examples/websockets/main.go) -* [Markdown and Cache](examples/cache-markdown/main.go) -* [Online Visitors](examples/online-visitors/main.go) -* [URL Shortener using BoltDB](examples/url-shortener/main.go) +* [Level: Beginner](examples/beginner) + * [Hello World](examples/beginner/hello-world/main.go) + * [Routes (using httprouter)](examples/beginner/routes-using-httprouter/main.go) + * [Routes (using gorillamux)](examples/beginner/routes-using-gorillamux/main.go) + * [Write JSON](examples/beginner/write-json/main.go) + * [Read JSON](examples/beginner/read-json/main.go) + * [Read Form](examples/beginner/read-form/main.go) + * [Favicon](examples/beginner/favicon/main.go) + * [File Server](examples/beginner/file-server/main.go) + * [Send Files](examples/beginner/send-files/main.go) +* [Level: Intermediate](examples/intermediate) + * [Send An E-mail](examples/intermediate/e-mail/main.go) + * [Upload/Read Files](examples/intermediate/upload-files/main.go) + * [Request Logger](examples/intermediate/request-logger/main.go) + * [Profiling (pprof)](examples/intermediate/pprof/main.go) + * [Basic Authentication](examples/intermediate/basicauth/main.go) + * [HTTP Access Control](examples/intermediate/cors/main.go) + * [Cache Markdown](examples/intermediate/cache-markdown/main.go) + * [Localization and Internationalization](examples/intermediate/i18n/main.go) + * [Recovery](examples/intermediate/recover/main.go) + * [Graceful Shutdown](examples/intermediate/graceful-shutdown/main.go) + * [View Engine](examples/intermediate/view) + * [Overview](examples/intermediate/view/overview/main.go) + * [Embedding Templates Into Executable](examples/intermediate/embedding-templates-into-app) + * [Template HTML: Part Zero](examples/intermediate/view/template_html_0/main.go) + * [Template HTML: Part One](examples/intermediate/view/template_html_1/main.go) + * [Template HTML: Part Two](examples/intermediate/view/template_html_2/main.go) + * [Template HTML: Part Three](examples/intermediate/view/template_html_3/main.go) + * [Template HTML: Part Four](examples/intermediate/view/template_html_4/main.go) + * [Custom Renderer](examples/intermediate/view/custom-renderer/main.go) + * [Password Hashing](examples/intermediate/password-hashing/main.go) + * [Sessions](examples/intermediate/sessions) + * [Overview](examples/intermediate/sessions/overview/main.go) + * [Encoding & Decoding the Session ID: Secure Cookie](examples/intermediate/sessions/securecookie/main.go) + * [Standalone](examples/intermediate/sessions/standalone/main.go) + * [With A Back-End Database](examples/intermediate/sessions/database/main.go) + * [Flash Messages](examples/intermediate/flash-messages/main.go) + * [Websockets](examples/intermediate/websockets) + * [Ridiculous Simple](examples/intermediate/websockets/ridiculous-simple/main.go) + * [Overview](examples/intermediate/websockets/overview/main.go) + * [Connection List](examples/intermediate/websockets/connectionlist/main.go) + * [Native Messages](examples/intermediate/websockets/naive-messages/main.go) + * [Secure](examples/intermediate/websockets/secure/main.go) + * [Custom Go Client](examples/intermediate/websockets/custom-go-client/main.go) +* [Level: Advanced](examples/advanced) + * [HTTP Testing](examples/advanced/httptest/main_test.go) + * [Watch & Compile Typescript source files](examples/advanced/typescript/main.go) + * [Cloud Editor](examples/advanced/cloud-editor/main.go) + * [Online Visitors](examples/advanced/online-visitors/main.go) + * [URL Shortener using BoltDB](examples/advanced/url-shortener/main.go) - -> Take look at the [community examples](https://github.com/iris-contrib/examples) too! \ No newline at end of file +> Take look at the [community examples](https://github.com/iris-contrib/examples) too! diff --git a/adaptors/typescript/editor/_example/main.go b/_examples/examples/advanced/cloud-editor/main.go similarity index 100% rename from adaptors/typescript/editor/_example/main.go rename to _examples/examples/advanced/cloud-editor/main.go diff --git a/adaptors/typescript/_example/www/index.html b/_examples/examples/advanced/cloud-editor/www/index.html similarity index 100% rename from adaptors/typescript/_example/www/index.html rename to _examples/examples/advanced/cloud-editor/www/index.html diff --git a/adaptors/typescript/editor/_example/www/scripts/app.ts b/_examples/examples/advanced/cloud-editor/www/scripts/app.ts similarity index 100% rename from adaptors/typescript/editor/_example/www/scripts/app.ts rename to _examples/examples/advanced/cloud-editor/www/scripts/app.ts diff --git a/adaptors/typescript/editor/_example/www/scripts/tsconfig.json b/_examples/examples/advanced/cloud-editor/www/scripts/tsconfig.json similarity index 100% rename from adaptors/typescript/editor/_example/www/scripts/tsconfig.json rename to _examples/examples/advanced/cloud-editor/www/scripts/tsconfig.json diff --git a/httptest/_example/main.go b/_examples/examples/advanced/httptest/main.go similarity index 100% rename from httptest/_example/main.go rename to _examples/examples/advanced/httptest/main.go diff --git a/httptest/_example/main_test.go b/_examples/examples/advanced/httptest/main_test.go similarity index 100% rename from httptest/_example/main_test.go rename to _examples/examples/advanced/httptest/main_test.go diff --git a/_examples/examples/online-visitors/main.go b/_examples/examples/advanced/online-visitors/main.go similarity index 100% rename from _examples/examples/online-visitors/main.go rename to _examples/examples/advanced/online-visitors/main.go diff --git a/_examples/examples/online-visitors/static/assets/js/visitors.js b/_examples/examples/advanced/online-visitors/static/assets/js/visitors.js similarity index 100% rename from _examples/examples/online-visitors/static/assets/js/visitors.js rename to _examples/examples/advanced/online-visitors/static/assets/js/visitors.js diff --git a/_examples/examples/online-visitors/templates/index.html b/_examples/examples/advanced/online-visitors/templates/index.html similarity index 100% rename from _examples/examples/online-visitors/templates/index.html rename to _examples/examples/advanced/online-visitors/templates/index.html diff --git a/_examples/examples/online-visitors/templates/other.html b/_examples/examples/advanced/online-visitors/templates/other.html similarity index 100% rename from _examples/examples/online-visitors/templates/other.html rename to _examples/examples/advanced/online-visitors/templates/other.html diff --git a/adaptors/typescript/_example/main.go b/_examples/examples/advanced/typescript/main.go similarity index 100% rename from adaptors/typescript/_example/main.go rename to _examples/examples/advanced/typescript/main.go diff --git a/adaptors/typescript/editor/_example/www/index.html b/_examples/examples/advanced/typescript/www/index.html similarity index 100% rename from adaptors/typescript/editor/_example/www/index.html rename to _examples/examples/advanced/typescript/www/index.html diff --git a/adaptors/typescript/_example/www/scripts/app.ts b/_examples/examples/advanced/typescript/www/scripts/app.ts similarity index 100% rename from adaptors/typescript/_example/www/scripts/app.ts rename to _examples/examples/advanced/typescript/www/scripts/app.ts diff --git a/_examples/examples/url-shortener/main.go b/_examples/examples/advanced/url-shortener/main.go similarity index 100% rename from _examples/examples/url-shortener/main.go rename to _examples/examples/advanced/url-shortener/main.go diff --git a/_examples/examples/url-shortener/resources/css/style.css b/_examples/examples/advanced/url-shortener/resources/css/style.css similarity index 100% rename from _examples/examples/url-shortener/resources/css/style.css rename to _examples/examples/advanced/url-shortener/resources/css/style.css diff --git a/_examples/examples/url-shortener/shortener.go b/_examples/examples/advanced/url-shortener/shortener.go similarity index 100% rename from _examples/examples/url-shortener/shortener.go rename to _examples/examples/advanced/url-shortener/shortener.go diff --git a/_examples/examples/url-shortener/templates/index.html b/_examples/examples/advanced/url-shortener/templates/index.html similarity index 100% rename from _examples/examples/url-shortener/templates/index.html rename to _examples/examples/advanced/url-shortener/templates/index.html diff --git a/_examples/examples/favicon/main.go b/_examples/examples/beginner/favicon/main.go similarity index 100% rename from _examples/examples/favicon/main.go rename to _examples/examples/beginner/favicon/main.go diff --git a/_examples/examples/favicon/static/favicons/iris_favicon_32_32.ico b/_examples/examples/beginner/favicon/static/favicons/iris_favicon_32_32.ico similarity index 100% rename from _examples/examples/favicon/static/favicons/iris_favicon_32_32.ico rename to _examples/examples/beginner/favicon/static/favicons/iris_favicon_32_32.ico diff --git a/_examples/examples/static-files/assets/css/styles.css b/_examples/examples/beginner/file-server/assets/css/styles.css similarity index 100% rename from _examples/examples/static-files/assets/css/styles.css rename to _examples/examples/beginner/file-server/assets/css/styles.css diff --git a/_examples/examples/static-files/main.go b/_examples/examples/beginner/file-server/main.go similarity index 100% rename from _examples/examples/static-files/main.go rename to _examples/examples/beginner/file-server/main.go diff --git a/_examples/examples/hello-world/main.go b/_examples/examples/beginner/hello-world/main.go similarity index 100% rename from _examples/examples/hello-world/main.go rename to _examples/examples/beginner/hello-world/main.go diff --git a/_examples/examples/beginner/read-form/main.go b/_examples/examples/beginner/read-form/main.go new file mode 100644 index 00000000..f659e195 --- /dev/null +++ b/_examples/examples/beginner/read-form/main.go @@ -0,0 +1,42 @@ +// package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON +package main + +import ( + "gopkg.in/kataras/iris.v6" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" + "gopkg.in/kataras/iris.v6/adaptors/view" +) + +type Visitor struct { + Username string + Mail string + Data []string `form:"mydata"` +} + +func main() { + app := iris.New() + // output startup banner and error logs on os.Stdout + app.Adapt(iris.DevLogger()) + // set the router, you can choose gorillamux too + app.Adapt(httprouter.New()) + // set the view html template engine + app.Adapt(view.HTML("./templates", ".html")) + + app.Get("/", func(ctx *iris.Context) { + if err := ctx.Render("form.html", nil); err != nil { + ctx.Log(iris.DevMode, err.Error()) + } + }) + + app.Post("/form_action", func(ctx *iris.Context) { + visitor := Visitor{} + err := ctx.ReadForm(&visitor) + if err != nil { + ctx.Log(iris.DevMode, "Error when reading form: "+err.Error()) + } + + ctx.Writef("Visitor: %#v", visitor) + }) + + app.Listen(":8080") +} diff --git a/_examples/examples/beginner/read-form/templates/form.html b/_examples/examples/beginner/read-form/templates/form.html new file mode 100644 index 00000000..af68b075 --- /dev/null +++ b/_examples/examples/beginner/read-form/templates/form.html @@ -0,0 +1,19 @@ + +
+ + + + + +