Imagekit – Golang | How to use Context

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

Imagekit - Golang | How to use Context

问题

我正在尝试通过imagekit sdk将图像从我的Golang后端上传到imagekit。我在理解上下文API的用途方面遇到了问题。

目前,我在后端中并没有真正使用上下文,并且将所有依赖项都传递给构造函数。

我遇到了一个异常:

2023/01/04 09:53:56 http: panic serving 127.0.0.1:50780: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
net/http.(*conn).serve.func1()
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:1850 +0xbf
panic({0x1005836c0, 0x10092b820})
        /usr/local/Cellar/go/1.19.3/libexec/src/runtime/panic.go:890 +0x262
github.com/bolzfieber/bolzfieber-backend/internal/controllers.PitchesController.CreateAndUpdatePitch({0xc0001f7530?, 0x0?}, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /Users/pbahr/DEV/Bolzfieber/BE/bolzfieber-backend/internal/controllers/pitches.go:91 +0x5ae
net/http.HandlerFunc.ServeHTTP(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000be048?)
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).routeHTTP(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 +0x216
net/http.HandlerFunc.ServeHTTP(0xc0000b00c0?, {0x1006a94e8?, 0xc0000ca000?}, 0xc00011b918?)
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).ServeHTTP(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:71 +0x355
github.com/go-chi/chi/v5.(*Mux).Mount.func1({0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:314 +0x19c
net/http.HandlerFunc.ServeHTTP(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000aa005?)
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).routeHTTP(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 +0x216
net/http.HandlerFunc.ServeHTTP(0x1006a9bf8?, {0x1006a94e8?, 0xc0000ca000?}, 0x10092b640?)
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).ServeHTTP(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)
        /Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:88 +0x310
net/http.serverHandler.ServeHTTP({0x1006a84d0?}, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2947 +0x30c
net/http.(*conn).serve(0xc00032c000, {0x1006a9ca0, 0xc000318270})
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:1991 +0x607
created by net/http.(*Server).Serve
        /usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:3102 +0x4db

其中第91行是imagekit的上传代码行。所以我猜空指针是上下文。

我的当前方法如下:

func (c PitchesController) CreateAndUpdatePitch(w http.ResponseWriter, r *http.Request) {
	var pitchUpload models.PitchUpload
	err := json.NewDecoder(r.Body).Decode(&pitchUpload)

	if err != nil {
		fmt.Printf("Error: %v\n", err)
		utils.RespondWithError(w, http.StatusBadRequest, "Bad request")
		return
	}

	var pitch = models.Pitch{
		Name:            pitchUpload.Name,
		Address:         pitchUpload.Address,
		Author:          pitchUpload.Author,
		PitchType:       pitchUpload.PitchType,
		PitchSize:       pitchUpload.PitchSize,
		GoalSize:        pitchUpload.GoalSize,
		GoalType:        pitchUpload.GoalType,
		LocationScopeId: pitchUpload.LocationScopeId,
		Coordinates:     pitchUpload.Coordinates,
		Rating:          pitchUpload.Rating,
	}

	for i, image := range pitchUpload.Images {
		fmt.Printf("pitch: %v\n", pitchUpload.Name)
		response, uploadErr := c.Ik.Uploader.Upload(r.Context(), image, uploader.UploadParam{
			FileName:          pitchUpload.Name + "_" + string(rune(i)),
			UseUniqueFileName: newTrue(),
			Tags:              "pitch",
			Folder:            "/pitches/" + pitchUpload.Name,
		})

		if uploadErr != nil {
			fmt.Printf("Error: %v\n", uploadErr)
			utils.RespondWithError(w, http.StatusBadRequest, "Bad request")
			return
		}

		pitch.Images = append(pitch.Images, models.PitchImage{FileUrl: response.Data.Url})
	}

	c.Db.Save(&pitch)

	utils.RespondWithJSON(w, http.StatusCreated, "Created successfully")
}

以下是步骤:

  1. 接收POST请求的主体并解码为有效的Go结构体。
  2. 创建一个新的结构体,稍后应保存到数据库中。
  3. 遍历结构体的图像(base64字符串)。
  4. 对于每个图像,上传到imagekit。
  5. 将imagekit的响应附加到应保存到数据库中的结构体。
  6. 保存到数据库。
英文:

Im trying to upload images from my Golang backend to imagekit via the imagekit sdk. Im facing problems with understanding what the context api is used for.

Currently I don't really use context in my backend and pass all dependencies in the constructor.

Im running into an exception:

2023/01/04 09:53:56 http: panic serving 127.0.0.1:50780: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
net/http.(*conn).serve.func1()
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:1850 +0xbf
panic({0x1005836c0, 0x10092b820})
/usr/local/Cellar/go/1.19.3/libexec/src/runtime/panic.go:890 +0x262
github.com/bolzfieber/bolzfieber-backend/internal/controllers.PitchesController.CreateAndUpdatePitch({0xc0001f7530?, 0x0?}, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
/Users/pbahr/DEV/Bolzfieber/BE/bolzfieber-backend/internal/controllers/pitches.go:91 +0x5ae
net/http.HandlerFunc.ServeHTTP(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000be048?)
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).routeHTTP(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
/Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 +0x216
net/http.HandlerFunc.ServeHTTP(0xc0000b00c0?, {0x1006a94e8?, 0xc0000ca000?}, 0xc00011b918?)
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).ServeHTTP(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
/Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:71 +0x355
github.com/go-chi/chi/v5.(*Mux).Mount.func1({0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
/Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:314 +0x19c
net/http.HandlerFunc.ServeHTTP(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000aa005?)
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).routeHTTP(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
/Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 +0x216
net/http.HandlerFunc.ServeHTTP(0x1006a9bf8?, {0x1006a94e8?, 0xc0000ca000?}, 0x10092b640?)
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*Mux).ServeHTTP(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)
/Users/pbahr/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:88 +0x310
net/http.serverHandler.ServeHTTP({0x1006a84d0?}, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:2947 +0x30c
net/http.(*conn).serve(0xc00032c000, {0x1006a9ca0, 0xc000318270})
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:1991 +0x607
created by net/http.(*Server).Serve
/usr/local/Cellar/go/1.19.3/libexec/src/net/http/server.go:3102 +0x4db

Whereas line 91 is the upload line of the imagekit. So I suppose the null pointer is the context.

My current approach looks like this:

func (c PitchesController) CreateAndUpdatePitch(w http.ResponseWriter, r *http.Request) {
	var pitchUpload models.PitchUpload
	err := json.NewDecoder(r.Body).Decode(&pitchUpload)

	if err != nil {
		fmt.Printf("Error: %v\n", err)
		utils.RespondWithError(w, http.StatusBadRequest, "Bad request")
		return
	}

	var pitch = models.Pitch{
		Name:            pitchUpload.Name,
		Address:         pitchUpload.Address,
		Author:          pitchUpload.Author,
		PitchType:       pitchUpload.PitchType,
		PitchSize:       pitchUpload.PitchSize,
		GoalSize:        pitchUpload.GoalSize,
		GoalType:        pitchUpload.GoalType,
		LocationScopeId: pitchUpload.LocationScopeId,
		Coordinates:     pitchUpload.Coordinates,
		Rating:          pitchUpload.Rating,
	}

	for i, image := range pitchUpload.Images {
		fmt.Printf("pitch: %v\n", pitchUpload.Name)
		response, uploadErr := c.Ik.Uploader.Upload(r.Context(), image, uploader.UploadParam{
			FileName:          pitchUpload.Name + "_" + string(rune(i)),
			UseUniqueFileName: newTrue(),
			Tags:              "pitch",
			Folder:            "/pitches/" + pitchUpload.Name,
		})

		if uploadErr != nil {
			fmt.Printf("Error: %v\n", uploadErr)
			utils.RespondWithError(w, http.StatusBadRequest, "Bad request")
			return
		}

		pitch.Images = append(pitch.Images, models.PitchImage{FileUrl: response.Data.Url})
	}

	c.Db.Save(&pitch)

	utils.RespondWithJSON(w, http.StatusCreated, "Created successfully")
}

Following steps apply:

  1. receiving post body and decode to valid go struct
  2. creating a new struct that later should be saved to db
  3. looping over the images of the struct (base64 strings)
  4. for each image upload to imagekit
  5. append imagekit responses to struct that should be saved to db
  6. save to db

答案1

得分: 2

在这一行出现空指针错误,很可能是因为c.Ik.、c.Ik.Uploader、r或uploader为空。我们可以轻松地排除其中的一些可能性。

英文:

Getting a nil pointer at this line probably means c.Ik. or c.Ik.Uploader. or r. or uploader. are nils. We can easily remove some of suspects.

huangapple
  • 本文由 发表于 2023年1月4日 17:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75003092.html
匿名

发表评论

匿名网友

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

确定