草庐IT

Web框架

全部标签

go - 如何为 go Web api 编写单元测试

我正在为golangwebAPI编写单元测试。我有一个类似domain.com/api/user/current的API。此API将返回用户登录的信息。我使用http.NewRequest向该API发出请求。但它不起作用。那么如何在记录权限的情况下对这个API进行self调用?funcTestGetCurrentUserLogged(t*testing.T){assert:=assert.New(t)varurl="http://example.com/user/current";req,_:=http.NewRequest(http.MethodGet,url,nil)client:

amazon-web-services - 多年来无法预签名 URL?

签名版本4最多可使用一周。在Python中我做了:s3_client=boto3.client('s3',aws_access_key_id=access_key,aws_secret_access_key=secret_key,config=botocore.client.Config(signature_version='s3'))returns3_client.generate_presigned_url('get_object',Params={'Bucket':bucket_name,'Key':key},ExpiresIn=400000000)#thisisamax:~te

json - 使用 Echo 或 Gin 框架的大型数组的内存消耗

当我尝试用Echo(还有Gin)发送一个大数组时,我遇到了内存问题。请求后,内存不空闲。packagemainimport("net/http""strconv""github.com/labstack/echo")typeUserstruct{UsernamestringPasswordstringLastnamestringFirstnamestring}funcmain(){e:=echo.New()e.GET("/",func(cecho.Context)error{varuserUserusers:=make([]User,0)fori:=0;i为了测试,我并行运行请求并得到

go - 如何实现fasthttp框架

我想从这个链接开始学习fasthttps服务器https://github.com/valyala/fasthttp但我不知道如何在这个框架中实现一小段代码。谁能告诉我我将如何在其中实现一小段代码?请举个例子。我试过的代码packagemainimport"fmt"typeMyHandlerstruct{foobarstring}funcmain(){//passboundstructmethodtofasthttpmyHandler:=&MyHandler{foobar:"foobar",}fasthttp.ListenAndServe(":8080",myHandler.Handl

go - 访问 Golang Web 服务器

我知道Golang包含原生的内置网络服务器(net/http),可以在不使用外部网络服务器(apache、nginx等)的情况下用作服务器。对于本地开发,您只需运行http.ListenAndServe即可在本地准备好您的服务器。我的问题是,如何设置您的golang应用程序以供其他人公开访问而无需外部网络服务器? 最佳答案 在您的golang代码中,除了指定您的应用监听的端口外,您无需执行任何其他操作。设置端口后(例如:8080),您需要执行以下操作以使其可访问:如果您希望您的应用可在内部访问(LAN/WLAN),请检查您的私有(p

web - 为什么当我再次运行 main.go 时 View 相同

主.gopackagemainimport("html/template""net/http")vartemplates=template.Must(template.ParseGlob("./templates/*"))funcviewHandler(whttp.ResponseWriter,r*http.Request){err:=templates.ExecuteTemplate(w,"indexPage",nil)iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}}funcmain

amazon-web-services - 从快照 ID 获取卷 ID 的方法?

我有一个快照ID,想知道是否有任何方法可以获取快照来自的卷ID。 最佳答案 使用DescribeSnapshotsAPI调用。这是一个使用AWSCommand-LineInterface(CLI)的示例:$awsec2describe-snapshots--snapshot-idssnap-5caa7fb4--query'Snapshots[*].VolumeId'--outputtextvol-bd9b80c5 关于amazon-web-services-从快照ID获取卷ID的方法?,

go - Go Web 服务器在哪里查找文件

我有一个简单的网络应用程序,名为HttpServer.go的代码文件是:packagemainimport("net/http")funcmain(){mux:=http.NewServeMux()files:=http.FileServer(http.Dir("/public"))mux.Handle("/static/",http.StripPrefix("/static/",files))server:=&http.Server{Addr:"localhost:8080",Handler:mux,}server.ListenAndServe()}我把这个代码文件放在%GOPATH

docker - 将框架 Gin 切换到 Echo 后,服务器不再响应

我以前用Gin(Golang框架),部署docker镜像到GKE。它工作得很好。但是我把Gin换成Echo(也是Golang框架),服务器就没有响应了我认为是因为端口组合(端口转发)有问题。我的回显服务器代码如下。funcmain(){e:=presentation.Router()e.Logger.Fatal(e.Start(":8080"))//listenandserveon:8080}我的dockerfile如下所示。FROMalpine:3.9WORKDIR/appADDmain/appENVPORT80EXPOSE80CMD["./main"]当请求到达80端口时,它必须渲

multithreading - 基于 Go 的 http 包构建的 Web 应用程序是否作为使用多个线程处理传入请求的单个进程工作?

我读到Go应用程序使用内置Web服务器直接从客户端接收连接,而不是在Apache等Web服务器后面运行。此外,我还阅读了网络服务器(例如Apache)使用由fork()创建的多个进程处理传入请求的信息。对于Go应用程序也是如此,还是它在单个进程上运行并通过多个线程处理传入请求? 最佳答案 Go应用程序通常使用net/http包来实现Web服务器。documentationforthatpackage说:ServeacceptsincomingHTTPconnectionsonthelistenerl,creatinganewserv