Skip to content

Commit 2ae1e3a

Browse files
committed
changes on two things..
- rewriting debug and gin-mode - generation of user-agent to static string
1 parent 08d4473 commit 2ae1e3a

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- Update README.md ([#3](https://github.com/TibiaData/tibiadata-api-go/pull/3), [#5](https://github.com/TibiaData/tibiadata-api-go/pull/5) by [tobiasehlert](https://github.com/tobiasehlert))
99
- Implementation of response handler ([#4](https://github.com/TibiaData/tibiadata-api-go/pull/4) by [tobiasehlert](https://github.com/tobiasehlert))
1010
- Changing building and releasing ([#6](https://github.com/TibiaData/tibiadata-api-go/pull/6), [#7](https://github.com/TibiaData/tibiadata-api-go/pull/7) by [tobiasehlert](https://github.com/tobiasehlert))
11-
- Shrink of TibiaFansitesV3 ([#8](https://github.com/TibiaData/tibiadata-api-go/pull/8) by [tobiasehlert](https://github.com/tobiasehlert)) in
11+
- Shrink of TibiaFansitesV3 ([#8](https://github.com/TibiaData/tibiadata-api-go/pull/8) by [tobiasehlert](https://github.com/tobiasehlert))
1212
- Updating build workflow with enhancements ([#11](https://github.com/TibiaData/tibiadata-api-go/pull/11) by [tobiasehlert](https://github.com/tobiasehlert))
1313

1414

src/webserver.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var TibiadataDefaultVoc string = "all"
2828
var TibiadataAPIversion int = 3
2929
var TibiadataDebug bool
3030

31+
// Tibiadata app user-agent
32+
var TibiadataUserAgent string
33+
3134
// Tibiadata app details set to release/build on GitHub
3235
var TibiadataBuildRelease = "unknown" // will be set by GitHub Actions (to release number)
3336
var TibiadataBuildBuilder = "manual" // will be set by GitHub Actions
@@ -53,21 +56,28 @@ func main() {
5356
log.Printf("[info] TibiaData API commit: %s", TibiadataBuildCommit)
5457
log.Printf("[info] TibiaData API edition: %s", TibiadataBuildEdition)
5558

56-
// setting application to ReleaseMode if DEBUG_MODE is false
57-
if !getEnvAsBool("DEBUG_MODE", false) {
58-
// setting GIN_MODE to ReleaseMode
59-
gin.SetMode(gin.ReleaseMode)
60-
log.Printf("[info] TibiaData API app-mode: release")
61-
} else {
62-
// setting GIN_MODE to DebugMode
59+
// setting gin-application to certain mode if GIN_MODE is set to release, test or debug (default is release)
60+
switch ginMode := getEnv("GIN_MODE", "release"); ginMode {
61+
case "test":
62+
gin.SetMode(gin.TestMode)
63+
case "debug":
6364
gin.SetMode(gin.DebugMode)
64-
log.Printf("[info] TibiaData API app-mode: debug")
65+
default:
66+
gin.SetMode(gin.ReleaseMode)
67+
}
68+
// logging the gin.mode
69+
log.Printf("[info] TibiaData API gin-mode: %s", gin.Mode())
6570

71+
// setting tibiadata-application to log much less if DEBUG_MODE is false (default is false)
72+
if !getEnvAsBool("DEBUG_MODE", false) {
73+
log.Printf("[info] TibiaData API debug-mode: disabled")
74+
} else {
6675
// setting debug to true for more logging
6776
TibiadataDebug = true
77+
log.Printf("[info] TibiaData API debug-mode: enabled")
6878

6979
// logging user-agent string
70-
log.Printf("[debug] TIbiaData API User-Agent: %s", TibiadataUserAgentGenerator(TibiadataAPIversion))
80+
log.Printf("[debug] TIbiaData API User-Agent: %s", TibiadataUserAgent)
7181
}
7282

7383
router := gin.Default()
@@ -143,20 +153,29 @@ func TibiaDataInitializer() {
143153
if isEnvExist("TIBIADATA_EDITION") {
144154
TibiadataBuildEdition = getEnv("TIBIADATA_EDITION", "open-source")
145155
}
156+
157+
// generating TibiadataUserAgent with TibiadataUserAgentGenerator function
158+
TibiadataUserAgent = TibiadataUserAgentGenerator(TibiadataAPIversion)
146159
}
147160

148161
/*
149162
// TibiaDataAPIHandleErrorResponse func - handling of responses..
150163
func TibiaDataAPIHandleErrorResponse(c *gin.Context, s1 string, s2 string, s3 string) {
164+
if TibiadataDebug {
165+
log.Println("[error] " + s1 + " - (" + c.Request.RequestURI + "). " + s2 + "; " + s3)
166+
}
167+
151168
// return error response
152-
log.Println("[error] " + s1 + " - (" + c.Request.RequestURI + "). " + s2 + "; " + s3)
153169
c.JSON(http.StatusOK, gin.H{"error": s2})
154170
}
155171
156172
// TibiaDataAPIHandleOtherResponse func - handling of responses..
157173
func TibiaDataAPIHandleOtherResponse(c *gin.Context, httpCode int, s string, j interface{}) {
174+
if TibiadataDebug {
175+
log.Println("[info] " + s + " - (" + c.Request.RequestURI + ") executed successfully.")
176+
}
177+
158178
// return successful response (with specific status code)
159-
log.Println("[info] " + s + " - (" + c.Request.RequestURI + ") executed successfully.")
160179
c.JSON(httpCode, j)
161180
}
162181
*/
@@ -170,8 +189,11 @@ func TibiaDataAPIHandleSuccessResponse(c *gin.Context, s string, j interface{})
170189
log.Printf("[debug] %s\n", js)
171190
}
172191

192+
if TibiadataDebug {
193+
log.Println("[info] " + s + " - (" + c.Request.RequestURI + ") executed successfully.")
194+
}
195+
173196
// return successful response
174-
log.Println("[info] " + s + " - (" + c.Request.RequestURI + ") executed successfully.")
175197
c.JSON(http.StatusOK, j)
176198
}
177199

@@ -218,7 +240,7 @@ func TibiadataHTMLDataCollectorV3(TibiaURL string) string {
218240
// Set headers for all requests
219241
client.SetHeaders(map[string]string{
220242
"Content-Type": "application/json",
221-
"User-Agent": TibiadataUserAgentGenerator(TibiadataAPIversion),
243+
"User-Agent": TibiadataUserAgent,
222244
})
223245

224246
// Enabling Content length value for all request

0 commit comments

Comments
 (0)