Skip to content

Conversation

@bestgopher
Copy link
Contributor

If an addr parameter is not passed in Run, it will get PORT env variable. If there is not a PORT variable,it will use default port - 8080. But sometimes, there is a PORT env variable, but it is not a valid value(0-65535),the app will panic. So I add a function to judge the PORT whether is valid. For example:

my code:

func main() {
    r := gin.Default()
    ...
    if  err := r.Run(); err != nil {
       ...
   }
}

run:

➜  gin $ echo $PORT
abc
➜  gin $ go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /get                      --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT="abcd"
[GIN-debug] Listening and serving HTTP on :abcd
[GIN-debug] [ERROR] listen tcp: lookup tcp/abcd: nodename nor servname provided, or not known
2020/04/16 23:33:36 listen tcp: lookup tcp/abcd: nodename nor servname provided, or not known
exit status 1

@codecov
Copy link

codecov bot commented Apr 18, 2020

Codecov Report

Merging #2327 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2327   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files          41       41           
  Lines        2304     2308    +4     
=======================================
+ Hits         2267     2271    +4     
  Misses         21       21           
  Partials       16       16           
Impacted Files Coverage Δ
utils.go 97.53% <100.00%> (+0.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 90fff29...7f27964. Read the comment docs.


// Determine the PORT environment variable whether is valid。
// If the PORT can be parsed to uint(0-65535),return true。
func isValidPORTEnvVar(portString string) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use isValidPortEnvVar?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bestgopher @thinkerou The function is no problem. The function naming can indeed be changed, such as this isValidPortEnvVar Would be better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use uint or uint16, not uint32?

@thinkerou thinkerou added this to the v1.8 milestone Jun 26, 2021
@thinkerou thinkerou modified the milestones: v1.8, v1.9 May 28, 2022
@thinkerou thinkerou modified the milestones: v1.9, v1.10 Jan 17, 2023
@appleboy appleboy modified the milestones: v1.10, v1.11 Mar 21, 2024
@appleboy appleboy modified the milestones: v1.11, v1.12 Jun 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants