103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"iris-test/app/views"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/middleware/accesslog"
|
|
"github.com/kataras/iris/v12/sessions"
|
|
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
|
|
)
|
|
|
|
var redisDB *redis.Database
|
|
|
|
func createSessionEngine() *sessions.Sessions {
|
|
redisAddr := fmt.Sprintf("%s:%d", Config.Redis.Host, Config.Redis.Port)
|
|
|
|
redisDB = redis.New(redis.Config{
|
|
Network: "tcp",
|
|
Addr: redisAddr,
|
|
Timeout: time.Duration(30) * time.Second,
|
|
MaxActive: 10,
|
|
Username: Config.Redis.Username,
|
|
Password: Config.Redis.Password,
|
|
Database: Config.Redis.Database,
|
|
Prefix: Config.Redis.Prefix,
|
|
Driver: redis.GoRedis(), // defaults to this driver.
|
|
// To set a custom, existing go-redis client, use the "SetClient" method:
|
|
// Driver: redis.GoRedis().SetClient(customGoRedisClient)
|
|
})
|
|
|
|
sessions_engine := sessions.New(sessions.Config{
|
|
Cookie: "_session_id",
|
|
Expires: 0, // defaults to 0: unlimited life. Another good value is: 45 * time.Minute,
|
|
AllowReclaim: true,
|
|
CookieSecureTLS: true,
|
|
})
|
|
|
|
sessions_engine.UseDatabase(redisDB)
|
|
|
|
return sessions_engine
|
|
}
|
|
|
|
func createAccessLog() *accesslog.AccessLog {
|
|
// Initialize a new access log middleware.
|
|
ac := accesslog.New(os.Stdout)
|
|
// Remove this line to disable logging to console:
|
|
// ac.AddOutput(os.Stdout)
|
|
|
|
// The default configuration:
|
|
ac.Delim = '|'
|
|
ac.TimeFormat = "2006-01-02 15:04:05"
|
|
ac.Async = false
|
|
ac.IP = true
|
|
ac.BytesReceivedBody = true
|
|
ac.BytesSentBody = true
|
|
ac.BytesReceived = false
|
|
ac.BytesSent = false
|
|
ac.BodyMinify = true
|
|
ac.RequestBody = true
|
|
ac.ResponseBody = false
|
|
ac.KeepMultiLineError = true
|
|
ac.PanicLog = accesslog.LogHandler
|
|
|
|
// Default line format if formatter is missing:
|
|
// Time|Latency|Code|Method|Path|IP|Path Params Query Fields|Bytes Received|Bytes Sent|Request|Response|
|
|
//
|
|
// Set Custom Formatter:
|
|
ac.SetFormatter(&accesslog.JSON{
|
|
Indent: " ",
|
|
HumanTime: true,
|
|
})
|
|
// ac.SetFormatter(&accesslog.CSV{})
|
|
// ac.SetFormatter(&accesslog.Template{Text: "{{.Code}}"})
|
|
|
|
return ac
|
|
}
|
|
|
|
func createApp() *iris.Application {
|
|
sessionsEngine := createSessionEngine()
|
|
accessLog := createAccessLog()
|
|
|
|
app := iris.New()
|
|
app.Logger().SetLevel(Config.Application.LogLevel)
|
|
app.Use(sessionsEngine.Handler())
|
|
app.UseRouter(accessLog.Handler)
|
|
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
|
|
views.CreateRouter(app)
|
|
return app
|
|
}
|
|
|
|
func main() {
|
|
InitCfg()
|
|
InitLogging()
|
|
InitDB()
|
|
|
|
app := createApp()
|
|
defer redisDB.Close()
|
|
app.Listen(":8000")
|
|
}
|