mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 13:05:56 +00:00
add viper configuration example and minor improvements - read HISTORY.md
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
* Configuration
|
||||
* [Functional](configuration/functional/main.go)
|
||||
* [Configuration Struct](configuration/from-configuration-structure/main.go)
|
||||
* [Using Viper](configuration/viper)
|
||||
* [Import from YAML](configuration/from-yaml-file/main.go)
|
||||
* [Share Configuration across instances](configuration/from-yaml-file/shared-configuration/main.go)
|
||||
* [Import from TOML](configuration/from-toml-file/main.go)
|
||||
|
||||
@@ -4,5 +4,5 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/betacraft/yaag v1.0.1-0.20200719063524-47d781406108
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
github.com/kataras/iris/v12 master
|
||||
)
|
||||
|
||||
50
_examples/configuration/viper/config.yml
Normal file
50
_examples/configuration/viper/config.yml
Normal file
@@ -0,0 +1,50 @@
|
||||
Addr:
|
||||
Internal:
|
||||
IP: 127.0.0.1
|
||||
Plain: 8080
|
||||
Secure: 443
|
||||
Locale:
|
||||
Pattern: "./locales/*/*.ini"
|
||||
Default: "en-US"
|
||||
Supported:
|
||||
- "en-US"
|
||||
- "el-GR"
|
||||
Iris:
|
||||
LogLevel: debug
|
||||
SocketSharding: true
|
||||
EnableOptimizations: true
|
||||
DisableStartupLog: false
|
||||
FireMethodNotAllowed: true
|
||||
ForceLowercaseRouting: true
|
||||
EnablePathIntelligence: true
|
||||
Charset: "utf-8"
|
||||
TimeFormat: "2006-01-02 15:04:05"
|
||||
DisableBodyConsumptionOnUnmarshal: true
|
||||
FireEmptyFormError: true
|
||||
PostMaxMemory: 67108864
|
||||
RemoteAddrHeaders:
|
||||
- "X-Real-Ip"
|
||||
- "X-Forwarded-For"
|
||||
- "CF-Connecting-IP"
|
||||
- "True-Client-Ip"
|
||||
IgnoreServerErrors:
|
||||
- "http: Server closed"
|
||||
# Tunneling:
|
||||
# WebInterface: "http://127.0.0.1:4040"
|
||||
# AuthToken: "<secret>"
|
||||
# Tunnels:
|
||||
# - Name: "My awesome App"
|
||||
# Addr: "localhost:8080"
|
||||
# - Name: "My Second awesome App"
|
||||
# Addr: "localhost:9090"
|
||||
RemoteAddrPrivateSubnets:
|
||||
- Start: "192.168.0.0"
|
||||
End: "192.168.255.255"
|
||||
- Start: "198.18.0.0"
|
||||
End: "198.19.255.255"
|
||||
SSLProxyHeaders:
|
||||
X-Forwarded-Proto: "https"
|
||||
HostProxyHeaders:
|
||||
X-Host: true
|
||||
Other:
|
||||
ServerName: "My awesome Iris web server"
|
||||
54
_examples/configuration/viper/config/config.go
Normal file
54
_examples/configuration/viper/config/config.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
// Viper is a third-party package:
|
||||
// go get github.com/spf13/viper
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func init() {
|
||||
loadConfiguration()
|
||||
}
|
||||
|
||||
// C is the configuration of the app.
|
||||
var C = struct {
|
||||
Iris iris.Configuration
|
||||
Addr struct {
|
||||
Internal struct {
|
||||
IP string
|
||||
Plain int
|
||||
Secure int
|
||||
}
|
||||
}
|
||||
Locale struct {
|
||||
Pattern string
|
||||
Default string
|
||||
Supported []string
|
||||
}
|
||||
}{
|
||||
Iris: iris.DefaultConfiguration(),
|
||||
// other default values...
|
||||
}
|
||||
|
||||
func loadConfiguration() {
|
||||
viper.SetConfigName("config") // name of config file (without extension)
|
||||
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
|
||||
viper.AddConfigPath("/etc/app/") // path to look for the config file in
|
||||
viper.AddConfigPath("$HOME/.app") // call multiple times to add many search paths
|
||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||
err := viper.ReadInConfig() // Find and read the config file
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
} else {
|
||||
panic(fmt.Errorf("load configuration: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
err = viper.Unmarshal(&C)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("load configuration: unmarshal: %w", err))
|
||||
}
|
||||
}
|
||||
8
_examples/configuration/viper/go.mod
Normal file
8
_examples/configuration/viper/go.mod
Normal file
@@ -0,0 +1,8 @@
|
||||
module app
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/kataras/iris/v12 master
|
||||
github.com/spf13/viper v1.3.2
|
||||
)
|
||||
19
_examples/configuration/viper/main.go
Normal file
19
_examples/configuration/viper/main.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"app/config"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.TextYAML(config.C)
|
||||
})
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", config.C.Addr.Internal.IP, config.C.Addr.Internal.Plain)
|
||||
app.Listen(addr, iris.WithConfiguration(config.C.Iris))
|
||||
}
|
||||
@@ -4,6 +4,6 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
github.com/kataras/iris/v12 master
|
||||
go.mongodb.org/mongo-driver v1.3.4
|
||||
)
|
||||
|
||||
@@ -4,6 +4,6 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
github.com/kataras/iris/v12 master
|
||||
github.com/mailgun/groupcache/v2 v2.1.0
|
||||
)
|
||||
|
||||
@@ -4,5 +4,5 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/Shopify/sarama v1.26.4
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
github.com/kataras/iris/v12 master
|
||||
)
|
||||
|
||||
@@ -3,6 +3,6 @@ module github.com/kataras/iris/examples/logging/rollbar
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
github.com/kataras/iris/v12 master
|
||||
github.com/rollbar/rollbar-go v1.2.0
|
||||
)
|
||||
|
||||
@@ -2,4 +2,4 @@ module app
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
require github.com/kataras/iris/v12 master
|
||||
|
||||
@@ -2,4 +2,4 @@ module github.com/kataras/iris/_examples/mvc/vuejs-todo-mvc/src
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
require github.com/kataras/iris/v12 master
|
||||
|
||||
@@ -2,6 +2,4 @@ module github.com/kataras/iris/_examples/routing/subdomains/redirect/multi-insta
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
|
||||
replace github.com/kataras/iris/v12 => ../../../../../
|
||||
require github.com/kataras/iris/v12 master
|
||||
@@ -2,5 +2,5 @@ module app
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
require github.com/kataras/iris/v12 master
|
||||
|
||||
|
||||
@@ -4,5 +4,5 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/iris-contrib/middleware/jwt v0.0.0-20200710202437-92b01b85baaf
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha // indirect
|
||||
github.com/kataras/iris/v12 master
|
||||
)
|
||||
|
||||
@@ -4,5 +4,5 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/googollee/go-socket.io v1.4.3-0.20191109153049-7451e2f8c2e0
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha
|
||||
github.com/kataras/iris/v12 master
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user