将Gin运行在其他端口上。

huangapple go评论77阅读模式
英文:

Make Gin run on some other port

问题

我在我的main.go文件中有以下内容:

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/someone/something/backend/config"
)

func main() {

	app := config.App()

	env := app.Env
	gin := gin.Default()
	
	gin.Run(":" + env.PORT)
}

其中PORT是8080

当我运行go run main.go时,它显示Listening and serving HTTP on :8080

但是当我运行gin run main.go时,它显示Listening on port 3000

我如何在这里更改端口?我需要在端口3000上运行其他东西。

更新:gin run main.go --port 8080仍然记录为Listening on port 3000

更新2:gin run --port 8080 main.go也显示监听端口3000

更新3:GIN_MODE=debug PORT=8080 gin run main.go也不起作用。

将Gin运行在其他端口上。

将Gin运行在其他端口上。

英文:

I have this in my main.go file

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/someone/something/backend/config"
)

func main() {

	app := config.App()

	env := app.Env
	gin := gin.Default()
	
	gin.Run(":" + env.PORT)
}

Where PORT is 8080.

When I run go run main.go it says Listening and serving HTTP on :8080

but when I run gin run main.go it says Listening on port 3000

How can I change port here? to something else. I need to run something else on port 3000

Update: gin run main.go --port 8080 still logs Listening on port 3000.

Update: 2 gin run --port 8080 main.go also says listening on port 3000

Update: 3 GIN_MODE=debug PORT=8080 gin run main.go also doesn't work.

将Gin运行在其他端口上。

将Gin运行在其他端口上。

答案1

得分: 2

gin命令与另一个名为github.com/codegangsta/gin的代码库相关联。

GIN_MODE环境变量对gin命令没有影响,因为它与该命令无关。同时,使用PORT环境变量来更改其默认端口值也不起作用,因为它依赖于-p--port标志或GIN_PORT环境变量来更改自身的默认端口值。

如果你想要更改gin命令的默认端口值,可以使用以下任一选项:

  1. 使用--port-p标志,并遵循正确的语法。
  2. 使用环境变量GIN_PORT,将所需的端口作为值进行设置(例如GIN_PORT=8080)。

当使用-p--port标志时,命令语法是严格的,必须将其放在run命令之前

示例:gin -p 8080 run main.gogin --port 8080 run main.go

*注意:*你之前尝试使用--port没有产生任何效果,因为它没有正确地放置在命令的语法中,即在ginrun之间。

要使用变量来更改端口:

首先,设置变量,例如export GIN_PORT=8080,然后运行命令gin run main.go(你可以使用export GIN_PORT=8080 && gin run main.go将其合并为一个命令)。

通过环境变量设置端口和通过标志设置端口的区别在于,如果你使用export GIN_PORT=value设置端口,程序将使用该端口替代程序的默认端口,除非使用-p--port标志显式地与命令一起使用,这将覆盖环境变量

如果使用标志设置端口,该端口仅在该单个实例中更改。

当然,你可以在任何用于设置变量的文件中设置GIN_PORT,这样你就不必每次登录时都设置它。

附加信息

gin命令的main.go文件中,位于第48-53行的cli.IntFlag结构初始化如下:

cli.IntFlag{
	Name:   "port,p",
	Value:  3000,
	EnvVar: "GIN_PORT",
	Usage:  "port for the proxy server",
},

该初始化定义了gin命令的默认端口值,以及可以更改该值的标志和环境变量。Name字段用于标志的名称,Value字段用于默认值,EnvVar字段用于环境变量。

英文:

The gin command is related to a different repository github.com/codegangsta/gin.

The GIN_MODE environment variable has no effect on the gin command because it is not related with it. Also changing default port value of it with PORT environment variable won't work, because it relies on -p,--port flags or GIN_PORT environment variable to change the default port value of itself.

If you want to change the default value of port for gin command you can use either of these options:

Use the --port or -p flag following the proper syntax
or;
Using the environment variable GIN_PORT with the desired port as the value (GIN_PORT=8080)

When using the -p or --port flag, the command syntax is strict and this must be placed before the command run

example: gin -p 8080 run main.go or gin --port 8080 run main.go

NOTE: Your previous attempts to use --port did not have any effect because it was not placed properly in the command's syntax, between gin and run

To change the port using variables:

First, set the variable, such as export GIN_PORT=8080 and then running the command gin run main.go (you can do this in one command using export GIN_PORT=8080 && gin run main.go)

The difference between setting the port via environment variable and setting port with flags is that if you set port with export GIN_PORT=value, the program will use this port in place of the program default unless the -p or --port flag is used explicitly with the command, which will override the environment variable.

If you set port with flags, the port will only be changed for that single instance.

Of course you can set GIN_PORT in any file you use to set variables with, so that you don't have to set this each time you log in.

Additional Information

The cli.IntFlag struct initialization located on lines 48-53 in the main.go of the gin command:

cli.IntFlag{
			Name:   "port,p",
			Value:  3000,
			EnvVar: "GIN_PORT",
			Usage:  "port for the proxy server",
		},

Defines default port value of the gin command as well as which flags and which environment variable can change it. Name field for the flags. Value field for the default value. EnvVar field for the environment variable.

答案2

得分: 0

你可以使用环境变量PORT:

 PORT=8080 && gin run main.go
英文:

You can use the environment variable PORT:

 PORT=8080 && gin run main.go

答案3

得分: 0

resolveAddress() 函数 检查环境变量:

func resolveAddress(addr []string) string {
	switch len(addr) {
	case 0:
		if port := os.Getenv("PORT"); port != "" {
			debugPrint("环境变量 PORT=\"%s\"", port)
			return ":" + port
		}
		debugPrint("环境变量 PORT 未定义。默认使用端口 :8080")
		return ":8080"
	case 1:
		return addr[0]
	default:
		panic("参数过多")
	}
}

这意味着,在类似 Linux 的 shell 中,你应该能够执行以下命令:

PORT=8080 gin run

如果你想显示更多日志,可以使用以下命令:

GIN_MODE=debug PORT=8080 gin run

关于你的程序(带有你自己的 main.go),请检查你的 go.mod 文件中项目的名称,并尝试以下命令:

cd /path/to/my/project
go install
which yourAPP # 应该是 /Users/RohitB/go/bin/yourApp
GIN_MODE=debug PORT=8080 yourAPP

如果你需要 codegangsta/gin,请使用 gin --version 确认你没有错误地安装了 github.com/gin-gonic/gin

英文:

The resolveAddress() function does check the environment variable:

func resolveAddress(addr []string) string {
	switch len(addr) {
	case 0:
		if port := os.Getenv("PORT"); port != "" {
			debugPrint("Environment variable PORT=\"%s\"", port)
			return ":" + port
		}
		debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
		return ":8080"
	case 1:
		return addr[0]
	default:
		panic("too many parameters")
	}
}

That means, in a Linux-like shell, you should be able to do:

PORT=8080 gin run

And you can display more logs with:

GIN_MODE=debug PORT=8080 gin run

Regarding your program (the one with your own main.go), check the name of your project in your go.mod, and try:

cd /path/to/my/project
go install
which yourAPP # should be /Users/RohitB/go/bin/yourApp
GIN_MODE=debug PORT=8080 yourAPP

If you need codegangsta/gin, check with gin --version that you did not install by mistake github.com/gin-gonic/gin.

huangapple
  • 本文由 发表于 2023年4月18日 22:28:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76045836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定