Brijesh's Git Server — whois @ 17cd07e20e322ff28325829e826c8aeb7bb98800

Whois - whois.brijesh.dev

main.go (view raw)

 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
// package main
//
// import (
// 	"fmt"
//
// 	"github.com/likexian/whois"
// )
//
// func main() {
// 	result, _ := whois.Whois("brijesh.dev")
//
// 	fmt.Println("Result: ", result)
// }

package main

import (
	"html/template"
	"net/http"

	"github.com/likexian/whois"
)

type WHOISRecord struct {
	Domain string `json:"domain"`
	Result string `json:"result"`
}

var templates *template.Template

func main() {
	templates = template.Must(template.ParseGlob("templates/*.html"))
	http.HandleFunc("/", indexHandler)
	http.HandleFunc("/lookup", whoisLookupHandler)
	http.ListenAndServe(":8000", nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	templates.ExecuteTemplate(w, "index.html", nil)
}

func whoisLookupHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodGet {
		http.Redirect(w, r, "/", http.StatusSeeOther)
		return
	} else if r.Method != http.MethodPost {
		http.Error(w, "invalid request method", http.StatusMethodNotAllowed)
		return
	}

	domain := r.FormValue("domain")
	result, err := whois.Whois(domain)
	if err != nil {
		http.Error(w, "Error fetching WHOIS data", http.StatusInternalServerError)
		return
	}

	whoisRecord := &WHOISRecord{
		Domain: domain,
		Result: result,
	}

	templates.ExecuteTemplate(w, "index.html", map[string]any{
		"WHOISRecord": whoisRecord,
	})
}