On a daily basis we need more than just trafficking JSON. Sometimes the flow contains external media (Multipart Binaries) or metrics stuck in bottlenecks, where we introduce utilities to ease the journeys of the DEV and the User.
Location: /src/common/utils/file-upload.util.go
In Node, we suffered to learn to implement "Multipart" interception. The Gost lib introduces into its tool drawers (utils/) the simple magic of UploadImage, which imitates the native behavior of downloading Binary Payloads.
When hitting the endpoints head-on (like our avatar example: POST /users/:id/avatar), an Android or Front-end client fires its binary form on network.
In the controller we call the engine:
func UploadImage(c *gin.Context, fieldName string, destFolder string)How it works Didactically:
- The tool retrieves the buffer with Gin
c.FormFile("fieldName"). (In frontend the file input must be called the same field). - If the local backend (in container) does not have the referred folder (e.g.,
./uploads), it builds the folder dynamically (os.MkdirAll) using global permissions support (os.ModePerm). - Instead of using playful names, it creates cryptographic cache overwrite protection converting the original name to a UNIX millisecond and returning (e.g.,
170293023901.png). - It allocates on Secondary Memory (Disk, I/O) and returns to database in your Controller DTO (
filePath, err). The Service now saves this absolute link with ease! You have all the data processed in a clean native function!
Location: /src/config/redis.go and Global instantiations.
In heavy APIs, direct listing requests (like GET /users/super-query) violently hurt the Database and generate instance costs! What if you want to do caching like in nest providers with @Cache() interceptors?
We injected from the native Core ecosystem and activated with the parallel containers supported through a built-in Golang SDK: github.com/go-redis/redis/v8.
Configured on Boot, ready to use! Whenever your system needs to store ready results without triggering Repositories (Read/Write Intensive Flows), use from any file in your architecture (In Services):
import "gost/src/config"
import "time"
// Saving the Payload that was already serialized from JSON to the Temporary Redis Db, expiring in hours
config.RedisClient.Set(config.Ctx, "my_query_v1", listResults, time.Hour)
// Reloading (Fetching to return in view even before hitting the SQL API):
val, err := config.RedisClient.Get(config.Ctx, "my_query_v1").Result()Power in hands without headache of configuring clients in pool every time, already modular and singleton.
The End! With these utilities in hand + Severe Validation (Pipes) + Acentric Injection (Modules), Gost consolidates itself as a fantastic architectural casing for teams migrating or implementing robustness without the "overhead" that would be expected in Go! Happy Coding!