Skip to content

Commit 31c3cd2

Browse files
committed
update for Iris version 12.1.0
1 parent b30cec8 commit 31c3cd2

22 files changed

+394
-315
lines changed

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<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>
66
<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>
7-
<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>
7+
<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>
88

99
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.
1010

@@ -159,11 +159,16 @@ To read the Iris documentation please navigate to [the wiki pages](https://githu
159159
* [ORM](orm)
160160
* [Using xorm(Mysql, MyMysql, Postgres, Tidb, **SQLite**, MsSql, MsSql, Oracle)](orm/xorm/main.go)
161161
* [Using gorm](orm/gorm/main.go)
162+
* [Desktop App](desktop-app)
163+
* [Blink](desktop-app/blink)
164+
* [Lorca](desktop-app/lorca)
165+
* [Webview](desktop-app/webview)
162166
* [Miscellaneous](miscellaneous)
163167
* [HTTP Method Override](https://github.com/kataras/iris/blob/master/middleware/methodoverride/methodoverride_test.go) **NEW**
164168
* [Request Logger](http_request/request-logger/main.go)
165169
* [log requests to a file](http_request/request-logger/request-logger-file/main.go)
166-
* [Localization and Internationalization](miscellaneous/i18n/main.go)
170+
* [Localization and Internationalization](i18n/main.go)
171+
* [Sitemap.xml](sitemap/main.go)
167172
* [Recovery](miscellaneous/recover/main.go)
168173
* [Profiling (pprof)](miscellaneous/pprof/main.go)
169174
* [Internal Application File Logger](miscellaneous/file-logger/main.go)
@@ -189,7 +194,7 @@ To read the Iris documentation please navigate to [the wiki pages](https://githu
189194
* [Cookies](cookies)
190195
* [Basic](cookies/basic/main.go)
191196
* [Encode/Decode (securecookie)](cookies/securecookie/main.go)
192-
[Sessions](sessions)
197+
* [Sessions](sessions)
193198
* [Overview](sessions/overview/main.go)
194199
* [Middleware](sessions/middleware/main.go)
195200
* [Secure Cookie](sessions/securecookie/main.go)

desktop-app/blink/main.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"github.com/kataras/iris/v12"
5+
"github.com/raintean/blink"
6+
)
7+
8+
const addr = "127.0.0.1:8080"
9+
10+
/*
11+
$ go build -ldflags -H=windowsgui -o myapp.exe # build for windows
12+
$ ./myapp.exe # run the app
13+
*/
14+
func main() {
15+
go runServer()
16+
showAndWaitWindow()
17+
}
18+
19+
func runServer() {
20+
app := iris.New()
21+
app.Get("/", func(ctx iris.Context) {
22+
ctx.HTML("<h1> Hello Desktop</h1>")
23+
})
24+
app.Run(iris.Addr(addr))
25+
}
26+
27+
func showAndWaitWindow() {
28+
blink.SetDebugMode(true)
29+
if err := blink.InitBlink(); err != nil {
30+
panic(err)
31+
}
32+
33+
view := blink.NewWebView(false, 800, 600)
34+
view.LoadURL(addr)
35+
view.SetWindowTitle("My App")
36+
view.MoveToCenter()
37+
view.ShowWindow()
38+
39+
<-view.Destroy
40+
}

desktop-app/lorca/main.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"github.com/kataras/iris/v12"
5+
"github.com/zserge/lorca"
6+
)
7+
8+
const addr = "127.0.0.1:8080"
9+
10+
/*
11+
$ go build -ldflags="-H windowsgui" -o myapp.exe # build for windows
12+
$ ./myapp.exe # run
13+
*/
14+
func main() {
15+
go runServer()
16+
showAndWaitWindow()
17+
}
18+
19+
func runServer() {
20+
app := iris.New()
21+
app.Get("/", func(ctx iris.Context) {
22+
ctx.HTML("<head><title>My App</title></head><body><h1>Hello Desktop</h1></body>")
23+
})
24+
app.Run(iris.Addr(addr))
25+
}
26+
27+
func showAndWaitWindow() {
28+
webview, err := lorca.New("http://"+addr, "", 800, 600)
29+
if err != nil {
30+
panic(err)
31+
}
32+
defer webview.Close()
33+
34+
// webview.SetBounds(lorca.Bounds{
35+
// WindowState: lorca.WindowStateFullscreen,
36+
// })
37+
38+
// Wait for the browser window to be closed
39+
<-webview.Done()
40+
}

desktop-app/webview/main.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"github.com/kataras/iris/v12"
5+
"github.com/zserge/webview"
6+
)
7+
8+
const addr = "127.0.0.1:8080"
9+
10+
/*
11+
# Windows requires special linker flags for GUI apps.
12+
# It's also recommended to use TDM-GCC-64 compiler for CGo.
13+
# http://tdm-gcc.tdragon.net/download
14+
#
15+
#
16+
$ go build -ldflags="-H windowsgui" -o myapp.exe # build for windows
17+
$ ./myapp.exe # run
18+
#
19+
#
20+
# Note: if you see "use option -std=c99 or -std=gnu99 to compile your code"
21+
# please refer to: https://github.com/zserge/webview/issues/188
22+
*/
23+
func main() {
24+
go runServer()
25+
showAndWaitWindow()
26+
}
27+
28+
func runServer() {
29+
app := iris.New()
30+
app.Get("/", func(ctx iris.Context) {
31+
ctx.HTML("<h1> Hello Desktop</h1>")
32+
})
33+
app.Run(iris.Addr(addr))
34+
}
35+
36+
func showAndWaitWindow() {
37+
webview.Open("My App",
38+
addr, 800, 600, true)
39+
}

i18n/hosts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 1993-2009 Microsoft Corp.
2+
#
3+
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
4+
#
5+
# This file contains the mappings of IP addresses to host names. Each
6+
# entry should be kept on an individual line. The IP address should
7+
# be placed in the first column followed by the corresponding host name.
8+
# The IP address and the host name should be separated by at least one
9+
# space.
10+
#
11+
# Additionally, comments (such as these) may be inserted on individual
12+
# lines or following the machine name denoted by a '#' symbol.
13+
#
14+
# For example:
15+
#
16+
# 102.54.94.97 rhino.acme.com # source server
17+
# 38.25.63.10 x.acme.com # x client host
18+
19+
# localhost name resolution is handled within DNS itself.
20+
# 127.0.0.1 localhost
21+
# ::1 localhost
22+
127.0.0.1 mydomain.com
23+
127.0.0.1 en.mydomain.com
24+
127.0.0.1 el.mydomain.com
25+
127.0.0.1 el-gr.mydomain.com
26+
127.0.0.1 zh.mydomain.com

i18n/main.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package main
2+
3+
import (
4+
"github.com/kataras/iris/v12"
5+
)
6+
7+
func newApp() *iris.Application {
8+
app := iris.New()
9+
10+
// Configure i18n.
11+
// First parameter: Glob filpath patern,
12+
// Second variadic parameter: Optional language tags, the first one is the default/fallback one.
13+
app.I18n.Load("./locales/*/*.ini", "en-US", "el-GR", "zh-CN")
14+
// app.I18n.LoadAssets for go-bindata.
15+
16+
// Default values:
17+
// app.I18n.URLParameter = "lang"
18+
// app.I18n.Subdomain = true
19+
//
20+
// Set to false to disallow path (local) redirects,
21+
// see https://github.com/kataras/iris/issues/1369.
22+
// app.I18n.PathRedirect = true
23+
24+
app.Get("/", func(ctx iris.Context) {
25+
hi := ctx.Tr("hi", "iris")
26+
27+
locale := ctx.GetLocale()
28+
29+
ctx.Writef("From the language %s translated output: %s", locale.Language(), hi)
30+
})
31+
32+
app.Get("/some-path", func(ctx iris.Context) {
33+
ctx.Writef("%s", ctx.Tr("hi", "iris"))
34+
})
35+
36+
app.Get("/other", func(ctx iris.Context) {
37+
language := ctx.GetLocale().Language()
38+
39+
fromFirstFileValue := ctx.Tr("key1")
40+
fromSecondFileValue := ctx.Tr("key2")
41+
ctx.Writef("From the language: %s, translated output:\n%s=%s\n%s=%s",
42+
language, "key1", fromFirstFileValue,
43+
"key2", fromSecondFileValue)
44+
})
45+
46+
// using in inside your views:
47+
view := iris.HTML("./views", ".html")
48+
app.RegisterView(view)
49+
50+
app.Get("/templates", func(ctx iris.Context) {
51+
ctx.View("index.html", iris.Map{
52+
"tr": ctx.Tr, // word, arguments... {call .tr "hi" "iris"}}
53+
})
54+
55+
// Note that,
56+
// Iris automatically adds a "tr" global template function as well,
57+
// the only differene is the way you call it inside your templates and
58+
// that it accepts a language code as its first argument: {{ tr "el-GR" "hi" "iris"}}
59+
})
60+
//
61+
62+
return app
63+
}
64+
65+
func main() {
66+
app := newApp()
67+
68+
// go to http://localhost:8080/el-gr/some-path
69+
// ^ (by path prefix)
70+
//
71+
// or http://el.mydomain.com8080/some-path
72+
// ^ (by subdomain - test locally with the hosts file)
73+
//
74+
// or http://localhost:8080/zh-CN/templates
75+
// ^ (by path prefix with uppercase)
76+
//
77+
// or http://localhost:8080/some-path?lang=el-GR
78+
// ^ (by url parameter)
79+
//
80+
// or http://localhost:8080 (default is en-US)
81+
// or http://localhost:8080/?lang=zh-CN
82+
//
83+
// go to http://localhost:8080/other?lang=el-GR
84+
// or http://localhost:8080/other (default is en-US)
85+
// or http://localhost:8080/other?lang=en-US
86+
//
87+
// or use cookies to set the language.
88+
app.Run(iris.Addr(":8080"), iris.WithSitemap("http://localhost:8080"))
89+
}

i18n/main_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/kataras/iris/v12/httptest"
9+
)
10+
11+
func TestI18n(t *testing.T) {
12+
app := newApp()
13+
14+
const (
15+
expectedf = "From the language %s translated output: %s"
16+
17+
enUS = "hello, iris"
18+
elGR = "γεια, iris"
19+
zhCN = "您好,iris"
20+
)
21+
22+
var (
23+
tests = map[string]string{
24+
"en-US": fmt.Sprintf(expectedf, "en-US", enUS),
25+
"el-GR": fmt.Sprintf(expectedf, "el-GR", elGR),
26+
"zh-CN": fmt.Sprintf(expectedf, "zh-CN", zhCN),
27+
}
28+
29+
elgrMulti = fmt.Sprintf("From the language: %s, translated output:\n%s=%s\n%s=%s", "el-GR",
30+
"key1",
31+
"αυτό είναι μια τιμή από το πρώτο αρχείο: locale_multi_first",
32+
"key2",
33+
"αυτό είναι μια τιμή από το δεύτερο αρχείο μετάφρασης: locale_multi_second")
34+
enusMulti = fmt.Sprintf("From the language: %s, translated output:\n%s=%s\n%s=%s", "en-US",
35+
"key1",
36+
"this is a value from the first file: locale_multi_first",
37+
"key2",
38+
"this is a value from the second file: locale_multi_second")
39+
)
40+
41+
e := httptest.New(t, app)
42+
// default should be en-US.
43+
e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal(tests["en-US"])
44+
45+
for lang, body := range tests {
46+
e.GET("/").WithQueryString("lang=" + lang).Expect().Status(httptest.StatusOK).
47+
Body().Equal(body)
48+
49+
// test lowercase.
50+
e.GET("/").WithQueryString("lang=" + strings.ToLower(lang)).Expect().Status(httptest.StatusOK).
51+
Body().Equal(body)
52+
53+
// test first part (e.g. en instead of en-US).
54+
langFirstPart := strings.Split(lang, "-")[0]
55+
e.GET("/").WithQueryString("lang=" + langFirstPart).Expect().Status(httptest.StatusOK).
56+
Body().Equal(body)
57+
58+
// test accept-language header prefix (i18n wrapper).
59+
e.GET("/"+lang).WithHeader("Accept-Language", lang).Expect().Status(httptest.StatusOK).
60+
Body().Equal(body)
61+
62+
// test path prefix (i18n router wrapper).
63+
e.GET("/" + lang).Expect().Status(httptest.StatusOK).
64+
Body().Equal(body)
65+
66+
// test path prefix with first part.
67+
e.GET("/" + langFirstPart).Expect().Status(httptest.StatusOK).
68+
Body().Equal(body)
69+
}
70+
71+
e.GET("/other").WithQueryString("lang=el-GR").Expect().Status(httptest.StatusOK).
72+
Body().Equal(elgrMulti)
73+
e.GET("/other").WithQueryString("lang=en-US").Expect().Status(httptest.StatusOK).
74+
Body().Equal(enusMulti)
75+
76+
// test path prefix (i18n router wrapper).
77+
e.GET("/el-gr/other").Expect().Status(httptest.StatusOK).
78+
Body().Equal(elgrMulti)
79+
e.GET("/en/other").Expect().Status(httptest.StatusOK).
80+
Body().Equal(enusMulti)
81+
82+
e.GET("/el-GRtemplates").Expect().Status(httptest.StatusNotFound)
83+
e.GET("/el-templates").Expect().Status(httptest.StatusNotFound)
84+
85+
e.GET("/el/templates").Expect().Status(httptest.StatusOK).Body().Contains(elGR).Contains(zhCN)
86+
}

i18n/views/index.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h3>Test translate current locale template function <i>[dynamic]</i> ("word", arguments...) <br/> <code>call .tr "hi" "iris"</code></h3>
2+
3+
{{call .tr "hi" "iris"}}
4+
5+
<hr/>
6+
7+
<h3>Test translate of any language template function <i>[static]</i> ("language", "word", arguments...) <br/> <code>tr "zh-CN" "hi" "iris"</code></h3>
8+
9+
{{tr "zh-CN" "hi" "iris"}}

0 commit comments

Comments
 (0)