1
0
mirror of https://github.com/IceWhaleTech/CasaOS.git synced 2025-07-06 23:37:26 +02:00
Files
CasaOS/pkg/utils/httper/httper.go

156 lines
3.2 KiB
Go
Raw Normal View History

2021-09-26 10:35:02 +08:00
package httper
import (
"bytes"
2022-04-06 12:10:51 +08:00
"fmt"
2021-09-26 10:35:02 +08:00
"io"
"io/ioutil"
"net/http"
"time"
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/tidwall/gjson"
2021-09-26 10:35:02 +08:00
)
// 发送GET请求
// url:请求地址
// response:请求返回的内容
2021-09-26 10:35:02 +08:00
func Get(url string, head map[string]string) (response string) {
2022-02-28 14:14:39 +08:00
client := &http.Client{Timeout: 30 * time.Second}
2021-09-26 10:35:02 +08:00
req, err := http.NewRequest("GET", url, nil)
2022-02-28 14:14:39 +08:00
2021-09-26 10:35:02 +08:00
for k, v := range head {
req.Header.Add(k, v)
}
if err != nil {
return ""
}
resp, err := client.Do(req)
if err != nil {
✨ New Feature - [Apps] This is a feature that has been highly requested by the community. Import the original Docker application into CasaOS. Now it's easy to import with just a few clicks! - [Apps] App list supports a custom sorting function! You can arrange apps in different orders by dragging the icons. - [Apps] App custom installation supports Docker Compose configuration import in YAML format. - [Files] Added thumbnail preview function for image files. - [Connect] Multiple CasaConenct devices in the LAN will be transmitted through the LAN network. - [System] Added a switch for auto-mounting USB disk devices. 🎈 Enhancement - [System] Optimized the system update alert, you will see the new version update log from the next version. - [Apps] Added live preview for icons in custom installed apps. - [Apps] Optimized the input of WebUI. - [Files] Completely updated the image preview, now it supports switching all images in the same folder, as well as dragging, zooming, rotating and resetting. - [Widgets] Added color levels for CPU and RAM charts. - [Conenct] Optimized the display of the right-click menu of the Connect friends list. 🎈 Changed - [Files] Change the initial display directory to /DATA 🐞 Fixed - [System] Fixed an issue with Raspberry Pi devices failing to boot using USB disks. (Achieved by disabling USB disk auto-mount) - [Apps] Fixed the issue that some Docker CLI commands failed to import. - [Apps] Fixed the issue that the app is not easily recognized in /DATA/AppData directory and docker command line after installation, it will be shown as the app name. (Newly installed apps only) - [Apps] Fixed the issue that Pi-hole cannot be launched after installation in the app store. - [Apps] Fixed the issue that apps cannot be updated with WatchTower. - [Files] Fixed the issue that when there is an upload task, the task status is lost after closing Files.
2022-05-13 18:12:26 +08:00
fmt.Println(err)
// 需要错误日志的处理
// logger.Error(error)
2021-09-26 10:35:02 +08:00
return ""
// panic(error)
2021-09-26 10:35:02 +08:00
}
defer resp.Body.Close()
var buffer [512]byte
result := bytes.NewBuffer(nil)
for {
n, err := resp.Body.Read(buffer[0:])
result.Write(buffer[0:n])
if err != nil && err == io.EOF {
break
} else if err != nil {
// logger.Error(err)
2021-09-26 10:35:02 +08:00
return ""
// panic(err)
}
}
response = result.String()
return
}
// 发送GET请求
// url:请求地址
// response:请求返回的内容
2022-05-05 13:46:55 +08:00
func PersonGet(url string) (response string) {
2021-09-26 10:35:02 +08:00
client := &http.Client{Timeout: 5 * time.Second}
2022-05-05 13:46:55 +08:00
req, err := http.NewRequest("GET", url, nil)
2021-09-26 10:35:02 +08:00
if err != nil {
2022-05-05 13:46:55 +08:00
return ""
2021-09-26 10:35:02 +08:00
}
2022-05-05 13:46:55 +08:00
resp, err := client.Do(req)
if err != nil {
// 需要错误日志的处理
// logger.Error(error)
2022-05-05 13:46:55 +08:00
return ""
// panic(error)
2021-09-26 10:35:02 +08:00
}
defer resp.Body.Close()
2022-05-05 13:46:55 +08:00
var buffer [512]byte
result := bytes.NewBuffer(nil)
for {
n, err := resp.Body.Read(buffer[0:])
result.Write(buffer[0:n])
if err != nil && err == io.EOF {
break
} else if err != nil {
// logger.Error(err)
2022-05-05 13:46:55 +08:00
return ""
// panic(err)
}
}
response = result.String()
2021-09-26 10:35:02 +08:00
return
}
// 发送POST请求
// url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
// content:请求放回的内容
2022-05-05 13:46:55 +08:00
func Post(url string, data []byte, contentType string, head map[string]string) (content string) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
req.Header.Add("content-type", contentType)
2021-09-26 10:35:02 +08:00
for k, v := range head {
req.Header.Add(k, v)
}
if err != nil {
panic(err)
}
2022-05-05 13:46:55 +08:00
client := &http.Client{Timeout: 5 * time.Second}
2021-09-26 10:35:02 +08:00
resp, error := client.Do(req)
if error != nil {
2022-05-05 13:46:55 +08:00
fmt.Println(error)
return
2021-09-26 10:35:02 +08:00
}
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
content = string(result)
return
}
// 发送POST请求
// url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
// content:请求放回的内容
2021-09-26 10:35:02 +08:00
func ZeroTierGet(url string, head map[string]string) (content string, code int) {
req, err := http.NewRequest(http.MethodGet, url, nil)
for k, v := range head {
req.Header.Add(k, v)
}
if err != nil {
panic(err)
}
client := &http.Client{Timeout: 20 * time.Second}
resp, error := client.Do(req)
if error != nil {
panic(error)
}
defer resp.Body.Close()
code = resp.StatusCode
result, _ := ioutil.ReadAll(resp.Body)
content = string(result)
return
}
// 发送GET请求
// url:请求地址
// response:请求返回的内容
2021-09-26 10:35:02 +08:00
func OasisGet(url string) (response string) {
head := make(map[string]string)
t := make(chan string)
go func() {
str := Get(config.ServerInfo.ServerApi+"/token", nil)
t <- gjson.Get(str, "data").String()
}()
head["Authorization"] = <-t
return Get(url, head)
}