mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
Page:
Quick start
Pages
API versioning
Automatic public address
Benchmarks
Cache
Configuration
Content negotiation
Cookies
Documentation terms and conditions
Forms
Grpc
HTTP method override
HTTP referrer
Home
Host
Installation
Localization
MVC
Model validation
Our users
Project and community
Publications
Quick start
Request authentication
Response recorder
Routing context methods
Routing error handlers
Routing middleware
Routing override context
Routing path parameter types
Routing reverse lookups
Routing subdomains
Routing wrap the router
Routing
Sessions database
Sessions flash messages
Sessions
Sitemap
Starter kits
Support
Testing
URL query parameters
View
Websockets
dependency injection
2
Quick start
Gerasimos (Makis) Maropoulos edited this page 2020-06-19 08:47:37 +03:00
Table of Contents
Create an empty file, let's assume its name is example.go
, then open it and copy-paste the below code.
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.Default()
app.Use(myMiddleware)
app.Handle("GET", "/ping", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "pong"})
})
// Listens and serves incoming http requests
// on http://localhost:8080.
app.Listen(":8080")
}
func myMiddleware(ctx iris.Context) {
ctx.Application().Logger().Infof("Runs before %s", ctx.Path())
ctx.Next()
}
Start a terminal session and execute the following.
# run example.go and visit http://localhost:8080/ping on browser
$ go run example.go
Show me more!
Let's take a small overview of how easy is to get up and running.
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// Load all templates from the "./views" folder
// where extension is ".html" and parse them
// using the standard `html/template` package.
app.RegisterView(iris.HTML("./views", ".html"))
// Method: GET
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// Bind: {{.message}} with "Hello world!"
ctx.ViewData("message", "Hello world!")
// Render template file: ./views/hello.html
ctx.View("hello.html")
})
// Method: GET
// Resource: http://localhost:8080/user/42
//
// Need to use a custom regexp instead?
// Easy;
// Just mark the parameter's type to 'string'
// which accepts anything and make use of
// its `regexp` macro function, i.e:
// app.Get("/user/{id:string regexp(^[0-9]+$)}")
app.Get("/user/{id:uint64}", func(ctx iris.Context) {
userID, _ := ctx.Params().GetUint64("id")
ctx.Writef("User ID: %d", userID)
})
// Start the server using a network address.
app.Listen(":8080")
}
<!-- file: ./views/hello.html -->
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>{{.message}}</h1>
</body>
</html>
Wanna re-start your app automatically when source code changes happens? Install the iris-cli tool and execute
iris-cli run
instead ofgo run main.go
.
At the next section we will learn more about Routing.
- What is Iris
- 📌Getting Started
- Host
- Configuration
- Routing
- HTTP Method Override
- API Versioning
- Content Negotiation
- Response Recorder
- HTTP Referrer
- Request Authentication
- URL Query Parameters
- Forms
- Model Validation
- Cache
- View
- Cookies
- Sessions
- Websockets
- Dependency Injection
- MVC
- gRPC
- Sitemap
- Localization
- Testing
- 🤓Resources
Home | Project | Quick Start | Technical Docs | Copyright © 2019-2020 Gerasimos Maropoulos. Documentation terms of use.