fix cors and add html template
All checks were successful
Build docker container / Build image (push) Successful in 30s
All checks were successful
Build docker container / Build image (push) Successful in 30s
This commit is contained in:
parent
1d1f9b0fe7
commit
c96249c0b5
18
main.go
18
main.go
@ -4,9 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddressType string
|
type AddressType string
|
||||||
@ -82,8 +84,19 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/":
|
case "/":
|
||||||
fmt.Fprintf(w, "%s\n%s", netAddr.Address, netAddr.Type)
|
fp := path.Join("templates", "index.html")
|
||||||
case "/api":
|
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)
|
responseJSON(w, netAddr)
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
@ -95,6 +108,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func responseJSON(w http.ResponseWriter, data interface{}) {
|
func responseJSON(w http.ResponseWriter, data interface{}) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
err := json.NewEncoder(w).Encode(data)
|
err := json.NewEncoder(w).Encode(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
|
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
|
||||||
|
@ -6,7 +6,19 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
IPv4: asdf
|
IP: {{ .Address }}<br />
|
||||||
IPv6: asdf
|
IP: {{ .Type}}<br />
|
||||||
</body>
|
</body>
|
||||||
|
<script>
|
||||||
|
async function getIp() {
|
||||||
|
const isIpv4 = "{{ .Type }}" == "IPv4";
|
||||||
|
let requestUrl = isIpv4 ? "https://ip6.mziesel.nl/json" : "https://ip4.mziesel.nl/json";
|
||||||
|
|
||||||
|
const response = await fetch(requestUrl);
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
console.log(data)
|
||||||
|
}
|
||||||
|
getIp();
|
||||||
|
</script>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user