@@ -3,14 +3,19 @@ package main
33import (
44 "encoding/json"
55 "fmt"
6+ "log/slog"
67 "net/http"
78 "strings"
89
910 "github.com/yosida95/uritemplate"
1011)
1112
13+ type jsonError struct {
14+ Error string
15+ }
16+
1217func match (w http.ResponseWriter , r * http.Request ) {
13- w .Header ().Set ("Content-Type" , "application/json " )
18+ w .Header ().Set ("Content-Type" , "application/payload " )
1419 w .Header ().Set ("X-Content-Type-Options" , "nosniff" )
1520 w .Header ().Set ("X-Frame-Options" , "deny" )
1621
@@ -19,39 +24,44 @@ func match(w http.ResponseWriter, r *http.Request) {
1924
2025 errors := make ([]string , 0 , 2 )
2126 if template == "" {
22- errors = append (errors , " The \ " template\ " parameter is mandatory." )
27+ errors = append (errors , ` The "template" parameter is mandatory.` )
2328 }
2429
2530 uri := query .Get ("uri" )
2631 if uri == "" {
27- errors = append (errors , " The \ " uri\ " parameter is mandatory." )
32+ errors = append (errors , ` The "uri" parameter is mandatory.` )
2833 }
2934
3035 if len (errors ) > 0 {
31- err , _ := json .MarshalIndent (error {strings .Join (errors , " " )}, "" , " " )
32- http .Error (w , string (err ), http .StatusBadRequest )
36+ payload , _ := json .MarshalIndent (jsonError {strings .Join (errors , " " )}, "" , " " )
37+ http .Error (w , string (payload ), http .StatusBadRequest )
38+
3339 return
3440 }
3541
3642 tpl , err := uritemplate .New (template )
3743 if nil != err {
38- err , _ := json .MarshalIndent (error {fmt .Sprintf ("\" %s\" is not a valid URI template (RFC6570)." , template )}, "" , " " )
39- http .Error (w , string (err ), http .StatusBadRequest )
44+ payload , _ := json .MarshalIndent (jsonError {fmt .Sprintf (`"%s" is not a valid URI template (RFC6570).` , template )}, "" , " " )
45+ http .Error (w , string (payload ), http .StatusBadRequest )
46+
4047 return
4148 }
4249
4350 match := tpl .Match (uri )
4451 if match == nil {
45- err , _ := json .Marshal (struct { Match bool }{false })
46- fmt .Fprintf (w , "%s" , string (err ))
52+ payload , _ := json .Marshal (struct { Match bool }{false })
53+ if _ , err := w .Write (payload ); err != nil {
54+ slog .Info ("failed to write response" , "remote_addr" , r .RemoteAddr , "error" , err )
55+ }
56+
4757 return
4858 }
4959
50- json , _ := json .MarshalIndent (struct {
60+ payload , _ := json .MarshalIndent (struct {
5161 Match bool
5262 Values uritemplate.Values
5363 }{true , match }, "" , " " )
54- if _ , err := w .Write (json ); err != nil {
55- panic ( err )
64+ if _ , err := w .Write (payload ); err != nil {
65+ slog . Info ( "failed to write response" , "remote_addr" , r . RemoteAddr , "error" , err )
5666 }
5767}
0 commit comments