mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
32
_examples/http-server/custom-httpserver/easy-way/main.go
Normal file
32
_examples/http-server/custom-httpserver/easy-way/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
// Any custom fields here. Handler and ErrorLog are set to the server automatically
|
||||
srv := &http.Server{Addr: ":8080"}
|
||||
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/mypath
|
||||
app.Run(iris.Server(srv)) // same as app.Listen(":8080")
|
||||
|
||||
// More:
|
||||
// see "multi" if you need to use more than one server at the same app.
|
||||
//
|
||||
// for a custom listener use: iris.Listener(net.Listener) or
|
||||
// iris.TLS(cert,key) or iris.AutoTLS(), see "custom-listener" example for those.
|
||||
}
|
||||
47
_examples/http-server/custom-httpserver/multi/main.go
Normal file
47
_examples/http-server/custom-httpserver/multi/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
// Note: It's not needed if the first action is "go app.Run".
|
||||
if err := app.Build(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// start a secondary server listening on localhost:9090.
|
||||
// use "go" keyword for Listen functions if you need to use more than one server at the same app.
|
||||
//
|
||||
// http://localhost:9090/
|
||||
// http://localhost:9090/mypath
|
||||
srv1 := &http.Server{Addr: ":9090", Handler: app}
|
||||
go srv1.ListenAndServe()
|
||||
println("Start a server listening on http://localhost:9090")
|
||||
|
||||
// start a "second-secondary" server listening on localhost:5050.
|
||||
//
|
||||
// http://localhost:5050/
|
||||
// http://localhost:5050/mypath
|
||||
srv2 := &http.Server{Addr: ":5050", Handler: app}
|
||||
go srv2.ListenAndServe()
|
||||
println("Start a server listening on http://localhost:5050")
|
||||
|
||||
// Note: app.Run is totally optional, we have already built the app with app.Build,
|
||||
// you can just make a new http.Server instead.
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/mypath
|
||||
app.Listen(":8080") // Block here.
|
||||
}
|
||||
40
_examples/http-server/custom-httpserver/std-way/main.go
Normal file
40
_examples/http-server/custom-httpserver/std-way/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
// call .Build before use the 'app' as a http.Handler on a custom http.Server
|
||||
app.Build()
|
||||
|
||||
// create our custom server and assign the Handler/Router
|
||||
srv := &http.Server{Handler: app, Addr: ":8080"} // you have to set Handler:app and Addr, see "iris-way" which does this automatically.
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/mypath
|
||||
println("Start a server listening on http://localhost:8080")
|
||||
srv.ListenAndServe() // same as app.Listen(":8080")
|
||||
|
||||
// Notes:
|
||||
// Banner is not shown at all. Same for the Interrupt Handler, even if app's configuration allows them.
|
||||
//
|
||||
// `.Run` is the only one function that cares about those three.
|
||||
|
||||
// More:
|
||||
// see "multi" if you need to use more than one server at the same app.
|
||||
//
|
||||
// for a custom listener use: iris.Listener(net.Listener) or
|
||||
// iris.TLS(cert,key) or iris.AutoTLS(), see "custom-listener" example for those.
|
||||
}
|
||||
Reference in New Issue
Block a user