本文整理汇总了Golang中github.com/AlexanderChen1989/go-json-rest/rest.ResponseWriter.Write方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseWriter.Write方法的具体用法?Golang ResponseWriter.Write怎么用?Golang ResponseWriter.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/AlexanderChen1989/go-json-rest/rest.ResponseWriter
的用法示例。
在下文中一共展示了ResponseWriter.Write方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetSystemBySn
func GetSystemBySn(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
var info struct {
Sn string
Type string
}
info.Sn = r.FormValue("sn")
info.Type = r.FormValue("type")
info.Sn = strings.TrimSpace(info.Sn)
info.Type = strings.TrimSpace(info.Type)
if info.Type == "" {
info.Type = "raw"
}
repo, ok := middleware.RepoFromContext(ctx)
if !ok {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误", "Content": ""})
}
return
}
if info.Sn == "" {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "SN参数不能为空"})
}
return
}
mod, err := repo.GetSystemBySn(info.Sn)
if err != nil {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error(), "Content": ""})
}
return
}
if info.Type == "raw" {
w.Header().Add("Content-type", "text/html; charset=utf-8")
w.Write([]byte(mod.Content))
} else {
w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "成功获取system信息", "Content": mod})
}
}
示例2: ExportScanDeviceList
func ExportScanDeviceList(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
repo, ok := middleware.RepoFromContext(ctx)
if !ok {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
return
}
var info struct {
Keyword string
Company string
Product string
ModelName string
CpuRule string
Cpu string
MemoryRule string
Memory string
DiskRule string
Disk string
UserID string
}
info.Keyword = r.FormValue("Keyword")
info.UserID = r.FormValue("UserID")
info.Company = r.FormValue("Company")
info.Product = r.FormValue("Product")
info.ModelName = r.FormValue("ModelName")
info.CpuRule = r.FormValue("CpuRule")
info.Cpu = r.FormValue("Cpu")
info.MemoryRule = r.FormValue("MemoryRule")
info.Memory = r.FormValue("Memory")
info.DiskRule = r.FormValue("DiskRule")
info.Disk = r.FormValue("Disk")
info.UserID = strings.TrimSpace(info.UserID)
info.Keyword = strings.TrimSpace(info.Keyword)
info.Company = strings.TrimSpace(info.Company)
info.Product = strings.TrimSpace(info.Product)
info.ModelName = strings.TrimSpace(info.ModelName)
info.CpuRule = strings.TrimSpace(info.CpuRule)
info.Cpu = strings.TrimSpace(info.Cpu)
info.MemoryRule = strings.TrimSpace(info.MemoryRule)
info.Memory = strings.TrimSpace(info.Memory)
info.DiskRule = strings.TrimSpace(info.DiskRule)
info.Disk = strings.TrimSpace(info.Disk)
var where string
where = " and t1.is_show_in_scan_list = 'Yes' "
if info.UserID != "" {
var userID int
userID, _ = strconv.Atoi(info.UserID)
where += " and t1.user_id = '" + fmt.Sprintf("%d", userID) + "'"
}
idsParam := r.FormValue("ids")
if idsParam != "" {
ids := strings.Split(idsParam, ",")
if len(ids) > 0 {
where += " and t1.id in (" + strings.Join(ids, ",") + ")"
}
}
if info.Company != "" {
where += " and t1.company = '" + info.Company + "'"
}
if info.Product != "" {
where += " and t1.product = '" + info.Product + "'"
}
if info.ModelName != "" {
where += " and t1.model_name = '" + info.ModelName + "'"
}
if info.CpuRule != "" && info.Cpu != "" {
where += " and t1.cpu_sum " + info.CpuRule + info.Cpu
}
if info.MemoryRule != "" && info.Memory != "" {
where += " and t1.memory_sum " + info.MemoryRule + info.Memory
}
if info.DiskRule != "" && info.Disk != "" {
where += " and t1.disk_sum " + info.DiskRule + info.Disk
}
if info.Keyword != "" {
where += " and ( "
info.Keyword = strings.Replace(info.Keyword, "\n", ",", -1)
info.Keyword = strings.Replace(info.Keyword, ";", ",", -1)
list := strings.Split(info.Keyword, ",")
for k, v := range list {
var str string
v = strings.TrimSpace(v)
if k == 0 {
str = ""
} else {
str = " or "
}
where += str + " t1.sn = '" + v + "' or t1.ip = '" + v + "' or t1.company = '" + v + "' or t1.product = '" + v + "' or t1.model_name = '" + v + "'"
}
isValidate, _ := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", info.Keyword)
if isValidate {
where += " or t1.nic like '%%\"" + info.Keyword + "\"%%' "
}
where += " ) "
//.........这里部分代码省略.........
示例3: GetNetworkBySn
func GetNetworkBySn(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
var info struct {
Sn string
Type string
}
info.Sn = r.FormValue("sn")
info.Type = r.FormValue("type")
info.Sn = strings.TrimSpace(info.Sn)
info.Type = strings.TrimSpace(info.Type)
if info.Type == "" {
info.Type = "raw"
}
repo, ok := middleware.RepoFromContext(ctx)
if !ok {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误", "Content": ""})
}
return
}
if info.Sn == "" {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "SN参数不能为空"})
}
return
}
deviceId, err := repo.GetDeviceIdBySn(info.Sn)
if err != nil {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error(), "Content": ""})
}
return
}
device, err := repo.GetDeviceById(deviceId)
if err != nil {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error(), "Content": ""})
}
return
}
mod, err := repo.GetNetworkBySn(info.Sn)
if err != nil {
if info.Type == "raw" {
w.Write([]byte(""))
} else {
w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error(), "Content": ""})
}
return
}
result := make(map[string]interface{})
result["Hostname"] = device.Hostname
result["Ip"] = device.Ip
result["Netmask"] = mod.Netmask
result["Gateway"] = mod.Gateway
result["Vlan"] = mod.Vlan
result["Trunk"] = mod.Trunk
result["Bonding"] = mod.Bonding
if info.Type == "raw" {
w.Header().Add("Content-type", "text/html; charset=utf-8")
var str string
if device.Hostname != "" {
str += "HOSTNAME=\"" + device.Hostname + "\""
}
if device.Ip != "" {
str += "\nIPADDR=\"" + device.Ip + "\""
}
if mod.Netmask != "" {
str += "\nNETMASK=\"" + mod.Netmask + "\""
}
if mod.Gateway != "" {
str += "\nGATEWAY=\"" + mod.Gateway + "\""
}
if mod.Vlan != "" {
str += "\nVLAN=\"" + mod.Vlan + "\""
}
if mod.Trunk != "" {
str += "\nTrunk=\"" + mod.Trunk + "\""
}
if mod.Bonding != "" {
str += "\nBonding=\"" + mod.Bonding + "\""
}
w.Write([]byte(str))
} else {
w.Header().Add("Content-type", "application/json; charset=utf-8")
w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "成功获取network信息", "Content": result})
//.........这里部分代码省略.........
示例4: UploadDevice
func UploadDevice(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
w.Header().Add("Content-type", "text/html; charset=utf-8")
r.ParseForm()
file, handle, err := r.FormFile("file")
if err != nil {
w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
return
}
cd, err := iconv.Open("UTF-8", "GBK")
if err != nil {
w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
return
}
defer cd.Close()
dir := "/tmp/cloudboot-server/"
if !util.FileExist(dir) {
err := os.MkdirAll(dir, 0777)
if err != nil {
w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
return
}
}
list := strings.Split(handle.Filename, ".")
fix := list[len(list)-1]
h := md5.New()
h.Write([]byte(fmt.Sprintf("%s", time.Now().UnixNano()) + handle.Filename))
cipherStr := h.Sum(nil)
md5 := fmt.Sprintf("%s", hex.EncodeToString(cipherStr))
filename := "osinstall-upload-" + md5 + "." + fix
result := make(map[string]interface{})
result["result"] = filename
if util.FileExist(dir + filename) {
os.Remove(dir + filename)
}
f, err := os.OpenFile(dir+filename, os.O_WRONLY|os.O_CREATE, 0666)
io.Copy(f, file)
if err != nil {
w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
return
}
defer f.Close()
defer file.Close()
data := map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": result}
json, err := json.Marshal(data)
if err != nil {
w.Write([]byte("{\"Message\":\"" + err.Error() + "\",\"Status\":\"error\"}"))
return
}
w.Write([]byte(json))
return
}