Skip to content

Instantly share code, notes, and snippets.

@deepanshumehtaa
Last active October 1, 2023 14:31
Show Gist options
  • Save deepanshumehtaa/373d9815f7d64cd3ad721c9d6b63db10 to your computer and use it in GitHub Desktop.
Save deepanshumehtaa/373d9815f7d64cd3ad721c9d6b63db10 to your computer and use it in GitHub Desktop.
Simple Gin App
/*
create `go.mod` (requirements.txt of go):
> go mod init myApp/web-service-gin
then run
> go get github.com/gin-gonic/gin
> github.com/thoas/go-funk
create `main.go`
```go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/albums", <function_reference>)
router.Run("localhost:8080")
}
```
To run go server:
> go run .
Download and Install dependencies from `go.mod`
> go mod download
to install
> `go build` or `go install`
*/
package main
import (
"github.com/gin-gonic/gin"
"github.com/thoas/go-funk"
"net/http"
"fmt"
)
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
var albums = []album{
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
}
func getAlbums(c *gin.Context) {
userId := c.Param("id")
fmt.Println("params are: ", userId)
c.IndentedJSON(
http.StatusOK,
albums,
)
}
// by id
func getAlbumById(ctx *gin.Context) {
albumID := ctx.Param("id")
fmt.Println("albumID", albumID)
getIdData := func(obj album) bool {
if obj.ID == albumID {
return true
}
return false
}
data := funk.Filter(albums, getIdData)
ctx.JSON(200, data)
}
// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
var newAlbum album
// `BindJSON`: binding and parsing JSON data from an HTTP request's body
if err := c.BindJSON(&newAlbum); err != nil {
c.JSON(
400, // StatusBadRequest
gin.H{"error": err.Error()},
)
return
}
// Add the new album to the slice.
albums = append(albums, newAlbum)
c.IndentedJSON(
201, // http.StatusCreated
newAlbum,
)
}
func main() {
router := gin.Default()
// lambda or closure functions
getHealth := func(ctx *gin.Context) {
data := map[string]any{
"live": true,
"status": "ok",
}
// returning responses
ctx.JSON(200, data)
}
router.GET("/health", getHealth)
router.GET("/albums", getAlbums)
router.GET("/album/:id", getAlbumById)
router.POST("/album/create", postAlbums)
router.Run("localhost:8080")
}
/*
go.mod
module api/myApp/
go 1.18
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/cors v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/thoas/go-funk v0.9.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment