I trying to make simple server that able to upload image and return the url of image with go.
here is my code
package controlers
import (
"context"
"net/http"
"os"
"github.com/codedius/imagekit-go"
"github.com/gin-gonic/gin"
)
func UploadImage(ctx *gin.Context) {
// Replace with your own API keys
publicKey := os.Getenv("IMAGEKITPUBLICKEY")
privateKey := os.Getenv("IMAGEKITPRIVATEKEY")
opts := imagekit.Options{
PublicKey: publicKey,
PrivateKey: privateKey,
}
// Create a new ImageKit client
client, err := imagekit.NewClient(&opts)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": "fail to create client imege",
})
}
// Open the image file to upload
file, err := os.Open("image.jpg")
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": "failed to open image file",
})
return
}
defer file.Close()
// Upload the image to ImageKit
uploadParams := imagekit.UploadRequest{
File: file,
FileName: "image.jpg",
UseUniqueFileName: false,
Tags: []string{"go", "image"},
Folder: "/",
IsPrivateFile: false,
}
c := context.Background()
uploadResult, err := client.Upload.ServerUpload(c, &uploadParams)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": "failed to upload image",
})
return
}
// Return the URL of the uploaded image as a JSON response
ctx.JSON(http.StatusOK, gin.H{
"url": uploadResult.URL,
})
}
it will be called using this router package
package routers
import (
"API-Books/controlers"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
func StartServer() *gin.Engine {
router := gin.Default()
// Enable CORS
router.Use(func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Next()
})
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:3000"}
router.Use(cors.New(config))
// another router
router.POST("/api/upload", controlers.UploadImage)
return router
}
)
and run from main function
package main
import (
"API-Books/routers"
)
func init() {
initializer.LoadEnvVar()
}
func main() {
routers.StartServer().Run()
}
I already put image file in same directory with main.go its named "image.jpg"
I want to when I try post a request it will upload image and returned a link (i will store it to database later, or if you have better solution other than imagekit.io I will apreciate (its just school project so please only free service"