1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-11 14:05:59 +00:00

one for today, one for tomorrow and ready :)

Former-commit-id: 2e0a335c84c1d9c57a61b7f3203e54f7cccd7dcc
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-30 20:40:52 +02:00
parent 75d710ce90
commit 64da28af8b
18 changed files with 320 additions and 2138 deletions

View File

@@ -11,6 +11,7 @@ It doesn't always contain the "best ways" but it does cover each important featu
- [Hello world!](hello-world/main.go)
- [Glimpse](overview/main.go)
- [Tutorial: Online Visitors](tutorial/online-visitors/main.go)
- [Tutorial: Vue.js Todo MVC](tutorial/vuejs-todo-mvc)
- [Tutorial: URL Shortener using BoltDB](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7)
- [Tutorial: How to turn your Android Device into a fully featured Web Server (**MUST**)](https://twitter.com/ThePracticalDev/status/892022594031017988)
- [POC: Convert the medium-sized project "Parrot" from native to Iris](https://github.com/iris-contrib/parrot)
@@ -18,7 +19,6 @@ It doesn't always contain the "best ways" but it does cover each important featu
- [Tutorial: DropzoneJS Uploader](tutorial/dropzonejs)
- [Tutorial: Caddy](tutorial/caddy)
- [Tutorial:Iris Go Framework + MongoDB](https://medium.com/go-language/iris-go-framework-mongodb-552e349eab9c)
### 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.
@@ -253,17 +253,7 @@ Follow the examples below,
- [Login showcase - Plus Repository and Service layers](mvc/login) **UPDATED**
- [Singleton](mvc/singleton) **NEW**
- [Websocket Controller](mvc/websocket) **NEW**
<!--
Why updated?
Old method works, as promised no breaking changes.
But mvc.C as controller marker and mvc.Result on method functions return value
is more lightweight and faster than `mvc.Controller` because `mvc.Controller` initializes
some fields like `Data, Path`... and Data is a map even if not used, at the opossite hand
`mvc.C` just initializes the context `Ctx` field, the dev has all the `mvc.Controller`'s features
by the `mvc.Result` built'n types like `mvc.Response` and `mvc.View` PLUS she/he can
convert any custom type into a response dispatcher by implementing the `mvc.Result` interface.
-->
- [Vue.js Todo MVC](tutorial/vuejs-todo-mvc) **NEW**
### Subdomains

View File

@@ -9,6 +9,7 @@ import (
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
// Optionally, add two built'n handlers
// that can recover from any http-relative panics
// and log the requests to the terminal.

View File

@@ -31,38 +31,41 @@ func main() {
})
// Handle the post request from the upload_form.html to the server
app.Post("/upload", iris.LimitRequestBodySize(10<<20),
func(ctx iris.Context) {
// or use ctx.SetMaxRequestBodySize(10 << 20)
// to limit the uploaded file(s) size.
app.Post("/upload", func(ctx iris.Context) {
// iris.LimitRequestBodySize(32 <<20) as middleware to a route
// or use ctx.SetMaxRequestBodySize(32 << 20)
// to limit the whole request body size,
//
// or let the configuration option at app.Run for global setting
// for POST/PUT methods, including uploads of course.
// Get the file from the request
file, info, err := ctx.FormFile("uploadfile")
// Get the file from the request.
file, info, err := ctx.FormFile("uploadfile")
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
return
}
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
return
}
defer file.Close()
fname := info.Filename
defer file.Close()
fname := info.Filename
// Create a file with the same name
// assuming that you have a folder named 'uploads'
out, err := os.OpenFile("./uploads/"+fname,
os.O_WRONLY|os.O_CREATE, 0666)
// Create a file with the same name
// assuming that you have a folder named 'uploads'
out, err := os.OpenFile("./uploads/"+fname,
os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
return
}
defer out.Close()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
return
}
defer out.Close()
io.Copy(out, file)
})
io.Copy(out, file)
})
// start the server at http://localhost:8080
app.Run(iris.Addr(":8080"))
// start the server at http://localhost:8080 with post limit at 32 MB.
app.Run(iris.Addr(":8080"), iris.WithPostMaxMemory(32<<20))
}

View File

@@ -32,17 +32,25 @@ func TodoApp(app *mvc.Application) {
&prefixedLogger{prefix: "DEV"},
)
// GET: http://localhost:8080/todo
// GET: http://localhost:8080/todo/custom
app.Handle(new(TodoController))
// All dependencies of the parent *mvc.Application
// are cloned to that new child, thefore it has access to the same session as well.
// are cloned to this new child,
// thefore it has access to the same session as well.
// GET: http://localhost:8080/todo/sub
app.Party("/sub").
Handle(new(TodoSubController))
}
// If controller's fields (or even its functions) expecting an interface
// but a struct value is binded then it will check if that struct value implements
// the interface and if true then it will bind it as expected.
// but a struct value is binded then it will check
// if that struct value implements
// the interface and if true then it will add this to the
// available bindings, as expected, before the server ran of course,
// remember? Iris always uses the best possible way to reduce load
// on serving web resources.
type LoggerService interface {
Log(string)

View File

@@ -65,7 +65,7 @@ func (c *websocketController) update() {
// visits++
newCount := increment()
// This will call the "visit" event on all clients, incuding the current
// This will call the "visit" event on all clients, including the current
// with the 'newCount' variable.
//
// There are many ways that u can do it and faster, for example u can just send a new visitor

View File

@@ -82,7 +82,7 @@ func (c *Controller) PostRegister(form formValue) mvc.Result {
// you can use variables-- that are initialized before server start
// so you can win some time on serving.
// You can do it else where as well but I let them as pracise for you,
// essentialy you can understand by just looking below.
// essentially you can understand by just looking below.
var userLoginView = mvc.View{
Name: PathLogin.Path + ".html",
Data: page{"User Login"},

View File

@@ -1 +1,43 @@
# Unfinished - wait until today :)
# A Todo MVC Application using Iris and Vue.js
## The Tools
Programming Languages are just tools for us, but we need a safe, fast and “cross-platform” programming language to power our service.
[Go](https://golang.org) is a [rapidly growing](https://www.tiobe.com/tiobe-index/) open source programming language designed for building simple, fast, and reliable software. Take a look [here](https://github.com/golang/go/wiki/GoUsers) which great companies use Go to power their services.
### Install the Go Programming Language
Extensive information about downloading & installing Go can be found [here](https://golang.org/dl/).
[![](https://i3.ytimg.com/vi/9x-pG3lvLi0/hqdefault.jpg)](https://youtu.be/9x-pG3lvLi0)
> Maybe [Windows](https://www.youtube.com/watch?v=WT5mTznJBS0) or [Mac OS X](https://www.youtube.com/watch?v=5qI8z_lB5Lw) user?
> The article does not contain an introduction to the language itself, if youre a newcomer I recommend you to bookmark this article, [learn](https://github.com/golang/go/wiki/Learn) the languages fundamentals and come back later on.
## The Dependencies
Many articles have been written, in the past, that lead developers not to use a web framework because they are useless and "bad". I have to tell you that there is no such thing, it always depends on the (web) framework that youre going to use. At production environment, we dont have the time or the experience to code everything that we wanna use in the applications, and if we could are we sure that we can do better and safely than others? In short term: **Good frameworks are helpful tools for any developer, company or startup and "bad" frameworks are waste of time, crystal clear.**
Youll need only two dependencies:
1. The Iris Web Framework, for our server-side requirements. Can be found [here](https://github.com/kataras/iris)
2. Vue.js, for our client-side requirements. Download it from [here](https://vuejs.org/)
> If you have Go already installed then just execute `go get -u github.com/kataras/iris` to install the Iris Web Framework.
## Start
If we are all in the same page, its time to learn how we can create a live todo application that will be easy to deploy and extend even more!
We're going to use a vue.js todo application which uses browser'
s local storage and doesn't have any user-specified features like live sync between browser's tabs, you can find the original version inside the vue's [docs](https://vuejs.org/v2/examples/todomvc.html).
### The client-side (vue.js)
### The server-side (iris)
## References
https://vuejs.org/v2/examples/todomvc.html (using browser's local storage)

View File

@@ -85,7 +85,7 @@ func SendMessage(serverID, to, method, message string) error {
// SendtBytes broadcast a message to server
func SendtBytes(serverID, to, method string, message []byte) error {
// look https://github.com/kataras/iris/tree/master/adaptors/websocket/message.go , client.go and client.js
// look https://github.com/kataras/iris/blob/master/websocket/message.go , client.go and client.js
// to understand the buffer line:
buffer := []byte(fmt.Sprintf("iris-websocket-message:%v;0;%v;%v;", method, serverID, to))
buffer = append(buffer, message...)