示例#1
0
func checkrequest(w http.ResponseWriter, r *http.Request) bool {
	if moeregexp.IsMatch(Mongo_path, r.URL.Path) {
		return true
	}
	panic("非法请求")
	return false
}
示例#2
0
func dbo_Mongo_DB(w http.ResponseWriter, r *http.Request) {
	var cmd = ""
	if moeregexp.IsMatch(Mongo_DB_func_path, r.URL.Path) {
		DB, cmd, err := Mongo_DB_parse(r.URL.Path)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		log.Print("匹配:" + DB + "\t" + cmd)
		if cmd == "show collections" {
			s, err := MgoSession()
			if err != nil {
				w.Write([]byte(err.Error()))
				return
			}
			Cs, _ := s.DB(DB).CollectionNames()
			w.Write([]byte(strings.Join(Cs, "\n")))
			return
		}
		w.Write([]byte("请求命令不支持"))
		return
	} else {
		w.Write([]byte("请求不匹配" + cmd))
	}
}
示例#3
0
func Mongo_DB_C_parse(urlpath string) (string, string, string, error) {
	/*

		/mongo/DB/C.count()     查询集合元素数量
		/mongo/DB/C.find()  查询所有文档
		/mongo/DB/C.findOne()   查询并返回一个对象。如果没有找到则返回 null
		/mongo/DB/C.find().count()  返回匹配该查询的对象总数
		/mongo/DB/C.insert()        向聚集中插入对象。不会检查该对象是否已经存在聚集中
		/mongo/DB/C.remove()    从聚集里删除匹配的对象
		/mongo/DB/C.save()  在聚集中保存对象,如果已经存在的话则替换它
		/mongo/DB/C.update()    在聚集中更新对象。update() 有许多参数

	*/
	if !moeregexp.IsMatch(Mongo_DB_C_func_path, urlpath) {
		err := "urlpath:\n" + urlpath + "不匹配正则" + Mongo_DB_C_func_path
		return "", "", "", errors.New(err)
	}
	DB_C := strings.Split(urlpath[7:strings.Index(urlpath, ".")], "/")
	if len(DB_C) != 2 {
		err := "奇怪的错误"
		return "", "", "", errors.New(err)
	}
	DB := DB_C[0]
	C := DB_C[1]
	funcs := urlpath[strings.Index(urlpath, ".")+1:]
	return DB, C, funcs, nil
}
示例#4
0
func router(w http.ResponseWriter, r *http.Request) {
	urlpath := r.URL.Path
	//路由匹配正则 "^/mongo.+"
	if moeregexp.IsMatch(httpmongo.Mongo_path, urlpath) {
		//调用handler_mongo,处理 /mongo路由下的所有请求
		httpmongo.Httphandler_mongo(*mongodb, w, r)
	}
}
示例#5
0
func Httphandler_mongo(DBaddr string, w http.ResponseWriter, r *http.Request) {
	MONGO_URL = DBaddr

	checkrequest(w, r)
	if moeregexp.IsMatch(Mongo_DB_C_func_path, r.URL.Path) {
		dbo_Mongo_DB_C(w, r)
	} else {
		if moeregexp.IsMatch(Mongo_DB_func_path, r.URL.Path) {
			dbo_Mongo_DB(w, r)
		} else {
			if moeregexp.IsMatch(Mongo_func_path, r.URL.Path) {
				dbo_Mongo(w, r)
			}
		}
	}

}
示例#6
0
func Mongo_parse(urlpath string) (string, error) {
	/*	support list

		/mongo.show dbs

	*/
	if !moeregexp.IsMatch(Mongo_func_path, urlpath) {
		err := "urlpath:\n" + urlpath + "不匹配正则" + Mongo_func_path
		return "", errors.New(err)
	}
	funcs := urlpath[7:]
	return funcs, nil
}
示例#7
0
func router(w http.ResponseWriter, r *http.Request) {
	urlpath := r.URL.Path

	if moeregexp.IsMatch(httpmongo.Mongo_path, urlpath) {
		httpmongo.Httphandler_mongo(getmongo(), w, r)
	} else {
		switch urlpath[0:] {
		case "/":
			handle_admin(w, r)
		default:
			w.Write([]byte("404"))
		}
	}
}
示例#8
0
func Mongo_DB_parse(urlpath string) (string, string, error) {
	/*	support list

		/mongo/DB.show collections

	*/
	if !moeregexp.IsMatch(Mongo_DB_func_path, urlpath) {
		err := "urlpath:\n" + urlpath + "不匹配正则" + Mongo_DB_func_path
		return "", "", errors.New(err)
	}
	DBname := urlpath[7:strings.Index(urlpath, ".")]
	funcs := urlpath[strings.Index(urlpath, ".")+1:]
	return DBname, funcs, nil
}
示例#9
0
func dbo_Mongo_DB_C(w http.ResponseWriter, r *http.Request) {
	var cmd = ""
	if moeregexp.IsMatch(Mongo_DB_C_func_path, r.URL.Path) {
		DB, C, cmd, err := Mongo_DB_C_parse(r.URL.Path)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		funcname := cmd[0:strings.Index(cmd, "(")]
		args := cmd[strings.Index(cmd, "(")+1 : len(cmd)-1]
		if strings.Contains(args, httpRequestBody) {
			body, err := ioutil.ReadAll(r.Body)
			if err != nil {
				w.Write([]byte("没有 接收到 post数据。url不应该有" + httpRequestBody))
				return
			}
			log.Print(string(body))
			args = strings.Replace(args, httpRequestBody, string(body), -1)
		}

		switch funcname {
		case "count":
			count(DB, C, w)
		case "find":
			find(DB, C, args, w)
		case "findcount":
			findcount(DB, C, args, w)
		case "findOne":
			findOne(DB, C, args, w)
		case "insert":
			insert(DB, C, args, w)
		case "insertmany":
			insertmany(DB, C, args, w)
		case "remove":
			remove(DB, C, args, w)
		case "save":
			save(DB, C, args, w)
		case "update":
			update(DB, C, args, w)
		default:
			w.Write([]byte("请求函数名未知"))
			return
		}
	} else {
		w.Write([]byte("请求不匹配" + cmd))
	}
}
示例#10
0
func dbo_Mongo(w http.ResponseWriter, r *http.Request) {
	var cmd = ""
	if moeregexp.IsMatch(Mongo_func_path, r.URL.Path) {
		cmd, err := Mongo_parse(r.URL.Path)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		if cmd == "show dbs" {
			s, err := MgoSession()
			if err != nil {
				w.Write([]byte(err.Error()))
				return
			}
			dbs, _ := s.DatabaseNames()
			w.Write([]byte(strings.Join(dbs, "\n")))
			return
		}
		w.Write([]byte("请求命令不支持"))
		return
	} else {
		w.Write([]byte("请求不匹配" + cmd))
	}
}