How can I release my api for anyone to consume?

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

How can I release my api for anyone to consume?

问题

我有一个使用Go、Gin和libpq构建的服务器,我的前端正在尝试调用API,但是出现了错误。错误信息如下:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Access to fetch at &#39;https://--brapoio-t.azurewebsites.net/-/-o&#39; from origin &#39;https://localhost:8080&#39; has been blocked by CORS policy: No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource. If an opaque response serves your needs, set the request&#39;s mode to &#39;no-cors&#39; to fetch the resource with CORS disabled.

<!-- end snippet -->

(URL被更改了,目的是在这里提问)

我的项目分为以下几部分:

main.go
routes:
//routes

main.go:

package main

import (
"os"

&quot;github.com/gin-gonic/gin&quot;
&quot;github.com/guilherm5/brapoio/routes&quot;

)

func main() {

Router := gin.New()
Router.Use(gin.LoggerWithWriter(gin.DefaultErrorWriter))

//tem toda minha parte de configura&#231;&#227;o
routes.ConfiguracaoSistema(Router)
routes.ConfiguracaoModulo(Router)
routes.ModuloSistema(Router)
routes.ArvoreMenu(Router)
routes.ModuloArvoreMenu(Router)

//tem toda minha parte de Usuario
routes.Usuario(Router)
routes.EmpresaUsuario(Router)
routes.CampoTabelaUsuario(Router)
routes.ProgramaUsuario(Router)
routes.FuncionalidadeProgramaUsuario(Router)
routes.TabelaUsuario(Router)
routes.ParametroFiltroTabelaUsuario(Router)
//meu servidor de autentica&#231;&#227;o
routes.JWTAutentication(Router)

listenAddr := &quot;:1000&quot;

if val, ok := os.LookupEnv(&quot;FUNCTIONS_CUSTOMHANDLER_PORT&quot;); ok {
	listenAddr = &quot;:&quot; + val
}

Router.Run(listenAddr)

}

routes包的示例代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

package routes

import (
	&quot;github.com/gin-gonic/gin&quot;
	&quot;github.com/guilherm5/brapoio/controllers&quot;
)

//NAO ESQUECER DE FAZER AZURE

func ModuloSistema(c *gin.Engine) {
	c.GET(&quot;/api/moduloSistema&quot;, controllers.GetModuloSistema())
	c.GET(&quot;/api/moduloSistemaID&quot;, controllers.GetModuloSistemaID())
	c.POST(&quot;/api/moduloSistema&quot;, controllers.NewModuloSistema())
	c.PUT(&quot;/api/moduloSistema&quot;, controllers.UpdateModuloSistemao())
	c.DELETE(&quot;/api/moduloSistema&quot;, controllers.DeleteModuloSistema())
}

<!-- end snippet -->

我该如何解决前端遇到的这个错误?

英文:

I have a server made in go, gin and libpq, my front end is trying to consume the api but it is giving an error..
this error:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Access to fetch at &#39;https://--brapoio-t.azurewebsites.net/-/-o&#39; from origin &#39;https://localhost:8080&#39; has been blocked by CORS policy: No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource. If an opaque response serves your needs, set the request&#39;s mode to &#39;no-cors&#39; to fetch the resource with CORS disabled.

<!-- end snippet -->

(the url was changed on purpose to ask the question here on stack overflow)

My project is divided like this:

main.go
routes:
//routes

main.go:

package main

import (
"os"

&quot;github.com/gin-gonic/gin&quot;
&quot;github.com/guilherm5/brapoio/routes&quot;

)

func main() {

Router := gin.New()
Router.Use(gin.LoggerWithWriter(gin.DefaultErrorWriter))

//tem toda minha parte de configura&#231;&#227;o
routes.ConfiguracaoSistema(Router)
routes.ConfiguracaoModulo(Router)
routes.ModuloSistema(Router)
routes.ArvoreMenu(Router)
routes.ModuloArvoreMenu(Router)

//tem toda minha parte de Usuario
routes.Usuario(Router)
routes.EmpresaUsuario(Router)
routes.CampoTabelaUsuario(Router)
routes.ProgramaUsuario(Router)
routes.FuncionalidadeProgramaUsuario(Router)
routes.TabelaUsuario(Router)
routes.ParametroFiltroTabelaUsuario(Router)
//meu servidor de autentica&#231;&#227;o
routes.JWTAutentication(Router)

listenAddr := &quot;:1000&quot;

if val, ok := os.LookupEnv(&quot;FUNCTIONS_CUSTOMHANDLER_PORT&quot;); ok {
	listenAddr = &quot;:&quot; + val
}

Router.Run(listenAddr)

}

an example of package routes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

package routes

import (
	&quot;github.com/gin-gonic/gin&quot;
	&quot;github.com/guilherm5/brapoio/controllers&quot;
)

//NAO ESQUECER DE FAZER AZURE

func ModuloSistema(c *gin.Engine) {
	c.GET(&quot;/api/moduloSistema&quot;, controllers.GetModuloSistema())
	c.GET(&quot;/api/moduloSistemaID&quot;, controllers.GetModuloSistemaID())
	c.POST(&quot;/api/moduloSistema&quot;, controllers.NewModuloSistema())
	c.PUT(&quot;/api/moduloSistema&quot;, controllers.UpdateModuloSistemao())
	c.DELETE(&quot;/api/moduloSistema&quot;, controllers.DeleteModuloSistema())
}

<!-- end snippet -->

How can I solve this error that my front end has been getting?

答案1

得分: 2

我认为首先你应该了解CORS(跨源资源共享)的原理,以理解为什么它会阻止你的前端网站访问资源。你可以阅读Mozilla的文档以获取更详细的信息。对于你使用Gin的情况,你可以添加CORS中间件来启用它。

英文:

I think first you should read about CORS to understand why it blocks your Frontend site to access the resources. You can read this doc from Mozilla for more detail. For your case with Gin, you can add CORS middleware to enable it.

huangapple
  • 本文由 发表于 2023年2月25日 19:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75565087.html
匿名

发表评论

匿名网友

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

确定