1 Star 0 Fork 0

natas-public / utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
index.go 5.81 KB
一键复制 编辑 原始数据 按行查看 历史
徐文越 提交于 2022-01-07 10:17 . v0.0.1
package net
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type HttpClient struct {
Host string
Context string
Uri string
Url string
Method string
Headers map[string]string
Data interface{}
Timeout time.Duration
Then chan interface{}
Catch chan error
}
var RequestMethod struct {
GET string
POST string
PUT string
DELETE string
}
var RequestHeader struct {
XRequestedWith string
XRequestedWithXML string
ContentType string
ContentTypeJSON string
}
func init() {
RequestMethod.GET = "GET"
RequestMethod.POST = "POST"
RequestMethod.PUT = "PUT"
RequestMethod.DELETE = "DELETE"
RequestHeader.XRequestedWith = "X-Requested-With"
RequestHeader.XRequestedWithXML = "XMLHttpRequest"
RequestHeader.ContentType = "Content-Type"
RequestHeader.ContentTypeJSON = "application/json;charset=utf-8"
}
/*
nhc := net.NewHttpClient()
nhc.Host = "http://localhost:50021/ecowater-api/v1/captcha"
nhc.Then = make(chan interface{})
nhc.Catch = make(chan error)
nhc.Data = &models.ABC{
Name: "xxx",
Age: 13,
}
nhc.Post()
select {
case resp := <-nhc.Then:
{
logs.Info(resp)
}
case err := <-nhc.Catch:
{
logs.Error(err.Error())
}
}
*/
func NewHttpClient() *HttpClient {
return &HttpClient{
Headers: map[string]string{
RequestHeader.ContentType: RequestHeader.ContentTypeJSON,
RequestHeader.XRequestedWith: RequestHeader.XRequestedWithXML,
},
Timeout: 5000 * time.Second,
}
}
func (hc *HttpClient) Get() {
go func() {
req, err := http.NewRequest(RequestMethod.GET, hc.Host+hc.Context+hc.Uri, nil)
if nil != err {
if nil != hc.Catch {
hc.Catch <- err
}
return
}
hc.SetHeader(req)
client := &http.Client{Timeout: hc.Timeout}
resp, err := client.Do(req)
if nil != err {
if nil != hc.Catch {
hc.Catch <- err
}
return
}
defer func() {
_ = resp.Body.Close()
}()
body, err := ioutil.ReadAll(resp.Body)
if nil != err {
if nil != hc.Catch {
hc.Catch <- err
}
return
}
if nil != hc.Then {
hc.Then <- string(body)
}
}()
}
func (hc *HttpClient) GetSync() (string, error) {
req, err := http.NewRequest(RequestMethod.GET, hc.Host+hc.Context+hc.Uri, nil)
if nil != err {
return "", err
}
hc.SetHeader(req)
client := &http.Client{Timeout: hc.Timeout}
resp, err := client.Do(req)
if nil != err {
return "", err
}
defer func() {
_ = resp.Body.Close()
}()
body, err := ioutil.ReadAll(resp.Body)
if nil != err {
return "", err
}
return string(body), nil
}
func (hc *HttpClient) Post() {
go func() {
sendBody, err := json.Marshal(hc.Data)
if nil != err {
if nil != hc.Catch {
hc.Catch <- err
}
return
}
req, err := http.NewRequest(RequestMethod.POST, hc.Host+hc.Context+hc.Uri, strings.NewReader(string(sendBody)))
if nil != err {
if nil != hc.Catch {
hc.Catch <- err
}
return
}
hc.SetHeader(req)
client := &http.Client{Timeout: hc.Timeout}
resp, err := client.Do(req)
if nil != err {
if nil != hc.Catch {
hc.Catch <- err
}
return
}
defer func() {
_ = resp.Body.Close()
}()
body, err := ioutil.ReadAll(resp.Body)
if nil != err {
if nil != hc.Catch {
hc.Catch <- err
}
return
}
if nil != hc.Then {
hc.Then <- string(body)
}
}()
}
func (hc *HttpClient) PostSync() (string, error) {
sendBody, err := json.Marshal(hc.Data)
if nil != err {
return "", err
}
req, err := http.NewRequest(RequestMethod.POST, hc.Host+hc.Context+hc.Uri, strings.NewReader(string(sendBody)))
if nil != err {
return "", err
}
hc.SetHeader(req)
client := &http.Client{Timeout: hc.Timeout}
resp, err := client.Do(req)
if nil != err {
return "", err
}
defer func() {
_ = resp.Body.Close()
}()
body, err := ioutil.ReadAll(resp.Body)
if nil != err {
return "", err
}
return string(body), nil
}
func (hc *HttpClient) Put() {
}
func (hc *HttpClient) PutSync() {
}
func (hc *HttpClient) Delete() {
}
func (hc *HttpClient) DeleteSync() {
}
func (hc *HttpClient) SetHeader(req *http.Request, force ...bool) {
var forceFlag = false
if 0 < len(force) {
forceFlag = force[0]
}
for key, value := range hc.Headers {
if forceFlag {
req.Header.Del(key)
}
if "" != req.Header.Get(key) {
return
}
req.Header.Add(key, value)
}
}
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ipaddr := ""
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ipaddr = ipnet.IP.String()
}
}
}
return ipaddr
}
func MacInt64() int64 {
// 获取本机的MAC地址
interfaces, err := net.Interfaces()
if err != nil {
panic("Error : " + err.Error())
}
mac := ""
for _, inter := range interfaces {
if "" == string(inter.HardwareAddr) {
continue
}
mac = inter.HardwareAddr.String()
fmt.Println(inter.HardwareAddr)
break
}
mac = strings.ReplaceAll(mac, ":", "")
if "" == mac {
return 0
}
return Hex2Dec(mac)
}
func Mac() string {
interfaces, err := net.Interfaces()
if err != nil {
panic("Error : " + err.Error())
}
mac := ""
for _, inter := range interfaces {
if "" == string(inter.HardwareAddr) {
continue
}
mac = inter.HardwareAddr.String()
break
}
return mac
}
func Macs() []string {
interfaces, err := net.Interfaces()
if err != nil {
panic("Error : " + err.Error())
}
var mac []string
for _, inter := range interfaces {
if "" == string(inter.HardwareAddr) {
continue
}
mac = append(mac, inter.HardwareAddr.String())
}
return mac
}
func Hex2Dec(val string) int64 {
n, err := strconv.ParseInt(val, 16, 64)
if err != nil {
fmt.Println(err)
}
return n
}
Go
1
https://gitee.com/natas-public/utils.git
git@gitee.com:natas-public/utils.git
natas-public
utils
utils
v0.1.0

搜索帮助