-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(main, logging, utils): Refactor logging and IP extraction logic
Move setupLogging to logging package and create GetRealIP utility function
- Loading branch information
1 parent
39155ac
commit 96f41e3
Showing
4 changed files
with
51 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package logging | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
func SetupLogging() { | ||
logFile, err := os.OpenFile("data/server.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
multiWriter := io.MultiWriter(os.Stdout, logFile) | ||
log.SetOutput(multiWriter) | ||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package utils | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func GetRealIP(r *http.Request) string { | ||
ip := r.Header.Get("X-Real-IP") | ||
if ip != "" { | ||
return ip | ||
} | ||
|
||
ip = r.Header.Get("X-Forwarded-For") | ||
if ip != "" { | ||
ips := strings.Split(ip, ",") | ||
if len(ips) > 0 { | ||
return strings.TrimSpace(ips[0]) | ||
} | ||
} | ||
|
||
ip, _, err := net.SplitHostPort(r.RemoteAddr) | ||
if err != nil { | ||
return r.RemoteAddr | ||
} | ||
return ip | ||
} |