iris-配置
2020-02-10 本文已影响0人
EasyNetCN
iris支持四种配置方式:结构方式(struction),toml文件方式,yaml文件方式,编程方式(functional)。以下示例是iris源代码中提供的配置的例子,最后本人的示例实现了基于Spring Boot的配置文件,设置服务启动端口。
结构方式
package main
import (
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<b>Hello!</b>")
})
// [...]
// Good when you want to modify the whole configuration.
app.Run(iris.Addr(":8080"), iris.WithConfiguration(iris.Configuration{ // default configuration:
DisableStartupLog: false,
DisableInterruptHandler: false,
DisablePathCorrection: false,
EnablePathEscape: false,
FireMethodNotAllowed: false,
DisableBodyConsumptionOnUnmarshal: false,
DisableAutoFireStatusCode: false,
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
Charset: "UTF-8",
}))
// or before Run:
// app.Configure(iris.WithConfiguration(iris.Configuration{...}))
}
toml文件方式
配置文件iris.tml
DisablePathCorrection = false
EnablePathEscape = false
FireMethodNotAllowed = true
DisableBodyConsumptionOnUnmarshal = false
TimeFormat = "Mon, 01 Jan 2006 15:04:05 GMT"
Charset = "UTF-8"
[Other]
MyServerName = "iris"
package main
import (
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<b>Hello!</b>")
})
// [...]
// Good when you have two configurations, one for development and a different one for production use.
app.Run(iris.Addr(":8080"), iris.WithConfiguration(iris.TOML("./configs/iris.tml")))
// or before run:
// app.Configure(iris.WithConfiguration(iris.TOML("./configs/iris.tml")))
// app.Run(iris.Addr(":8080"))
}
yaml文件方式
yaml配置文件
DisablePathCorrection: false
EnablePathEscape: false
FireMethodNotAllowed: true
DisableBodyConsumptionOnUnmarshal: true
TimeFormat: Mon, 01 Jan 2006 15:04:05 GMT
Charset: UTF-8
package main
import (
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<b>Hello!</b>")
})
// [...]
// Good when you have two configurations, one for development and a different one for production use.
// If iris.YAML's input string argument is "~" then it loads the configuration from the home directory
// and can be shared between many iris instances.
app.Run(iris.Addr(":8080"), iris.WithConfiguration(iris.YAML("./configs/iris.yml")))
// or before run:
// app.Configure(iris.WithConfiguration(iris.YAML("./configs/iris.yml")))
// app.Run(iris.Addr(":8080"))
}
编程方式
package main
import (
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.HTML("<b>Hello!</b>")
})
// [...]
// Good when you want to change some of the configuration's field.
// Prefix: "With", code editors will help you navigate through all
// configuration options without even a glitch to the documentation.
app.Run(iris.Addr(":8080"), iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
// or before run:
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
// app.Run(iris.Addr(":8080"))
}
Spring Boot配置文件bootstrap.yml
spring:
application:
name: app-service
server:
port: 8002
package main
import (
"fmt"
"io/ioutil"
"strconv"
"time"
"github.com/kataras/iris/v12"
"gopkg.in/yaml.v3"
)
type SpringConfiguration struct {
Spring SpringProperties `yaml:"spring"`
Server ServerProperties `yaml:"server"`
}
type SpringProperties struct {
Application ApplicationProperties `yaml:"application"`
Cloud CloudProperties `yaml:"cloud"`
}
type ApplicationProperties struct {
Name string `yaml:"name"`
}
type CloudProperties struct {
Consul ConsuleProperties `yaml:"consul"`
}
type ServerProperties struct {
Port int32 `yaml:"port"`
}
type ConsuleProperties struct {
Host string `yaml:"host"`
Port int32 `yaml:"port"`
}
func main() {
app := iris.Default()
app.Get("/ping", func(ctx iris.Context) {
ctx.WriteString(time.Now().Format("2006-01-02 15:04:05"))
})
c := new(SpringConfiguration)
data, err := ioutil.ReadFile("bootstrap.yml")
err = yaml.Unmarshal(data, c)
if err != nil {
fmt.Errorf("parse yaml: %w", err)
}
app.Run(iris.Addr(":" + strconv.FormatInt(int64(c.Server.Port), 10)))
}