diff --git a/main.go b/main.go index 32925cb..6764e61 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,11 @@ import ( "context" "encoding/json" "fmt" + "html/template" "log" "net" "net/http" + "path" ) type AddressType string @@ -82,8 +84,19 @@ func mainHandler(w http.ResponseWriter, r *http.Request) { case http.MethodGet: switch r.URL.Path { case "/": - fmt.Fprintf(w, "%s\n%s", netAddr.Address, netAddr.Type) - case "/api": + fp := path.Join("templates", "index.html") + tmpl, err := template.ParseFiles(fp) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if err := tmpl.Execute(w, netAddr); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + case "/raw": + fmt.Fprintf(w, "%s\n%s\n", netAddr.Address, netAddr.Type) + case "/json": responseJSON(w, netAddr) default: http.NotFound(w, r) @@ -95,6 +108,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) { func responseJSON(w http.ResponseWriter, data interface{}) { w.Header().Set("Content-Type", "application/json") + w.Header().Set("Access-Control-Allow-Origin", "*") err := json.NewEncoder(w).Encode(data) if err != nil { http.Error(w, "Failed to encode JSON", http.StatusInternalServerError) diff --git a/templates/index.html b/templates/index.html index 6d58ac4..ab25390 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,7 +6,19 @@ - IPv4: asdf - IPv6: asdf + IP: {{ .Address }}
+ IP: {{ .Type}}
+