Add RemoteIP #10
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "hanz/web:main"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Enable server to get remote IP
For anyone who would like to use this feature, you can add this line on your
go.mod
:Updated with the latest commit
Hi hanz,
just wanted to let you know that I'm very busy at the moment so I can't do a full review yet.
One thing that immediately caught my attention is that the remote IP creates additional overhead on every request even when it's not requested. This should be improved for performance reasons.
Hi @ed thank you for responding
I have pushed a new commit that moved
remote_ip
fromhandleConnection
toContext.RemoteIP()
which will only fetch the data when needed.Added a few points to improve (not including test code for now).
@ -13,6 +14,7 @@ type Context interface {
Redirect(int, string) error
Request() Request
Response() Response
RemoteIP() string
Alphabetical order should be maintained.
@ -21,6 +23,7 @@ type Context interface {
type context struct {
request
response
conn net.Conn
Variable names should have a length that reflects their lifetime.
This is a struct member (long lifetime), so the name should not be shortened (use
connection
).@ -76,6 +79,15 @@ func (ctx *context) Response() Response {
return &ctx.response
}
func (ctx *context) RemoteIP() string {
Even if it feels self-explanatory, a comment here would help follow the style of the other functions.
@ -77,2 +80,4 @@
}
func (ctx *context) RemoteIP() string {
remote_ip, _, err := net.SplitHostPort(ctx.conn.RemoteAddr().String())
This doesn't follow Go's naming standards. Just
ip
is fine.@ -78,1 +81,4 @@
func (ctx *context) RemoteIP() string {
remote_ip, _, err := net.SplitHostPort(ctx.conn.RemoteAddr().String())
if err != nil {
Project specific style: Newlines before and after a block (unless it's at function start/end).
@ -5,0 +6,4 @@
"net"
"net/http"
"os"
os_signal "os/signal"
Renaming a package should be avoided unless absolutely necessary.
@ -11,6 +12,7 @@ type Context interface {
Handler() Handler
Next(Context) error
Redirect(int, string) error
RemoteIP() string
The function name is alphabetically sorted in the interface but the actual function should also appear in a sorted spot.
@ -76,6 +79,17 @@ func (ctx *context) Response() Response {
return &ctx.response
}
// RemoteIP extracts the client's IP address from the connection's remote address.
This comment can be misinterpreted. I suggest the following:
@ -78,1 +81,4 @@
// RemoteIP extracts the client's IP address from the connection's remote address.
func (ctx *context) RemoteIP() string {
ip, _, err := net.SplitHostPort(ctx.connection.RemoteAddr().String())
Is it possible for
net.SplitHostPort
to cause an error on its ownRemoteAddr
?If the answer is yes, it's probably better to return an error alongside the IP.
if the answer is no,
err
variable should be removed.@ -79,0 +84,4 @@
ip, _, err := net.SplitHostPort(ctx.connection.RemoteAddr().String())
if err != nil {
ip = ""
IP is always empty in the error case, so this entire branch effectively does nothing.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.