-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (70 loc) · 2.33 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/json"
"fmt"
"log"
"log/slog"
"net/http"
"strings"
"github.com/a-h/templ"
"github.com/rkperes/pokehtmx/templates"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func main() {
component := templates.Index()
mux := http.NewServeMux()
mux.Handle("/", templ.Handler(component))
mux.HandleFunc("POST /search", searchHandler)
fmt.Println("Listening on :3000")
if err := http.ListenAndServe(":3000", mux); err != nil {
log.Fatalf("http.ListenAndServe: %v", err)
}
}
const pokeapiSearchPokemon = "https://pokeapi.co/api/v2/pokemon"
type pokepapiSearchPokemonResponse struct {
Id int `json:"id"`
Name string `json:"name"`
Sprites pokeapiSprites `json:"sprites"`
}
type pokeapiSprites struct {
FrontDefault string `json:"front_default"`
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
searchQuery := strings.ToLower(r.FormValue("pokemon"))
if len(searchQuery) == 0 {
slog.Error("pokeapi search empty", slog.Any("form", r.Form))
w.WriteHeader(http.StatusBadRequest)
return
}
u := fmt.Sprintf("%s/%s", pokeapiSearchPokemon, searchQuery)
resp, err := http.Get(u)
if err != nil {
slog.Error(fmt.Sprintf("pokeapi search: %v", err), slog.String("url", u))
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
if resp.StatusCode == http.StatusNotFound {
slog.Warn("pokeapi not found", slog.String("url", u))
http.Error(w, "Pokemon not found", http.StatusNotFound)
return
}
if resp.StatusCode != http.StatusOK {
slog.Error(fmt.Sprintf("pokeapi error: %v", err), slog.String("url", u))
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
var pokeapiResp pokepapiSearchPokemonResponse
if err := json.NewDecoder(resp.Body).Decode(&pokeapiResp); err != nil {
slog.Error(fmt.Sprintf("pokeapi unmarshal: %v", err), slog.String("url", u))
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
component := templates.SearchResultDisplay(templates.SearchResult{
Id: fmt.Sprintf("%03d", pokeapiResp.Id),
Name: cases.Title(language.English).String(pokeapiResp.Name),
SpriteURL: pokeapiResp.Sprites.FrontDefault,
})
slog.Info("serve search results", slog.Any("pokemon", pokeapiResp), slog.String("url", u))
templ.Handler(component).ServeHTTP(w, r)
}