Skip to content

Commit

Permalink
update for Iris version 12.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Dec 13, 2019
1 parent b30cec8 commit 31c3cd2
Show file tree
Hide file tree
Showing 22 changed files with 394 additions and 315 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<a href="https://travis-ci.org/kataras/iris"><img src="https://img.shields.io/travis/kataras/iris.svg?style=flat-square" alt="Build Status"></a>
<a href="https://github.com/iris-contrib/examples/blob/v12/LICENSE"><img src="https://img.shields.io/badge/%20license-MIT%20%20License%20-E91E63.svg?style=flat-square" alt="License"></a>
<a href="https://github.com/kataras/iris/blob/v12/HISTORY.md"><img src="https://img.shields.io/badge/version-v12.0%20-blue.svg?style=flat-square" alt="CHANGELOG/HISTORY"></a>
<a href="https://github.com/kataras/iris/blob/v12/HISTORY.md"><img src="https://img.shields.io/badge/version-v12.1%20-blue.svg?style=flat-square" alt="CHANGELOG/HISTORY"></a>

This repository 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.

Expand Down Expand Up @@ -159,11 +159,16 @@ To read the Iris documentation please navigate to [the wiki pages](https://githu
* [ORM](orm)
* [Using xorm(Mysql, MyMysql, Postgres, Tidb, **SQLite**, MsSql, MsSql, Oracle)](orm/xorm/main.go)
* [Using gorm](orm/gorm/main.go)
* [Desktop App](desktop-app)
* [Blink](desktop-app/blink)
* [Lorca](desktop-app/lorca)
* [Webview](desktop-app/webview)
* [Miscellaneous](miscellaneous)
* [HTTP Method Override](https://github.com/kataras/iris/blob/master/middleware/methodoverride/methodoverride_test.go) **NEW**
* [Request Logger](http_request/request-logger/main.go)
* [log requests to a file](http_request/request-logger/request-logger-file/main.go)
* [Localization and Internationalization](miscellaneous/i18n/main.go)
* [Localization and Internationalization](i18n/main.go)
* [Sitemap.xml](sitemap/main.go)
* [Recovery](miscellaneous/recover/main.go)
* [Profiling (pprof)](miscellaneous/pprof/main.go)
* [Internal Application File Logger](miscellaneous/file-logger/main.go)
Expand All @@ -189,7 +194,7 @@ To read the Iris documentation please navigate to [the wiki pages](https://githu
* [Cookies](cookies)
* [Basic](cookies/basic/main.go)
* [Encode/Decode (securecookie)](cookies/securecookie/main.go)
[Sessions](sessions)
* [Sessions](sessions)
* [Overview](sessions/overview/main.go)
* [Middleware](sessions/middleware/main.go)
* [Secure Cookie](sessions/securecookie/main.go)
Expand Down
40 changes: 40 additions & 0 deletions desktop-app/blink/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"github.com/kataras/iris/v12"
"github.com/raintean/blink"
)

const addr = "127.0.0.1:8080"

/*
$ go build -ldflags -H=windowsgui -o myapp.exe # build for windows
$ ./myapp.exe # run the app
*/
func main() {
go runServer()
showAndWaitWindow()
}

func runServer() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<h1> Hello Desktop</h1>")
})
app.Run(iris.Addr(addr))
}

func showAndWaitWindow() {
blink.SetDebugMode(true)
if err := blink.InitBlink(); err != nil {
panic(err)
}

view := blink.NewWebView(false, 800, 600)
view.LoadURL(addr)
view.SetWindowTitle("My App")
view.MoveToCenter()
view.ShowWindow()

<-view.Destroy
}
40 changes: 40 additions & 0 deletions desktop-app/lorca/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"github.com/kataras/iris/v12"
"github.com/zserge/lorca"
)

const addr = "127.0.0.1:8080"

/*
$ go build -ldflags="-H windowsgui" -o myapp.exe # build for windows
$ ./myapp.exe # run
*/
func main() {
go runServer()
showAndWaitWindow()
}

func runServer() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<head><title>My App</title></head><body><h1>Hello Desktop</h1></body>")
})
app.Run(iris.Addr(addr))
}

func showAndWaitWindow() {
webview, err := lorca.New("http://"+addr, "", 800, 600)
if err != nil {
panic(err)
}
defer webview.Close()

// webview.SetBounds(lorca.Bounds{
// WindowState: lorca.WindowStateFullscreen,
// })

// Wait for the browser window to be closed
<-webview.Done()
}
39 changes: 39 additions & 0 deletions desktop-app/webview/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"github.com/kataras/iris/v12"
"github.com/zserge/webview"
)

const addr = "127.0.0.1:8080"

/*
# Windows requires special linker flags for GUI apps.
# It's also recommended to use TDM-GCC-64 compiler for CGo.
# http://tdm-gcc.tdragon.net/download
#
#
$ go build -ldflags="-H windowsgui" -o myapp.exe # build for windows
$ ./myapp.exe # run
#
#
# Note: if you see "use option -std=c99 or -std=gnu99 to compile your code"
# please refer to: https://github.com/zserge/webview/issues/188
*/
func main() {
go runServer()
showAndWaitWindow()
}

func runServer() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<h1> Hello Desktop</h1>")
})
app.Run(iris.Addr(addr))
}

func showAndWaitWindow() {
webview.Open("My App",
addr, 800, 600, true)
}
26 changes: 26 additions & 0 deletions i18n/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 mydomain.com
127.0.0.1 en.mydomain.com
127.0.0.1 el.mydomain.com
127.0.0.1 el-gr.mydomain.com
127.0.0.1 zh.mydomain.com
File renamed without changes.
File renamed without changes.
File renamed without changes.
89 changes: 89 additions & 0 deletions i18n/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"github.com/kataras/iris/v12"
)

func newApp() *iris.Application {
app := iris.New()

// Configure i18n.
// First parameter: Glob filpath patern,
// Second variadic parameter: Optional language tags, the first one is the default/fallback one.
app.I18n.Load("./locales/*/*.ini", "en-US", "el-GR", "zh-CN")
// app.I18n.LoadAssets for go-bindata.

// Default values:
// app.I18n.URLParameter = "lang"
// app.I18n.Subdomain = true
//
// Set to false to disallow path (local) redirects,
// see https://github.com/kataras/iris/issues/1369.
// app.I18n.PathRedirect = true

app.Get("/", func(ctx iris.Context) {
hi := ctx.Tr("hi", "iris")

locale := ctx.GetLocale()

ctx.Writef("From the language %s translated output: %s", locale.Language(), hi)
})

app.Get("/some-path", func(ctx iris.Context) {
ctx.Writef("%s", ctx.Tr("hi", "iris"))
})

app.Get("/other", func(ctx iris.Context) {
language := ctx.GetLocale().Language()

fromFirstFileValue := ctx.Tr("key1")
fromSecondFileValue := ctx.Tr("key2")
ctx.Writef("From the language: %s, translated output:\n%s=%s\n%s=%s",
language, "key1", fromFirstFileValue,
"key2", fromSecondFileValue)
})

// using in inside your views:
view := iris.HTML("./views", ".html")
app.RegisterView(view)

app.Get("/templates", func(ctx iris.Context) {
ctx.View("index.html", iris.Map{
"tr": ctx.Tr, // word, arguments... {call .tr "hi" "iris"}}
})

// Note that,
// Iris automatically adds a "tr" global template function as well,
// the only differene is the way you call it inside your templates and
// that it accepts a language code as its first argument: {{ tr "el-GR" "hi" "iris"}}
})
//

return app
}

func main() {
app := newApp()

// go to http://localhost:8080/el-gr/some-path
// ^ (by path prefix)
//
// or http://el.mydomain.com8080/some-path
// ^ (by subdomain - test locally with the hosts file)
//
// or http://localhost:8080/zh-CN/templates
// ^ (by path prefix with uppercase)
//
// or http://localhost:8080/some-path?lang=el-GR
// ^ (by url parameter)
//
// or http://localhost:8080 (default is en-US)
// or http://localhost:8080/?lang=zh-CN
//
// go to http://localhost:8080/other?lang=el-GR
// or http://localhost:8080/other (default is en-US)
// or http://localhost:8080/other?lang=en-US
//
// or use cookies to set the language.
app.Run(iris.Addr(":8080"), iris.WithSitemap("http://localhost:8080"))
}
86 changes: 86 additions & 0 deletions i18n/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"strings"
"testing"

"github.com/kataras/iris/v12/httptest"
)

func TestI18n(t *testing.T) {
app := newApp()

const (
expectedf = "From the language %s translated output: %s"

enUS = "hello, iris"
elGR = "γεια, iris"
zhCN = "您好,iris"
)

var (
tests = map[string]string{
"en-US": fmt.Sprintf(expectedf, "en-US", enUS),
"el-GR": fmt.Sprintf(expectedf, "el-GR", elGR),
"zh-CN": fmt.Sprintf(expectedf, "zh-CN", zhCN),
}

elgrMulti = fmt.Sprintf("From the language: %s, translated output:\n%s=%s\n%s=%s", "el-GR",
"key1",
"αυτό είναι μια τιμή από το πρώτο αρχείο: locale_multi_first",
"key2",
"αυτό είναι μια τιμή από το δεύτερο αρχείο μετάφρασης: locale_multi_second")
enusMulti = fmt.Sprintf("From the language: %s, translated output:\n%s=%s\n%s=%s", "en-US",
"key1",
"this is a value from the first file: locale_multi_first",
"key2",
"this is a value from the second file: locale_multi_second")
)

e := httptest.New(t, app)
// default should be en-US.
e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal(tests["en-US"])

for lang, body := range tests {
e.GET("/").WithQueryString("lang=" + lang).Expect().Status(httptest.StatusOK).
Body().Equal(body)

// test lowercase.
e.GET("/").WithQueryString("lang=" + strings.ToLower(lang)).Expect().Status(httptest.StatusOK).
Body().Equal(body)

// test first part (e.g. en instead of en-US).
langFirstPart := strings.Split(lang, "-")[0]
e.GET("/").WithQueryString("lang=" + langFirstPart).Expect().Status(httptest.StatusOK).
Body().Equal(body)

// test accept-language header prefix (i18n wrapper).
e.GET("/"+lang).WithHeader("Accept-Language", lang).Expect().Status(httptest.StatusOK).
Body().Equal(body)

// test path prefix (i18n router wrapper).
e.GET("/" + lang).Expect().Status(httptest.StatusOK).
Body().Equal(body)

// test path prefix with first part.
e.GET("/" + langFirstPart).Expect().Status(httptest.StatusOK).
Body().Equal(body)
}

e.GET("/other").WithQueryString("lang=el-GR").Expect().Status(httptest.StatusOK).
Body().Equal(elgrMulti)
e.GET("/other").WithQueryString("lang=en-US").Expect().Status(httptest.StatusOK).
Body().Equal(enusMulti)

// test path prefix (i18n router wrapper).
e.GET("/el-gr/other").Expect().Status(httptest.StatusOK).
Body().Equal(elgrMulti)
e.GET("/en/other").Expect().Status(httptest.StatusOK).
Body().Equal(enusMulti)

e.GET("/el-GRtemplates").Expect().Status(httptest.StatusNotFound)
e.GET("/el-templates").Expect().Status(httptest.StatusNotFound)

e.GET("/el/templates").Expect().Status(httptest.StatusOK).Body().Contains(elGR).Contains(zhCN)
}
9 changes: 9 additions & 0 deletions i18n/views/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h3>Test translate current locale template function <i>[dynamic]</i> ("word", arguments...) <br/> <code>call .tr "hi" "iris"</code></h3>

{{call .tr "hi" "iris"}}

<hr/>

<h3>Test translate of any language template function <i>[static]</i> ("language", "word", arguments...) <br/> <code>tr "zh-CN" "hi" "iris"</code></h3>

{{tr "zh-CN" "hi" "iris"}}
Loading

0 comments on commit 31c3cd2

Please sign in to comment.