mirror of
https://github.com/mpolden/echoip.git
synced 2024-11-10 15:37:24 +01:00
Render HTML template if User-Agent is a regular browser
This commit is contained in:
parent
afebbf00f5
commit
f0bed47d1e
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -9,6 +10,10 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
Host string
|
||||||
|
}
|
||||||
|
|
||||||
func isCli(userAgent string) bool {
|
func isCli(userAgent string) bool {
|
||||||
match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$",
|
match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$",
|
||||||
userAgent)
|
userAgent)
|
||||||
@ -31,7 +36,9 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
if isCli(req.UserAgent()) {
|
if isCli(req.UserAgent()) {
|
||||||
io.WriteString(w, fmt.Sprintf("%s\n", host))
|
io.WriteString(w, fmt.Sprintf("%s\n", host))
|
||||||
} else {
|
} else {
|
||||||
// XXX: Render HTML
|
t, _ := template.ParseFiles("index.html")
|
||||||
|
client := &Client{Host: host}
|
||||||
|
t.Execute(w, client)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user