mirror of
https://github.com/kataras/iris.git
synced 2025-12-30 16:27:04 +00:00
Add (10) more _examples. TOC: https://github.com/kataras/iris/tree/v6/_examples
Part 3. Former-commit-id: 229b86baca4043c69517968318d9a962d2e026d0
This commit is contained in:
35
_examples/beginner/listen-letsencrypt/main.go
Normal file
35
_examples/beginner/listen-letsencrypt/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Package main provide one-line integration with letsencrypt.org
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
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())
|
||||
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
ctx.Writef("Hello from SECURE SERVER!")
|
||||
})
|
||||
|
||||
app.Get("/test2", func(ctx *iris.Context) {
|
||||
ctx.Writef("Welcome to secure server from /test2!")
|
||||
})
|
||||
|
||||
app.Get("/redirect", func(ctx *iris.Context) {
|
||||
ctx.Redirect("/test2")
|
||||
})
|
||||
|
||||
// This will provide you automatic certification & key from letsencrypt.org's servers
|
||||
// it also starts a second 'http://' server which will redirect all 'http://$PATH' requests to 'https://$PATH'
|
||||
|
||||
// NOTE: may not work on local addresses like this,
|
||||
// use it on a real domain, because
|
||||
// it uses the "golang.org/x/crypto/acme/autocert" package.
|
||||
app.ListenLETSENCRYPT("localhost:443")
|
||||
}
|
||||
37
_examples/beginner/listen-tls/main.go
Normal file
37
_examples/beginner/listen-tls/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
const host = "127.0.0.1:443"
|
||||
|
||||
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())
|
||||
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
ctx.Writef("Hello from the SECURE server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx *iris.Context) {
|
||||
ctx.Writef("Hello from the SECURE server on path /mypath")
|
||||
})
|
||||
|
||||
// start a secondary server (HTTP) on port 80, this is a non-blocking func
|
||||
// redirects all http to the main server which is tls/ssl on port :443
|
||||
|
||||
iris.Proxy(":80", "https://"+host)
|
||||
// start the MAIN server (HTTPS) on port 443, this is a blocking func
|
||||
app.ListenTLS(host, "mycert.cert", "mykey.key")
|
||||
|
||||
// now if you navigate to http://127.0.0.1/mypath it will
|
||||
// send you back to https://127.0.0.1:443/mypath (https://127.0.0.1/mypath)
|
||||
//
|
||||
// go to the listen-letsencrypt example to view how you can integrate your server
|
||||
// to get automatic certification and key from the letsencrypt.org 's servers.
|
||||
}
|
||||
22
_examples/beginner/listen-unix/main.go
Normal file
22
_examples/beginner/listen-unix/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
const host = "127.0.0.1:443"
|
||||
|
||||
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())
|
||||
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.ListenUNIX("/tmp/srv.sock", 0666)
|
||||
}
|
||||
56
_examples/beginner/stream-writer/main.go
Normal file
56
_examples/beginner/stream-writer/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt" // just an optional helper
|
||||
"io"
|
||||
"time" // showcase the delay
|
||||
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
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())
|
||||
|
||||
timeWaitForCloseStream := 4 * time.Second
|
||||
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
i := 0
|
||||
// goroutine in order to no block and just wait,
|
||||
// goroutine is OPTIONAL and not a very good option but it depends on the needs
|
||||
// Look the streaming_simple_2 for an alternative code style
|
||||
// Send the response in chunks and wait for a second between each chunk.
|
||||
go ctx.StreamWriter(func(w io.Writer) bool {
|
||||
i++
|
||||
fmt.Fprintf(w, "this is a message number %d\n", i) // write
|
||||
time.Sleep(time.Second) // imaginary delay
|
||||
if i == 4 {
|
||||
return false // close and flush
|
||||
}
|
||||
return true // continue write
|
||||
})
|
||||
|
||||
// when this handler finished the client should be see the stream writer's contents
|
||||
// simulate a job here...
|
||||
time.Sleep(timeWaitForCloseStream)
|
||||
})
|
||||
|
||||
app.Get("/alternative", func(ctx *iris.Context) {
|
||||
// Send the response in chunks and wait for a second between each chunk.
|
||||
ctx.StreamWriter(func(w io.Writer) bool {
|
||||
for i := 1; i <= 4; i++ {
|
||||
fmt.Fprintf(w, "this is a message number %d\n", i) // write
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
// when this handler finished the client should be see the stream writer's contents
|
||||
return false // stop and flush the contents
|
||||
})
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Reference in New Issue
Block a user