-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (88 loc) · 2.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"events/services/events"
"events/services/users"
"github.com/iris-contrib/middleware/pg"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/logger"
)
// Read the example and its comments carefully.
func makeAccessLog() *accesslog.AccessLog {
// Initialize a new access log middleware.
ac := accesslog.File("access.log")
// 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 = true
ac.IP = true
ac.BytesReceivedBody = true
ac.BytesSentBody = true
ac.BytesReceived = true
ac.BytesSent = true
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,
})
return ac
}
func main() {
ac := makeAccessLog()
defer ac.Close() // Close the underline file.
// Customize the logger configuration
customLogger := logger.New(logger.Config{
Status: true,
IP: true,
Method: true,
Path: true,
Query: true,
TraceRoute: false,
PathAfterHandler: false,
MessageContextKeys: []string{"logger_message"}, // Customize the log message key
MessageHeaderKeys: []string{"X-Request-ID"}, // Include additional header keys in the log
})
app := iris.New()
// Register the middleware (UseRouter to catch http errors too).
app.UseRouter(ac.Handler)
// Add the custom logger middleware
app.Use(customLogger)
//app.Use(logger.New())
app.Logger().SetLevel("debug")
app.Use(iris.Compression)
db := newPostgresMiddleware()
events.InitRoutes(app, db)
users.InitRoutes(app, db)
app.Listen(":8080")
}
func newPostgresMiddleware() iris.Handler {
schema := pg.NewSchema()
schema.MustRegister("users", users.User{})
schema.MustRegister("events", events.Event{})
opts := pg.Options{
Host: "localhost",
Port: 5432,
User: "godevops",
Password: "godevops123",
DBName: "godevops",
Schema: "public",
SSLMode: "disable",
Transactional: true, // or false to disable the transactional feature.
Trace: false, // or false to production to disable query logging.
CreateSchema: true, // true to create the schema if it doesn't exist.
CheckSchema: false, // true to check the schema for missing tables and columns.
ErrorHandler: func(ctx iris.Context, err error) {},
}
p := pg.New(schema, opts)
return p.Handler()
}