gin icon indicating copy to clipboard operation
gin copied to clipboard

add `isValidPORTEnvVar` function to judge the PORT whether is valid

Open bestgopher opened this issue 4 years ago • 1 comments

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

bestgopher avatar Apr 18 '20 08:04 bestgopher

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%) :arrow_up:

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.

codecov[bot] avatar Apr 18 '20 08:04 codecov[bot]