Example #1
0
// Create a new entity to persist into the database - start HTML extraction.
func createNewEntity(url string, entity *Entity) error {
	err := NewEntityDir()
	if err != nil {
		log.Println("Error creating entity dir: ", err)
	}

	go ExtractData(entity, url)

	id, err := uuid.NewV4()
	if err != nil {
		log.Println("Error creating UUID: ", err)
	}

	// Set new entities' initial data from the request.
	entity.Status = ParsingStatus
	entity.Url = url
	entity.UUID = id.String()
	entity.CreatedAt = time.Now()

	// Persist new entity into the database.
	err = Create(entity)
	if err != nil {
		return err
	}

	err = Find(entity, bson.M{"uuid": entity.UUID}).One(&entity)
	if err != nil {
		return err
	}

	return err
}
Example #2
0
// findGame finds a game in the list of open games. If one doesn't exist, creates a new gameid
// returns a new Game and if it's a new game or not.
func findGame(ctx context.Context, con redis.Conn) (*Game, bool, error) {
	lc := "FindGame"

	// do we have an open game?
	gameID, err := redis.String(con.Do("RPOP", openGames))

	// ignore nil errors, since that is expected
	if err != nil && err != redis.ErrNil {
		logger.Error(ctx, lc, "Error finding open game: %v", err)
		return new(Game), false, err
	}

	// is this a brand new game?
	isNew := (gameID == "")

	if isNew {
		logger.Info(ctx, lc, "Could not find open game, creating one... ")
		u, err := uuid.NewV4()
		if err != nil {
			return nil, false, err
		}
		gameID = u.String()
	}

	return NewGame(gameID), isNew, nil
}
Example #3
0
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer context.Clear(r)

	u4, err := gouuid.NewV4()
	if err != nil {
		log.Panic(err)
	}
	uuidToken := u4.String()
	context.Set(r, uuidKey, uuidToken)

	defer bugsnag.OnCapturePanic(r, func(event bugsnag.EventDescriber) {
		event.WithMetaData("request", "uuid", uuidToken)
	})

	log.Println(uuidToken, "Start", r.Method, r.URL, "for", parseRemoteAddr(r))

	w.Header().Set("Cache-Control", "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0")

	if origin, ok := isWhiteListedCorsOrigin(r); ok {
		w.Header().Set("Access-Control-Allow-Origin", origin)
		w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Authorization, Accept, Accept-Encoding, Accept-Language, Access-Control-Request-Headers, Access-Control-Request-Method, Connection, Host, Origin, User-Agent")
		w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, PUT, POST, DELETE")
		w.Header().Set("Access-Control-Allow-Credentials", "true")
		w.Header().Set("Access-Control-Max-Age", "1728000")
	}

	if r.Method == "OPTIONS" {
		w.Header().Set("Content-Length", "0")
		w.WriteHeader(http.StatusOK)
		return
	}
	router.Routes.ServeHTTP(w, r)
}
Example #4
0
func (t *tokengenerator) GenerateToken() (string, error) {
	id, err := uuid.NewV4()
	if err != nil {
		return "", err
	}
	return id.String(), nil
}
func (fs *FakeFileSystem) TempDir(prefix string) (string, error) {
	fs.filesLock.Lock()
	defer fs.filesLock.Unlock()

	if fs.TempDirError != nil {
		return "", fs.TempDirError
	}

	var path string
	if len(fs.TempDirDir) > 0 {
		path = fs.TempDirDir
	} else {
		uuid, err := gouuid.NewV4()
		if err != nil {
			return "", err
		}

		path = uuid.String()
	}

	// Make sure to record a reference for FileExist, etc. to work
	stats := fs.getOrCreateFile(path)
	stats.FileType = FakeFileTypeDir

	return path, nil
}
func GenerateUUID() (uuidStr string) {
	uuid, err := gouuid.NewV4()
	if err != nil {
		return
	}
	return uuid.String()
}
Example #7
0
func (t *JwtToken) jwtId() (string, error) {
	if jti, err := uuid.NewV4(); err != nil {
		return "", err
	} else {
		return jti.String(), nil
	}
}
Example #8
0
func nextUUID() (string, error) {
	u, err := gouuid.NewV4()
	if err != nil {
		return "", err
	}
	return u.String(), nil
}
Example #9
0
func newUuid() string {
	id, err := uuid.NewV4()
	if err != nil {
		panic(err)
	}
	return id.String()
}
Example #10
0
func createContainerWithoutConsume() garden.Container {
	handle, err := uuid.NewV4()
	Expect(err).ShouldNot(HaveOccurred())
	container, err := client.Create(garden.ContainerSpec{Handle: handle.String()})
	Expect(err).ShouldNot(HaveOccurred())
	return container
}
Example #11
0
File: vault.go Project: gaku/1pass
func newItemId() string {
	id, err := uuid.NewV4()
	if err != nil {
		panic("Failed to generate UUID")
	}
	return strings.ToUpper(hex.EncodeToString(id[:]))
}
Example #12
0
func GenerateUuid() string {
	Uuid, err := uuid.NewV4()
	if err != nil {
		log.Fatal("Failed to generate UUID")
		return ""
	}
	return Uuid.String()
}
func NewEntity() (*Entity, error) {
	var id *uuid.UUID
	var err error
	if id, err = uuid.NewV4(); err != nil {
		return nil, err
	}
	return &Entity{UUID: id}, nil
}
Example #14
0
func generateRunId(user models.User) string {
	u, err := gouuid.NewV4()
	if err != nil {
		stats.ERROR.Println("Cannot generate UUID")
		return "CannotGenerateRunId"
	}
	return fmt.Sprint(user.UserName, "-", user.Id, "-", u.String())
}
func RandomName() string {
	guid, err := uuid.NewV4()
	if err != nil {
		panic(err)
	}

	return guid.String()
}
Example #16
0
func GenerateGuid() string {
	guid, err := uuid.NewV4()
	if err != nil {
		panic("Failed to generate a GUID.  Craziness.")
	}

	return guid.String()
}
Example #17
0
func (this *DocumentEntity) Init() {
	u4, _ := uuid.NewV4()
	this.id = u4.String()
	this.timestamp = time.Now().UnixNano()
	this.sentences = list.New()
	this.Unknown = make(map[string]int64)
	this.Status = ""
}
Example #18
0
func GenerateToken() (string, error) {
	u, err := uuid.NewV4()
	if err != nil {
		return "", err
	}

	t := strings.Replace(u.String(), "-", "", -1)
	return t, nil
}
Example #19
0
/*
NewJob creates a new job from the config as well as a job spec.  After creating
the job, calling job.Process() will actually perform the work.
*/
func NewJob(cfg *Config, spec *Spec, req *http.Request) *Job {
	var idUUID *gouuid.UUID
	var err error
	idUUID, err = gouuid.NewV4()
	if TestMode {
		idUUID, err = gouuid.NewV5(gouuid.NamespaceURL, []byte("0"))
	}

	if err != nil {
		cfg.Logger.WithField("error", err).Error("error creating uuid")
	}
	id := idUUID.String()

	bobfile := spec.Bobfile
	if bobfile == "" {
		bobfile = defaultBobfile
	}

	ret := &Job{
		Bobfile:        bobfile,
		ID:             id,
		Account:        spec.RepoOwner,
		GitHubAPIToken: spec.GitHubAPIToken,
		Ref:            spec.GitRef,
		Repo:           spec.RepoName,
		Workdir:        cfg.Workdir,
		InfoRoute:      "/jobs/" + id,
		LogRoute:       "/jobs/" + id + "/tail?n=" + defaultTail,
		logDir:         cfg.Workdir + "/" + id,
		Status:         "created",
		Created:        time.Now(),
	}
	ret.addHostToRoutes(req)

	out, file, err := newMultiWriter(ret.logDir)
	if err != nil {
		cfg.Logger.WithField("error", err).Error("error creating log dir")
		id = ""
	}

	ret.logFile = file
	ret.Logger = &logrus.Logger{
		Formatter: cfg.Logger.Formatter,
		Level:     cfg.Logger.Level,
		Out:       out,
	}

	if ret.GitHubAPIToken == "" {
		ret.GitHubAPIToken = cfg.GitHubAPIToken
	}

	if id != "" {
		jobs[id] = ret
	}

	return ret
}
Example #20
0
func NewUuid() (uuid string, err error) {
	uuidPtr, err := gouuid.NewV4()
	if err != nil {
		err = errors.New("Could not generate UUID")
	} else {
		uuid = uuidPtr.String()
	}
	return
}
Example #21
0
func (t *rabbitTransport) Send(req message.Request, _timeout time.Duration) (message.Response, error) {
	id := req.Id()
	if id == "" {
		_uuid, err := uuid.NewV4()
		if err != nil {
			log.Errorf("[Typhon:RabbitTransport] Failed to generate request uuid: %v", err)
			return nil, err
		}
		req.SetId(_uuid.String())
	}

	rspQueue := req.Id()
	defer func() {
		t.inflightReqsM.Lock()
		delete(t.inflightReqs, rspQueue)
		t.inflightReqsM.Unlock()
	}()
	rspChan := make(chan message.Response, 1)
	t.inflightReqsM.Lock()
	t.inflightReqs[rspQueue] = rspChan
	t.inflightReqsM.Unlock()

	timeout := time.NewTimer(_timeout)
	defer timeout.Stop()

	headers := req.Headers()
	headers["Content-Encoding"] = "request"
	headers["Service"] = req.Service()
	headers["Endpoint"] = req.Endpoint()

	select {
	case <-t.Ready():
	case <-timeout.C:
		log.Warnf("[Typhon:RabbitTransport] Timed out after %s waiting for ready", _timeout.String())
		return nil, transport.ErrTimeout
	}

	if err := t.connection().Publish(Exchange, req.Service(), amqp.Publishing{
		CorrelationId: req.Id(),
		Timestamp:     time.Now().UTC(),
		Body:          req.Payload(),
		ReplyTo:       t.replyQueue,
		Headers:       headersToTable(headers),
	}); err != nil {
		log.Errorf("[Typhon:RabbitTransport] Failed to publish: %v", err)
		return nil, err
	}

	select {
	case rsp := <-rspChan:
		return rsp, nil
	case <-timeout.C:
		log.Warnf("[Typhon:RabbitTransport] Timed out after %s waiting for response to %s", _timeout.String(),
			req.Id())
		return nil, transport.ErrTimeout
	}
}
Example #22
0
func (up *UUIDProvider) Next() (string, error) {
	uuid, err := uuid.NewV4()

	if err != nil {
		return "", err
	}

	return uuid.String(), nil
}
Example #23
0
// NewMsg creates a new ComposedMsg to respond to a parent message. This includes setting
// up its headers.
func NewMsg(msg_type string, parent ComposedMsg) (msg ComposedMsg) {
	msg.Parent_header = parent.Header
	msg.Header.Session = parent.Header.Session
	msg.Header.Username = parent.Header.Username
	msg.Header.Msg_type = msg_type
	u, _ := uuid.NewV4()
	msg.Header.Msg_id = u.String()
	return
}
func newHeartbeatRequest() *control.ControlMessage {
	id, _ := uuid.NewV4()

	return &control.ControlMessage{
		Origin:      proto.String("MET"),
		Identifier:  factories.NewControlUUID(id),
		Timestamp:   proto.Int64(time.Now().UnixNano()),
		ControlType: control.ControlMessage_HeartbeatRequest.Enum(),
	}
}
func (gen uuidV4Generator) Generate() (uuidStr string, err error) {
	uuid, err := gouuid.NewV4()
	if err != nil {
		err = bosherr.WrapError(err, "Generating V4 uuid")
		return
	}

	uuidStr = uuid.String()
	return
}
Example #26
0
func NewRequest(importer, function string, call_type CallType, params []byte) (r *Request) {
	r = new(Request)
	id, _ := uuid.NewV4()
	r.UUID = id.String()
	r.Importer = importer
	r.Function = function
	r.CallType = call_type
	r.params = params
	return
}
Example #27
0
func NewRequest(importer, function string, call_type CallType, params interface{}) (r *Request) {
	r = new(Request)
	id, _ := uuid.NewV4()
	r.UUID = id.String()
	r.Importer = importer
	r.Function = function
	r.CallType = call_type
	r.params, _ = msgpack.Marshal(params)
	return
}
Example #28
0
func createContainer(containerSpec garden.ContainerSpec) garden.Container {
	handle, err := uuid.NewV4()
	Expect(err).ShouldNot(HaveOccurred())
	containerSpec.Handle = handle.String()
	container, err := client.Create(containerSpec)
	Expect(err).ShouldNot(HaveOccurred())
	err = StreamIn(container)
	Expect(err).ShouldNot(HaveOccurred())
	return container
}
Example #29
0
// MakeSystemUserId generates a fresh UUID to use as the system user id and returns it
func (rw *UserDbRow) MakeSystemUserId() (string, error) {
	u4, err := uuid.NewV4()
	if err != nil {
		glog.Errorf("Failed to generate a uuid as SysUserId: %s", err)
		return "", err
	}

	rw.SysUserId = u4.String()
	return rw.SysUserId, nil
}
Example #30
0
func NewServer(host string) *Server {
	id, err := uuid.NewV4()
	if err != nil {
		panic(err)
	}

	return &Server{
		redisClient: NewRedisClient(host),
		uuid:        id.String(),
	}
}