package main

import (
	"IPRegion/global"
	"IPRegion/ip2region"
	"IPRegion/util"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", IPHandle)
	log.Println("server start!")
	http.ListenAndServe(":8080", nil)
}

func IPHandle(w http.ResponseWriter, req *http.Request) {
	address := req.FormValue("address")
	var ipInfo ip2region.IpInfo
	if address == "" {
		addressReq, _ := util.GetIP(req)
		address = addressReq
	}

	ipInfo, _ = ip2region.GetIPInfo(address, global.SearchType)
	ipInfo.Domain = address
	res, _ := json.Marshal(ipInfo)
	fmt.Fprintln(w, string(res))

}
