Skip to content

Commit e559b12

Browse files
committed
sync examples
1 parent c104e9a commit e559b12

File tree

500 files changed

+10343
-49361
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

500 files changed

+10343
-49361
lines changed

.travis.gofmt.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
if [ -n "$(gofmt -l .)" ]; then
4+
echo "Go code is not formatted:"
5+
gofmt -d .
6+
exit 1
7+
fi

.travis.gotest.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
WDIR=$PWD
4+
5+
for f in *; do
6+
if [ -d "$f" ]; then
7+
cd $WDIR/"$f"
8+
go test -v -race ./...
9+
cd $WDIR
10+
fi
11+
done

.travis.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
sudo: false
12
language: go
23
os:
34
- linux
45
- osx
56
go:
6-
- 1.14.x
7+
- go1.15.x
8+
env:
9+
global:
10+
- GO111MODULE=on
11+
- GOPROXY=https://goproxy.io
12+
before_install:
13+
- chmod +x .travis.gofmt.sh
14+
- chmod +x .travis.gotest.sh
15+
script:
16+
- ./.travis.gofmt.sh
17+
- ./.travis.gotest.sh

README.md

+130-110
Large diffs are not rendered by default.

apidoc/swagger/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Swagger 2.0
2+
3+
Visit https://github.com/iris-contrib/swagger instead.

apidoc/yaag/go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/iris-contrib/examples/apidoc/yaag
2+
3+
go 1.14
4+
5+
require (
6+
github.com/betacraft/yaag v1.0.1-0.20200719063524-47d781406108
7+
github.com/kataras/iris/v12 v12.1.9-0.20200812051831-0edf0affb0bd
8+
)

apidoc/yaag/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"github.com/kataras/iris/v12"
5+
6+
"github.com/betacraft/yaag/irisyaag"
7+
"github.com/betacraft/yaag/yaag"
8+
)
9+
10+
type myXML struct {
11+
Result string `xml:"result"`
12+
}
13+
14+
func main() {
15+
app := iris.New()
16+
17+
yaag.Init(&yaag.Config{ // <- IMPORTANT, init the middleware.
18+
On: true,
19+
DocTitle: "Iris",
20+
DocPath: "apidoc.html",
21+
BaseUrls: map[string]string{"Production": "", "Staging": ""},
22+
})
23+
app.Use(irisyaag.New()) // <- IMPORTANT, register the middleware.
24+
25+
app.Get("/json", func(ctx iris.Context) {
26+
ctx.JSON(iris.Map{"result": "Hello World!"})
27+
})
28+
29+
app.Get("/plain", func(ctx iris.Context) {
30+
ctx.Text("Hello World!")
31+
})
32+
33+
app.Get("/xml", func(ctx iris.Context) {
34+
ctx.XML(myXML{Result: "Hello World!"})
35+
})
36+
37+
app.Get("/complex", func(ctx iris.Context) {
38+
value := ctx.URLParam("key")
39+
ctx.JSON(iris.Map{"value": value})
40+
})
41+
42+
// Run our HTTP Server.
43+
//
44+
// Documentation of "yaag" doesn't note the follow, but in Iris we are careful on what
45+
// we provide to you.
46+
//
47+
// Each incoming request results on re-generation and update of the "apidoc.html" file.
48+
// Recommentation:
49+
// Write tests that calls those handlers, save the generated "apidoc.html".
50+
// Turn off the yaag middleware when in production.
51+
//
52+
// Example usage:
53+
// Visit all paths and open the generated "apidoc.html" file to see the API's automated docs.
54+
app.Listen(":8080")
55+
}
File renamed without changes.
File renamed without changes.

auth/cors/main.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Package main integrates the "rs/cors" net/http middleware into Iris.
2+
// That cors third-party middleware cannot be registered through `iris.FromStd`
3+
// as a common middleware because it should be injected before the Iris Router itself,
4+
// it allows/dissallows HTTP Methods too.
5+
//
6+
// This is just an example you can use to run something, based on custom logic,
7+
// before the Iris Router itself.
8+
//
9+
// In the "routing/custom-wrapper" example
10+
// we learn how we can acquire and release an Iris context to fire an Iris Handler
11+
// based on custom logic, before the Iris Router itself. In that example
12+
// we will fire a net/http handler (the "rs/cors" handler one) instead.
13+
//
14+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
15+
package main
16+
17+
import (
18+
"github.com/kataras/iris/v12"
19+
"github.com/rs/cors"
20+
)
21+
22+
func main() {
23+
app := iris.New()
24+
c := cors.New(cors.Options{
25+
AllowedOrigins: []string{"*"},
26+
AllowCredentials: true,
27+
// Enable Debugging for testing, consider disabling in production
28+
Debug: true,
29+
})
30+
// app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
31+
// [custom logic...]
32+
// if shouldFireNetHTTPHandler {
33+
// ...ServeHTTP(w,r)
34+
// return
35+
// }
36+
// router(w,r)
37+
// })
38+
// In our case, the cors package has a ServeHTTP
39+
// of the same form of app.WrapRouter's accept input argument,
40+
// so we can just do:
41+
app.WrapRouter(c.ServeHTTP)
42+
43+
// Serve ./public/index.html, main.js.
44+
app.HandleDir("/", iris.Dir("./public"))
45+
46+
// Register routes here...
47+
app.Get("/data", listData)
48+
49+
// http://localhost:8080 and click the "fetch data" button.
50+
app.Listen(":8080")
51+
}
52+
53+
type item struct {
54+
Title string `json:"title"`
55+
}
56+
57+
func listData(ctx iris.Context) {
58+
ctx.JSON([]item{
59+
{"Item 1"},
60+
{"Item 2"},
61+
{"Item 3"},
62+
})
63+
}

auth/cors/public/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Iris Cors Example</title>
8+
</head>
9+
10+
<body>
11+
<ul id="list">
12+
</ul>
13+
14+
<input type="button" value="Fetch Data" id="fetchBtn" />
15+
16+
<script src="main.js"></script>
17+
</body>
18+
</html>

auth/cors/public/main.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
2+
async function doRequest(method = 'GET', url = '', data = {}) {
3+
// Default options are marked with *
4+
5+
const request = {
6+
method: method, // *GET, POST, PUT, DELETE, etc.
7+
mode: 'cors', // no-cors, *cors, same-origin
8+
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
9+
credentials: 'same-origin', // include, *same-origin, omit
10+
redirect: 'follow', // manual, *follow, error
11+
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
12+
};
13+
14+
if (data !== undefined && method !== 'GET' && method !== 'HEAD') {
15+
request.headers = {
16+
'Content-Type': 'application/json'
17+
// 'Content-Type': 'application/x-www-form-urlencoded',
18+
};
19+
// body data type must match "Content-Type" header.
20+
request.body = JSON.stringify(data);
21+
}
22+
23+
const response = await fetch(url, request);
24+
return response.json(); // parses JSON response into native JavaScript objects.
25+
}
26+
27+
const ul = document.getElementById("list");
28+
29+
function fetchData() {
30+
console.log("sending request...")
31+
32+
doRequest('GET', '/data').then(data => {
33+
data.forEach(item => {
34+
var li = document.createElement("li");
35+
li.appendChild(document.createTextNode(item.title));
36+
ul.appendChild(li);
37+
});
38+
39+
console.log(data); // JSON data parsed by `response.json()` call.
40+
});
41+
}
42+
43+
document.getElementById("fetchBtn").onclick = fetchData;

authentication/oauth2/main.go renamed to auth/goth/main.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,14 @@ var sessionsManager *sessions.Sessions
7474
func init() {
7575
// attach a session manager
7676
cookieName := "mycustomsessionid"
77-
// AES only supports key sizes of 16, 24 or 32 bytes.
78-
// You either need to provide exactly that amount or you derive the key from what you type in.
79-
hashKey := []byte("the-big-and-secret-fash-key-here")
80-
blockKey := []byte("lot-secret-of-characters-big-too")
77+
hashKey := securecookie.GenerateRandomKey(64)
78+
blockKey := securecookie.GenerateRandomKey(32)
8179
secureCookie := securecookie.New(hashKey, blockKey)
8280

8381
sessionsManager = sessions.New(sessions.Config{
84-
Cookie: cookieName,
85-
Encode: secureCookie.Encode,
86-
Decode: secureCookie.Decode,
82+
Cookie: cookieName,
83+
Encoding: secureCookie,
84+
AllowReclaim: true,
8785
})
8886
}
8987

File renamed without changes.
File renamed without changes.

auth/jwt/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generate RSA
2+
3+
```sh
4+
$ openssl genrsa -des3 -out private_rsa.pem 2048
5+
```
6+
7+
```go
8+
b, err := ioutil.ReadFile("./private_rsa.pem")
9+
if err != nil {
10+
panic(err)
11+
}
12+
key := jwt.MustParseRSAPrivateKey(b, []byte("pass"))
13+
```
14+
15+
OR
16+
17+
```go
18+
import "crypto/rand"
19+
import "crypto/rsa"
20+
21+
key, err := rsa.GenerateKey(rand.Reader, 2048)
22+
```
23+
24+
# Generate Ed25519
25+
26+
```sh
27+
$ openssl genpkey -algorithm Ed25519 -out private_ed25519.pem
28+
$ openssl req -x509 -key private_ed25519.pem -out cert_ed25519.pem -days 365
29+
```

0 commit comments

Comments
 (0)