Пример #1
5
func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	oa := CheckAuthorized(w, r, false)
	if oa == nil {
		return
	}
	if r.Method != "POST" {
		http.Redirect(w, r, "/", http.StatusFound)
		return
	}
	m, _, err := r.FormFile("server")
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		return
	}
	defer m.Close()
	dst, err := os.Create(deployFileName)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	_, err = io.Copy(dst, m)
	dst.Close()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	finfo, _ := os.Stat(deployFileName)

	w.WriteHeader(200)
	w.Write([]byte("Uploaded: " + humanize.Bytes(uint64(finfo.Size()))))

}
Пример #2
0
func (h *RpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	oa := CheckAuthorized(w, r, false)
	if oa == nil {
		return
	}

	sURL := r.URL.Path[5:]
	//log.Println("rpc:", sURL)
	mParams := r.URL.Query()
	if sURL == "shutdown" {
		DoShutdown()
		return
	}

	if sURL == "backupget" {
		file := mParams.Get("file")
		w.Header().Set("Content-Disposition", "attachment; filename="+file)
		http.ServeFile(w, r, "backup/"+file)
		return
	}

	if sURL == "backupload" {
		m, h, err := r.FormFile("backup")
		if err != nil {
			http.Error(w, err.Error(), http.StatusNotFound)
			return
		}
		defer m.Close()
		sFile := "backup/" + h.Filename
		dst, err := os.Create(sFile)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		_, err = io.Copy(dst, m)
		dst.Close()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		finfo, _ := os.Stat(sFile)

		w.WriteHeader(200)
		w.Write([]byte("Uploaded: " + humanize.Bytes(uint64(finfo.Size()))))
		return
	}

	var err error
	var data interface{}
	defer func() {
		var result = map[string]interface{}{}
		if err == nil {
			result["result"] = true
			if data != nil {
				result["data"] = data
			}
		} else {
			log.Println("rpc error:", sURL, err)
			result["result"] = false
			result["error"] = err.Error()
		}
		msg, _ := json.Marshal(result)
		w.Write(msg)
	}()

	if sURL == "start" {
		err = ServerStart()
		return
	}
	if sURL == "stop" {
		err = ServerStop()
		return
	}
	if sURL == "flush" {
		err = ServerFlush()
		return
	}
	if sURL == "deploy" {
		err = DoDeployServer()
		return
	}
	if sURL == "dobackup" {
		err = DoBackup()
		return
	}
	if sURL == "backuplist" {
		data, err = GetBackupList()
		return
	}
	if sURL == "backupdel" {
		file := mParams.Get("file")
		if len(file) < 1 {
			err = errors.New("invalid file:" + file)
			return
		}
		sFile := "backup/" + file
		err = os.RemoveAll(sFile)
		return
	}
	if sURL == "backuprestore" {
		file := mParams.Get("file")
		if len(file) < 1 {
			err = errors.New("invalid file:" + file)
			return
		}
		sFile := "backup/" + file
		err = DoBackupRestore(sFile)
		return
	}
	if sURL == "activate" {
		//err = srv.DoActivateVersion(true, mParams.Get("version"))
		return
	}
	if sURL == "deactivate" {
		//err = srv.DoActivateVersion(false, mParams.Get("version"))
		return
	}
	if sURL == "autorestart" {
		val := mParams.Get("val")
		v := false
		if val == "true" {
			v = true
		}
		//log.Println("autorestart", val)
		SetAutoRestart(v)
		return
	}
	if sURL == "files" {
		//d := map[string]interface{}{}
		//d["files"], err = srv.GetVersionFiles(mParams.Get("version"))
		//data = d
		return
	}
	if sURL == "db" {
		data, err = db.DoAdminRpc(mParams)
		return
	}

	if sURL == "status" {
		d := map[string]interface{}{}
		if len(mParams.Get("log")) > 0 {
			t, _ := strconv.Atoi(mParams.Get("logt"))
			log, t := srv.GetLog(t)
			d["log"] = log
			d["logt"] = t
		}
		if len(mParams.Get("deploy")) > 0 {
			d["deploy"] = GetDeployInfo()
		}
		if len(mParams.Get("server")) > 0 {
			d["server"] = ServerStatus()
		}
		if len(mParams.Get("stats")) > 0 {
			d["stats"] = srv.GetStatsCur()
		}
		if len(mParams.Get("system")) > 0 {
			sys := map[string]interface{}{}

			var mem perf.Mem
			err = mem.Get()
			sys["mem_load"] = mem.Load
			sys["mem_total"] = mem.Total
			sys["mem_used"] = mem.Used
			sys["mem_pagetotal"] = mem.PageTotal
			sys["mem_pageused"] = mem.PageUsed
			sys["mem_cache"] = mem.Cache

			uptCtrl := time.Since(timeStart)
			uptSys, _ := perf.GetUptime()
			//log.Println("x:", uptSys, uptCtrl)
			sys["upt_system"] = uptSys / time.Millisecond
			sys["upt_ctrl"] = uptCtrl / time.Millisecond

			var cpu perf.Cpu
			err = cpu.Get()
			sys["cpu_loadtotal"] = cpu.LoadTotal
			sys["cpu_loadsys"] = cpu.LoadSys

			d["system"] = sys
		}

		data = d
		return
	}

	log.Println("invalid rpc:", sURL)
}