forked from jfberry/signpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokemon.go
38 lines (31 loc) · 1007 Bytes
/
pokemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"bytes"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func GetPokemon(c *gin.Context) {
pokemonId := c.Param("pokemon_id")
template := c.Param("template")
clientIP := c.GetHeader("CF-Connecting-IP")
// fallback if someone is not using cloudflare
if clientIP == "" {
clientIP = c.ClientIP()
}
var pokemonRecord any
err := getJson(fmt.Sprintf("%s/api/pokemon/id/%s", config.Golbat.Url, pokemonId), &pokemonRecord)
if pokemonRecord == nil {
msg := "Unable to get location, the pokemon might have despawned\n"
fmt.Printf("%s [%s] %s", time.Now().Format(config.TimestampFormat), clientIP, msg)
c.Data(http.StatusNotFound, "application/json; charset=utf-8", []byte(msg))
} else {
var doc bytes.Buffer
err = pokemonTemplate.ExecuteTemplate(&doc, template, pokemonRecord)
_ = err
s := doc.String()
fmt.Printf("%s [%s] Redirecting to %s\n", time.Now().Format(config.TimestampFormat), clientIP, s)
c.Redirect(http.StatusMovedPermanently, s)
}
}