Exemplo n.º 1
0
Arquivo: db.go Projeto: pomkine/weasel
func (ps *ProxyStorage) SaveProxy(toSave *Proxy, update bool) (int, error) {
	logger.Tracef("Saving proxy to storage: %s", toSave)

	var query interface{}

	queryString := fmt.Sprintf(`[{"eq" : "%s", "in" : ["Ip"] },{"eq" : "%n", "in" : ["Port"] }]`, toSave.Ip, toSave.Port)
	json.Unmarshal([]byte(queryString), &query)

	queryResult := make(map[int]struct{}) // query result (document IDs) goes into map keys

	if err := db.EvalQuery(query, ps.proxyCol, &queryResult); err != nil {
		panic(err)
	}

	if len(queryResult) > 0 {
		logger.Tracef("Exists in database: %s", toSave)
		if update {
			logger.Tracef("Updating %s", toSave)
			for k, _ := range queryResult {
				//expecting single key
				return k, ps.proxyCol.Update(k, structs.Map(toSave))
			}
		}
	}
	return ps.proxyCol.Insert(structs.Map(toSave))
}
Exemplo n.º 2
0
func (m *Mongore) CreateToken(token account.Token) error {
	key := fmt.Sprintf("%s: %s", token.Type, token.User.Email)
	db.Cache.Set(key, nil, time.Duration(token.Expires)*time.Minute)
	db.AddHCache(key, token.Expires, structs.Map(token))

	db.Cache.Set(token.AccessToken, nil, time.Duration(token.Expires))
	db.AddHCache(token.AccessToken, token.Expires, structs.Map(token.User))
	return nil
}
Exemplo n.º 3
0
Arquivo: common.go Projeto: flazz/rack
func securityGroupRuleSingle(rule *osSecurityGroupRules.SecGroupRule) map[string]interface{} {
	m := structs.Map(rule)

	m["SecurityGroupID"] = m["SecGroupID"]

	return m
}
Exemplo n.º 4
0
// Function pluckElement is used to retrieve an array of just one field(destKeyName) present in original structure(plant).
// Input : 'plant' is the source from which an element needs to be plucked. An array of structure is expected.
//         'destKeyName' is the output element key name. This should not be an array as this will be used just to
//              form the output structure.
// Output : []interface{} - An array is returned. Type assertion can be used to derive an array of required type.
// See example for more details.
func pluckElement(plant interface{}, destKeyName string) ([]interface{}, error) {

	flag := 0
	// Read the value from interface{}
	srcExtract := reflect.ValueOf(plant)

	// The result map[string]interface{} to be returned
	var elementExtract []interface{}

	// Retrieve the source elements one by one and copy to dest
	for i := 0; i < srcExtract.Len(); i++ {
		indexVal := srcExtract.Index(i).Interface()
		indexValMap := structs.Map(indexVal)
		if value, present := indexValMap[destKeyName]; present {
			elementExtract = append(elementExtract, value)
			flag = 1
		}
	}

	// This is to make sure that at least one value got extracted from plant to branch
	if flag == 0 {
		err := errors.New("Source Destination Type Mismatch")
		return nil, err
	}
	return elementExtract, nil
}
Exemplo n.º 5
0
func getUsers(user string, properties []string) {
	api := setupAPI()

	// Get the user from the Graph.
	result, err := api.GetUser(user, properties)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v", err)
		return
	}

	// Convert the object to a map.
	m := structs.Map(result)

	// Get longest property name and sort all property names
	// alphabetically.
	var props []string
	longest := 0
	for k, _ := range m {
		props = append(props, k)
		if len(k) > longest {
			longest = len(k)
		}
	}
	sort.Strings(props)

	// Write out the properties.
	format := fmt.Sprintf("%%-%ds: %%v\n", longest)
	for _, prop := range props {
		fmt.Printf(format, prop, getValueAsString(m, prop))
	}
}
Exemplo n.º 6
0
func main() {
	templateHtml, err := mustache.ParseFile("./report.html")
	if err != nil {
		log.Fatal("Parse Html failed, ", err)
	}

	var tmpfile = tmpDir + "test_report.html"
	tmpHtmlFile, err := os.Create(tmpfile)
	if err != nil {
		log.Fatal("Create tmp file failed, ", err)
	}
	var data Data
	data.Name = "!!! XXXX !!!!"
	mapping := structs.Map(data)
	var str = templateHtml.Render(mapping)
	filelength, err := tmpHtmlFile.WriteString(str)
	fmt.Printf("wrote %d bytes\n", filelength)
	tmpHtmlFile.Sync()
	cmd := "wkhtmltopdf"
	args := []string{"--print-media-type", "--page-size", "A4", "-T", "0mm", "-B", "0mm", "-L", "0mm", "-R", "0mm", "--dpi", "600", "test_report.html", "test_report.pdf"}
	if err := exec.Command(cmd, args...).Run(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println("Successfully Generates the report of PDF format")

}
Exemplo n.º 7
0
//handle takes a job and appropriate sends it to a function that will run it
func (qs *QueueService) handle(job *QueueJob) {
	switch job.Job {
	case "updateDatasetFile":
		udfjd := UpdateDatasetFileJobData{}
		jobJson, err := json.Marshal(job.Data)
		if err != nil {
			log.Fatal("Error creating JSON: ", err)
		}

		err = json.Unmarshal(jobJson, &udfjd)
		if err != nil {
			log.Fatal("Error parsing JSON: ", err)
		}
		/*
			pds := NewPredictDatasetService(database.GetDatabase())
			pds.UpdateFile(udfjd)
		*/
		fs := files.NewFileService(database.GetDatabase())
		newTrans, err := fs.UpdateFile(udfjd.Extra.Subdom, udfjd.FileGUID, udfjd.TemplateGUID)
		//Alert luminous that its Done
		queue := NewQueueService("luminous")
		jobData := structs.Map(udfjd)
		jobData["NewTrans"] = newTrans
		qj := &QueueJob{
			Job:  `Spark451\Luminous\v2\Predict\Classes\Datasets@finishDatasetCreation`,
			Data: jobData,
		}
		queue.Push(qj)
	}
}
Exemplo n.º 8
0
// Write populates a buffer using a template with NGINX configuration
// and the servers and upstreams created by Ingress rules
func (t *Template) Write(cfg config.Configuration, ingressCfg ingress.Configuration) ([]byte, error) {
	conf := make(map[string]interface{})
	conf["backlogSize"] = sysctlSomaxconn()
	conf["upstreams"] = ingressCfg.Upstreams
	conf["servers"] = ingressCfg.Servers
	conf["tcpUpstreams"] = ingressCfg.TCPUpstreams
	conf["udpUpstreams"] = ingressCfg.UDPUpstreams
	conf["defResolver"] = cfg.Resolver
	conf["sslDHParam"] = cfg.SSLDHParam
	conf["customErrors"] = len(cfg.CustomHTTPErrors) > 0
	conf["cfg"] = fixKeyNames(structs.Map(cfg))

	if glog.V(3) {
		b, err := json.Marshal(conf)
		if err != nil {
			glog.Errorf("unexpected error: %v", err)
		}
		glog.Infof("NGINX configuration: %v", string(b))
	}

	buffer := new(bytes.Buffer)
	err := t.tmpl.Execute(buffer, conf)
	if err != nil {
		glog.V(3).Infof("%v", string(buffer.Bytes()))
	}

	return buffer.Bytes(), err
}
Exemplo n.º 9
0
func (p *LuaPlugin) prepContext(L *lua.State, req *http.Request, ctx *apiplexy.APIContext) {
	var clientIP string
	if req.Header.Get("X-Forwarded-For") != "" {
		clientIP = req.Header.Get("X-Forwarded-For")
	} else {
		clientIP, _, _ = net.SplitHostPort(req.RemoteAddr)
	}

	headers := make(map[string]interface{}, len(req.Header))
	for k, vs := range req.Header {
		headers[k] = strings.Join(vs, " ")
	}

	request := map[string]interface{}{
		"path":     req.URL.Path,
		"method":   req.Method,
		"ip":       clientIP,
		"referrer": req.Referer(),
		"browser":  req.UserAgent(),
		"headers":  headers,
	}
	pushMap(L, request, false)
	L.SetGlobal("request")

	pushMap(L, structs.Map(ctx), true)
	L.SetGlobal("context")
}
Exemplo n.º 10
0
// These are the endpoints required to do a login and verifying that tokens are
// alive
func LoginUser(c *gin.Context) {
	var user User
	var loginer login.Loginer
	var tokenStr string

	var token Token
	var tokener tokens.Tokener

	c.BindJSON(&user)

	loginer = user
	tokener = token

	SignatureStr, email := loginer.Login(user.Username, user.Password)
	tokenStr, err := tokener.GenerateToken(SignatureStr)

	if err != nil {
		c.JSON(404, gin.H{"error generating token": err})
	} else {
		data := structs.Map(user)
		err = boltdb.UpdateBucket(db, tokenStr, data)
		if err != nil {
			c.JSON(404, gin.H{"error updating bucket": err})
		} else {
			c.JSON(201, gin.H{"token": tokenStr, "email": email})
		}
	}
}
Exemplo n.º 11
0
func (m *StoreModifier) AndStoreMany(objs []interface{}) *StoreModifier {
	m.Request.Controls.Multiplicity = "multiple"

	s := reflect.ValueOf(objs)
	var interfaceList []map[string]interface{}
	interfaceList = make([]map[string]interface{}, s.Len())

	for i := 0; i < s.Len(); i++ {
		//newMap := structs.Map(s.Index(i).Interface())
		obj := s.Index(i).Interface()
		v := reflect.ValueOf(obj)
		k := v.Kind()
		fmt.Println("KIND : ", k)
		var newMap map[string]interface{}

		if k != reflect.Map {
			newMap = structs.Map(obj)
		} else {
			newMap = obj.(map[string]interface{})
		}

		interfaceList[i] = newMap
	}

	//for index, element := range objs {
	//	interfaceList[index] = structs.Map(element)
	//}

	m.Request.Body.Objects = interfaceList
	return m
}
Exemplo n.º 12
0
func (ngx *Manager) writeCfg(cfg nginxConfiguration, ingressCfg IngressConfig) (bool, error) {
	conf := make(map[string]interface{})
	conf["upstreams"] = ingressCfg.Upstreams
	conf["servers"] = ingressCfg.Servers
	conf["tcpUpstreams"] = ingressCfg.TCPUpstreams
	conf["udpUpstreams"] = ingressCfg.UDPUpstreams
	conf["defResolver"] = ngx.defResolver
	conf["sslDHParam"] = ngx.sslDHParam
	conf["cfg"] = fixKeyNames(structs.Map(cfg))

	buffer := new(bytes.Buffer)
	err := ngx.template.Execute(buffer, conf)
	if err != nil {
		glog.Infof("NGINX error: %v", err)
		return false, err
	}

	if glog.V(3) {
		b, err := json.Marshal(conf)
		if err != nil {
			fmt.Println("error:", err)
		}
		glog.Infof("NGINX configuration: %v", string(b))
	}

	changed, err := ngx.needsReload(buffer)
	if err != nil {
		return false, err
	}

	return changed, nil
}
Exemplo n.º 13
0
Arquivo: task.go Projeto: miku/dagger
// AutoPath returns a path based on the task name and parameters.
func AutoPathExt(outp Outputter, ext string) string {
	directory := strings.Replace(fmt.Sprintf("%T", outp), ".", "/", -1)
	m := structs.Map(outp)

	var keys []string
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	var parts []string
	for _, k := range keys {
		parts = append(parts, k)
		value := m[k]
		switch v := value.(type) {
		case string:
			parts = append(parts, v)
		case fmt.Stringer:
			parts = append(parts, v.String())
		default:
			parts = append(parts, fmt.Sprintf("%v", m[k]))
		}
	}
	fn := strings.Join(parts, "-")
	if len(fn) == 0 {
		fn = "output"
	}
	filename := fn + "." + ext
	return path.Join(directory, filename)
}
Exemplo n.º 14
0
func (app *Application) GetStats() map[string]interface{} {
	statsInterface := app.Storage.Get("stats")
	if statsInterface == nil {
		return nil
	}

	stats := statsInterface.(*models.Stats)
	payload := structs.Map(stats)

	// Fetch the other stats data from file.
	statsFromFileInterface := app.Storage.Get("statsFromFile")

	if statsFromFileInterface != nil {
		statsFromFile := statsFromFileInterface.(map[string]interface{})

		for key, value := range statsFromFile {
			trimmedKey := strings.Replace(key, "libmcrouter.mcrouter.5000.", "", -1)
			payload[trimmedKey] = value
		}
	}

	hostname, err := os.Hostname()
	if err == nil {
		payload["hostname"] = hostname
	}

	return payload
}
Exemplo n.º 15
0
// FindObjComments 获得某个对象的所有评论
// owner: 被评论对象属主
// TODO:分页暂不做
func (self CommentLogic) FindObjComments(ctx context.Context, objid, objtype int, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) {
	objLog := GetLogger(ctx)

	commentList := make([]*model.Comment, 0)
	err := MasterDB.Where("objid=? AND objtype=?", objid, objtype).Find(&commentList)
	if err != nil {
		objLog.Errorln("CommentLogic FindObjComments Error:", err)
		return
	}

	uids := slices.StructsIntSlice(commentList, "Uid")

	// 避免某些情况下最后回复人没在回复列表中
	uids = append(uids, owner, lastCommentUid)

	// 获得用户信息
	userMap := DefaultUser.FindUserInfos(ctx, uids)
	ownerUser = userMap[owner]
	if lastCommentUid != 0 {
		lastReplyUser = userMap[lastCommentUid]
	}
	comments = make([]map[string]interface{}, 0, len(commentList))
	for _, comment := range commentList {
		tmpMap := structs.Map(comment)
		tmpMap["content"] = template.HTML(self.decodeCmtContent(ctx, comment))
		tmpMap["user"] = userMap[comment.Uid]
		comments = append(comments, tmpMap)
	}
	return
}
Exemplo n.º 16
0
func (ngx *Manager) writeCfg(cfg config.Configuration, ingressCfg IngressConfig) (bool, error) {
	conf := make(map[string]interface{})
	conf["backlogSize"] = sysctlSomaxconn()
	conf["upstreams"] = ingressCfg.Upstreams
	conf["servers"] = ingressCfg.Servers
	conf["tcpUpstreams"] = ingressCfg.TCPUpstreams
	conf["udpUpstreams"] = ingressCfg.UDPUpstreams
	conf["defResolver"] = ngx.defResolver
	conf["sslDHParam"] = ngx.sslDHParam
	conf["customErrors"] = len(cfg.CustomHTTPErrors) > 0
	conf["cfg"] = fixKeyNames(structs.Map(cfg))

	if glog.V(3) {
		b, err := json.Marshal(conf)
		if err != nil {
			glog.Errorf("unexpected error:", err)
		}
		glog.Infof("NGINX configuration: %v", string(b))
	}

	buffer := new(bytes.Buffer)
	err := ngx.template.Execute(buffer, conf)
	if err != nil {
		glog.V(3).Infof("%v", string(buffer.Bytes()))
		return false, err
	}

	changed, err := ngx.needsReload(buffer)
	if err != nil {
		return false, err
	}

	return changed, nil
}
Exemplo n.º 17
0
func (c *Config) Map() map[string]interface{} {
	if c.AwsAccessKey != "" {
		s3util.DefaultConfig.AccessKey = c.AwsAccessKey
		s3util.DefaultConfig.SecretKey = c.AwsSecretKey
	}
	return structs.Map(c)
}
func get_source_by_id(res rest.ResponseWriter, conn redis.Conn, contentType string, doc_id string) (map[string]interface{}, bool) {
	hash_key := "index:" + contentType + ":source:" + doc_id
	hash_value := Source{"", "", "", 0}

	exists, err := redis.Bool(conn.Do("EXISTS", hash_key))
	if err != nil {
		rest.Error(res, "Unexpected error. "+err.Error(), 400)
		return nil, true
	}
	if exists == false {
		return nil, false
	}

	// get the document indexed
	values, err := conn.Do("HGETALL", hash_key)
	if err == nil {
		err = redis.ScanStruct(values.([]interface{}), &hash_value)
	}
	if err != nil {
		rest.Error(res, "Unexpected error. "+err.Error(), 400)
		return nil, true
	}

	source := structs.Map(hash_value)

	source["Id"] = doc_id
	source["Payload"] = StringToJson(source["Payload"].(string))

	return source, false
}
Exemplo n.º 19
0
Arquivo: posts.go Projeto: get3w/get3w
func (parser *Parser) getPost(file *get3w.File) *get3w.Post {
	post := &get3w.Post{}

	front, content := parser.read(post, file.Path)

	ext := getExt(file.Path)
	post.Content = getStringByExt(ext, content)
	post.ID = removeExt(file.Name)
	post.Path = file.Path
	if post.Title == "" {
		post.Title = post.ID
	}
	post.URL = "/posts/" + post.ID + ".html"

	vars := make(map[string]interface{})
	if len(front) > 0 {
		yaml.Unmarshal(front, vars)
	}
	post.AllParameters = structs.Map(post)
	for key, val := range vars {
		if _, ok := post.AllParameters[key]; !ok {
			post.AllParameters[key] = val
		}
	}

	return post
}
Exemplo n.º 20
0
func (s *service) call(server *Server, sending *sync.Mutex, mtype *methodType, req *Request, argv, replyv []reflect.Value, codec ServerCodec) {
	mtype.Lock()
	mtype.numCalls++
	mtype.Unlock()
	function := mtype.method.Func

	// Invoke the method, providing a new value for the reply.
	args := make([]reflect.Value, 0)
	args = append(args, s.rcvr)
	args = append(args, argv...)
	returnValues := function.Call(args)

	// look if their an error in the return types and return it if its not nil
	var isStruct bool
	var retStructIdx int
	for i, retVal := range returnValues {
		if e, ok := retVal.Interface().(error); ok {
			server.sendResponse(sending, req, nil, codec, e.Error())
			return
		}
		if retVal.Kind() == reflect.Struct {
			isStruct = true
			retStructIdx = i
		}
	}

	var res interface{}
	if isStruct {
		res = structs.Map(returnValues[retStructIdx].Interface())
	} else {
		res = returnValues[0].Interface()
	}

	server.sendResponse(sending, req, res, codec, "")
}
Exemplo n.º 21
0
Arquivo: list.go Projeto: flazz/rack
func (command *commandList) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsList).opts
	opts.Full = true
	allPages := resource.Params.(*paramsList).allPages
	pager := containers.List(command.Ctx.ServiceClient, opts)
	if allPages {
		pages, err := pager.AllPages()
		if err != nil {
			resource.Err = err
			return
		}
		info, err := containers.ExtractInfo(pages)
		if err != nil {
			resource.Err = err
			return
		}
		result := make([]map[string]interface{}, len(info))
		for j, container := range info {
			result[j] = structs.Map(&container)
		}
		resource.Result = result
	} else {
		limit := opts.Limit
		err := pager.EachPage(func(page pagination.Page) (bool, error) {
			info, err := containers.ExtractInfo(page)
			if err != nil {
				return false, err
			}
			result := make([]map[string]interface{}, len(info))
			for j, container := range info {
				result[j] = structs.Map(&container)
			}
			resource.Result = result
			if len(info) >= limit {
				return false, nil
			}
			limit -= len(info)
			command.Ctx.WaitGroup.Add(1)
			command.Ctx.Results <- resource
			return true, nil
		})
		if err != nil {
			resource.Err = err
			return
		}
	}
}
Exemplo n.º 22
0
func (m *DeleteModifier) AndDeleteObject(obj interface{}) *DeleteModifier {
	m.Request.Controls.Operation = "delete"
	m.Request.Controls.Multiplicity = "single"
	bodyMap := structs.Map(obj)
	key := bodyMap[m.Request.Body.Parameters.KeyProperty].(string)
	m.Request.Controls.Id = key
	return m
}
Exemplo n.º 23
0
Arquivo: upload.go Projeto: flazz/rack
func (command *commandUpload) Execute(resource *handler.Resource) {
	opts := resource.Params.(*paramsUpload).opts
	keypair, err := keypairs.Create(command.Ctx.ServiceClient, opts).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = structs.Map(keypair)
}
Exemplo n.º 24
0
Arquivo: get.go Projeto: flazz/rack
func (command *commandGet) Execute(resource *handler.Resource) {
	flavorID := resource.Params.(*paramsGet).flavor
	flavor, err := flavors.Get(command.Ctx.ServiceClient, flavorID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = structs.Map(flavor)
}
Exemplo n.º 25
0
Arquivo: get.go Projeto: flazz/rack
func (command *commandGet) Execute(resource *handler.Resource) {
	imageID := resource.Params.(*paramsGet).image
	image, err := images.Get(command.Ctx.ServiceClient, imageID).Extract()
	if err != nil {
		resource.Err = err
		return
	}
	resource.Result = structs.Map(image)
}
func (b *RedisBinder) Do(body io.ReadCloser) {
	b.Binder.Do(body)
	credentials := &RedisConf
	m := structs.Map(credentials)
	b.Binder.Bound.Credentials = make(map[string]interface{}, len(m))
	for k, v := range m {
		b.Binder.Bound.Credentials[strings.ToLower(k)] = v
	}
}
Exemplo n.º 27
0
Arquivo: common.go Projeto: flazz/rack
func volumeSingle(volume *volumes.Volume) map[string]interface{} {
	m := structs.Map(volume)
	for k, v := range m {
		if v == nil {
			m[k] = ""
		}
	}
	return m
}
Exemplo n.º 28
0
//Get - get a get handler for org management
func (s *orgController) Get() interface{} {
	var handler OrgGetHandler = func(params martini.Params, log *log.Logger, r render.Render, tokens oauth2.Tokens) {
		username := params[UserParam]
		org := NewOrg(username, log, tokens, s.store(), s.authClient)
		result, err := org.Show()
		genericResponseFormatter(r, "", structs.Map(result), err)
	}
	return handler
}
Exemplo n.º 29
0
Arquivo: common.go Projeto: flazz/rack
func snapshotSingle(snapshot *osSnapshots.Snapshot) map[string]interface{} {
	m := structs.Map(snapshot)
	if attachments := m["Attachments"].([]string); len(attachments) > 0 {
		m["Attachments"] = strings.Join(attachments, ",")
	} else {
		m["Attachments"] = ""
	}
	return m
}
Exemplo n.º 30
0
func (e *Event) String() string {
	m := structs.Map(e)
	if e.Type == "push" {
		return pushTmpl.Render(m)
	}
	if e.Type == "pullrequest" {
		return pqTmpl.Render(m)
	}
	return ""
}