29
loading...
This website collects cookies to deliver better user experience
~16mb
of memory, which was amazing!golang
docker imagedocker pull golang
mkdir go-mock-api
cd go-mock-api
4000
and mount the current directory (your go project dir) as volume and run our golang imagedocker run -itd -p 4000:4000 --name golang -v "$(pwd):/app" golang
docker exec -it -w /app golang bash
go version
api
modulego mod init api
go get -u github.com/gofiber/fiber/v2
api.go
with a sample hello world server on port 4000
as shown in the repository's readme.package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.SendString("Hello, World!")
})
app.Listen(":4000")
}
go get github.com/cespare/reflex
reflex -g '*.go' go run api.go --start-service
Starting service...
┌───────────────────────────────────────────────────┐
│ Fiber v2.13.0 │
│ http://127.0.0.1:4000 │
│ (bound on host 0.0.0.0 and port 4000) │
│ │
│ Handlers ............. 2 Processes ........... 1 │
│ Prefork ....... Disabled PID .............. 3315 │
└───────────────────────────────────────────────────┘
PATH
exported in your .bashrc
or .zshrc
file as export PATH=$PATH:$HOME/go/bin/
import "github.com/gofiber/fiber/v2"
user
route to the main functionapp.Get("/user", getUserHandler)
getUserHandler
which will handle the request. Here we will return a mock user.type User struct {
Name string `json:"name"`
Twitter string `json:"twitter"`
}
func getUserHandler(ctx *fiber.Ctx) error {
user := User{
Name: "Karan",
Twitter: "karan_6864",
}
return ctx.Status(fiber.StatusOK).JSON(user)
}
post
route to the main functionapp.Post("/user/create", createUserHandler)
createUserHandler
which will handle the request. Here we will simply parse the body and send it back in the responsefunc createUserHandler(ctx *fiber.Ctx) error {
body := new(User)
err := ctx.BodyParser(body)
if err != nil {
ctx.Status(fiber.StatusBadRequest)
return err
}
user := User{
Name: body.Name,
Twitter: body.Twitter,
}
return ctx.Status(fiber.StatusOK).JSON(user)
}
import (
"github.com/gofiber/fiber/v2/middleware/logger"
)
main
functionapp.Use(logger.New())
12:04:01 | 200 | 1ms | 172.17.0.1 | GET | /user
12:04:27 | 200 | 0s | 172.17.0.1 | POST | /user/create
app.Use(func(ctx *fiber.Ctx) error {
fmt.Println("Sample middleware")
return ctx.Next()
})
userApi := app.Group("/user")
userApi.Get("/", getUserHander)
userApi.Post("/create", createUserHandler)
public
folder, we can simply use the Static
function like belowapp.Static("/", "./public")
api.go
package main
import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
)
type User struct {
Name string `json:"name"`
Twitter string `json:"twitter"`
}
func getUserHander(ctx *fiber.Ctx) error {
user := User{
Name: "Karan",
Twitter: "karan_6864",
}
return ctx.Status(fiber.StatusOK).JSON(user)
}
func createUserHandler(ctx *fiber.Ctx) error {
body := new(User)
err := ctx.BodyParser(body)
if err != nil {
fmt.Println(err)
ctx.Status(fiber.StatusBadRequest)
return err
}
user := User{
Name: body.Name,
Twitter: body.Twitter,
}
return ctx.Status(fiber.StatusOK).JSON(user)
}
func main() {
app := fiber.New()
app.Use(logger.New())
userApi := app.Group("/user")
userApi.Get("/", getUserHander)
userApi.Post("/create", createUserHandler)
app.Listen(":4000")
}