43 lines
900 B
Go
43 lines
900 B
Go
![]() |
package main
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"net/http"
|
||
|
"fmt"
|
||
|
"encoding/json"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
router := gin.Default()
|
||
|
router.GET("/", func(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"message": "Hiya!",
|
||
|
})
|
||
|
})
|
||
|
router.GET("/ping", func(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"message": "pong",
|
||
|
})
|
||
|
})
|
||
|
router.GET("/pokemon/:name", func(c *gin.Context) {
|
||
|
name := c.Param("name")
|
||
|
url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon/%s", name)
|
||
|
res, err := http.Get(url)
|
||
|
if err != nil {
|
||
|
fmt.Printf("Could not retrieve info for Pokemon %s!", name)
|
||
|
}
|
||
|
|
||
|
var result map[string]any
|
||
|
decoder := json.NewDecoder(res.Body)
|
||
|
defer res.Body.Close()
|
||
|
if err := decoder.Decode(&result); err != nil {
|
||
|
fmt.Printf("Could not read response body!")
|
||
|
}
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"message": "success",
|
||
|
"pokemon_info": result,
|
||
|
})
|
||
|
})
|
||
|
router.Run()
|
||
|
}
|