func (pg *PostgresDBManager) SaveFeed(feed *models.Feed) bool { var feedStmt, itemStmt *sql.Stmt var err error var randID uuid.UUID // save feed to db feedStmt, err = pg.db.Prepare("INSERT INTO feeds VALUES($1, $2, $3, $4, $5, $6, $7)") if err != nil { log.Fatal(err) return false } // add id randID, err = uuid.V4() if err != nil { log.Fatal(err) return false } feed.ID = randID.String() _, err = feedStmt.Exec(feed.ID, feed.Title, feed.Description, feed.URL, feed.FeedURL, feed.LastPubDate, feed.ImageURL) if err != nil { log.Fatal(err) return false } // save feed Items itemStmt, err = pg.db.Prepare("INSERT INTO feed_items VALUES($1, $2, $3, $4, $5, $6, $7, $8)") if err != nil { log.Fatal(err) return false } for _, item := range feed.Items { randID, err = uuid.V4() if err != nil { log.Fatal(err) return false } item.ID = randID.String() _, err = itemStmt.Exec(item.ID, item.Title, item.Description, item.URL, item.MediaURL, item.PubDate, item.ImageURL, feed.ID) if err != nil { log.Fatal(err) return false } } return true }
func (p *Player) AddDebt(d Debt) error { newDebt := new(Debt) if err := mergo.MergeWithOverwrite(newDebt, d); err != nil { return errors.New(err.Error() + " - Could not set Debt data") } newDebt.UUID, _ = uuid.V4() if d.Created.IsZero() { newDebt.Created = time.Now() } else { newDebt.Created = d.Created } if !d.Settled.IsZero() { newDebt.Settled = d.Settled } newDebt.Debitor = p.UUID p.Debts = append(p.Debts, *newDebt) err := storage.Store(p) if err != nil { return errors.New("Could not add debt") } eventqueue.Publish(utils.CKPTEvent{ Type: utils.PLAYER_EVENT, RestrictedTo: []uuid.UUID{p.UUID}, Subject: "Gjeld registrert", Message: "Det er registrert et nytt gjeldskrav mot deg på ckpt.no!"}) return nil }
func createEremeticTask(request types.Request) (types.EremeticTask, error) { randId, err := uuid.V4() if err != nil { return types.EremeticTask{}, err } taskId := createID(randId.String()) status := []types.Status{ types.Status{ Status: mesos.TaskState_TASK_STAGING.String(), Time: time.Now().Unix(), }, } task := types.EremeticTask{ ID: taskId, TaskCPUs: request.TaskCPUs, TaskMem: request.TaskMem, Name: request.Name, Status: status, Command: request.Command, User: "******", Environment: request.Environment, Image: request.DockerImage, Volumes: request.Volumes, CallbackURI: request.CallbackURI, URIs: request.URIs, } return task, nil }
func createUUIDs(number int) []uuid.UUID { var uuids []uuid.UUID for number > 0 { uuid, _ := uuid.V4() uuids = append(uuids, uuid) number-- } return uuids }
func generateFakeResponse(streamID uuid.UUID) []model.StreamItem { //fake data uuid1, _ := uuid.V4() uuid2, _ := uuid.V4() return []model.StreamItem{ { ID: uuid1.String(), Timestamp: time.Now(), Type: model.TypePost, StreamID: streamID.String(), }, { ID: uuid2.String(), Timestamp: time.Now(), Type: model.TypeRepost, StreamID: streamID.String(), }, } }
// Create a Catering func NewCatering(tournament uuid.UUID, ci Info) (*Catering, error) { c := new(Catering) c.UUID, _ = uuid.V4() c.Tournament = tournament if err := mergo.MergeWithOverwrite(&c.Info, ci); err != nil { return nil, errors.New(err.Error() + " - Could not set initial catering info") } if err := storage.Store(c); err != nil { return nil, errors.New(err.Error() + " - Could not write catering to storage") } return c, nil }
func createEremeticTask(request types.Request) (types.EremeticTask, error) { randId, err := uuid.V4() if err != nil { return types.EremeticTask{}, err } taskId := createID(randId.String()) var volumes []*mesos.Volume for _, v := range request.Volumes { volumes = append(volumes, &mesos.Volume{ Mode: mesos.Volume_RW.Enum(), ContainerPath: proto.String(v.ContainerPath), HostPath: proto.String(v.HostPath), }) } var environment []*mesos.Environment_Variable for k, v := range request.Environment { environment = append(environment, &mesos.Environment_Variable{ Name: proto.String(k), Value: proto.String(v), }) } environment = append(environment, &mesos.Environment_Variable{ Name: proto.String("MESOS_TASK_ID"), Value: proto.String(taskId), }) task := types.EremeticTask{ ID: taskId, TaskCPUs: request.TaskCPUs, TaskMem: request.TaskMem, Name: request.Name, Status: mesos.TaskState_TASK_STAGING.String(), Command: &mesos.CommandInfo{ Value: proto.String(request.Command), User: proto.String("root"), Environment: &mesos.Environment{ Variables: environment, }, }, Container: &mesos.ContainerInfo{ Type: mesos.ContainerInfo_DOCKER.Enum(), Docker: &mesos.ContainerInfo_DockerInfo{ Image: proto.String(request.DockerImage), }, Volumes: volumes, }, } return task, nil }
// Create a Location func NewLocation(host uuid.UUID, lp Profile) (*Location, error) { l := new(Location) l.UUID, _ = uuid.V4() l.Active = true l.Host = host if err := mergo.MergeWithOverwrite(&l.Profile, lp); err != nil { return nil, errors.New(err.Error() + " - Could not set initial location profile") } if err := storage.Store(l); err != nil { return nil, errors.New(err.Error() + " - Could not write location to storage") } return l, nil }
// Create a player func NewPlayer(nick string, profile Profile) (*Player, error) { p := new(Player) newUUID, err := uuid.V4() if err != nil { // FIXME: Handle error } p.UUID = newUUID p.Nick = nick p.Profile = profile err = storage.Store(p) if err != nil { return nil, errors.New(err.Error() + " - Could not write player to storage") } return p, nil }
func (c *NewsItem) AddComment(player uuid.UUID, content string) error { comment := Comment{Player: player, Content: content} comment.UUID, _ = uuid.V4() comment.Created = time.Now() c.Comments = append(c.Comments, comment) err := storage.Store(c) if err != nil { return errors.New(err.Error() + " - Could not store updated NewsItem info with added comment") } eventqueue.Publish(utils.CKPTEvent{ Type: utils.NEWS_EVENT, RestrictedTo: []uuid.UUID{c.Author}, Subject: "Kommentar registrert", Message: "Det er registrert ny kommentar på en av dine bidrag på ckpt.no!"}) return nil }
// Create a Tournament func NewTournament(tdata Info) (*Tournament, error) { if err := validateTournamentInfo(tdata); err != nil { return nil, errors.New(err.Error() + " - Could not create tournament") } t := new(Tournament) t.UUID, _ = uuid.V4() if err := mergo.MergeWithOverwrite(&t.Info, tdata); err != nil { return nil, errors.New(err.Error() + " - Could not set initial tournament data") } // Merge seems to not handle time.Time for some reason, thus fixup fixupTournamentInfo(&t.Info, tdata) if err := storage.Store(t); err != nil { return nil, errors.New(err.Error() + " - Could not write tournament to storage") } return t, nil }
// Create a NewsItem func NewNewsItem(itemdata NewsItem, author uuid.UUID) (*NewsItem, error) { c := new(NewsItem) if err := mergo.MergeWithOverwrite(c, itemdata); err != nil { return nil, errors.New(err.Error() + " - Could not set initial NewsItem data") } c.UUID, _ = uuid.V4() c.Author = author c.Created = time.Now() if err := storage.Store(c); err != nil { return nil, errors.New(err.Error() + " - Could not write NewsItem to storage") } eventqueue.Publish(utils.CKPTEvent{ Type: utils.NEWS_EVENT, Subject: "Nytt bidrag lagt ut", Message: "Det er lagt ut et nytt bidrag på ckpt.no!"}) return c, nil }
func (w *WebSocketBarker) NewSocket() (alerts chan interface{}, quit chan struct{}, err error) { id, _ := uuid.V4() key := id.String() alerts = make(chan interface{}, 500) quit = make(chan struct{}, 1) w.muSockets.Lock() if len(w.sockets) > 500 { return alerts, quit, ErrTooManyConns } w.sockets[key] = alerts log.Printf("added socket %s", key) w.muSockets.Unlock() go func() { <-quit w.muSockets.Lock() delete(w.sockets, key) log.Printf("deleted socket %s", key) w.muSockets.Unlock() }() return alerts, quit, nil }
func TestJsonMarshal(t *testing.T) { id, _ := uuid.V4() item := model.StreamItem{ StreamID: id.String(), // NOTE: Converting between int64/float64 at the nanosecond level can // create some tiny drift. In practice, this is fine. Rounding to // the second level avoids issues with testing. Timestamp: time.Now().Round(time.Second), Type: model.TypePost, ID: id.String(), } output, _ := json.Marshal(item) var fromJSON model.StreamItem err := json.Unmarshal(output, &fromJSON) log.WithFields(log.Fields{ "Source": item, "JSON": string(output), "fromJSON": fromJSON, "ERROR": err, }).Debug("StreamItem Example") if err != nil { t.Errorf("Error converting to/from json") } CheckStreamItems(item, fromJSON, t) output2, _ := json.Marshal(model.RoshiStreamItem(item)) var fromJSON2 model.RoshiStreamItem err = json.Unmarshal(output2, &fromJSON2) log.WithFields(log.Fields{ "Source": item, "JSON": string(output2), "fromJSON": fromJSON2, "ERROR": err, }).Debug("RoshiStreamItem Example") if err != nil { t.Errorf("Error converting to/from json") } checkRoshiItems(model.RoshiStreamItem(item), fromJSON2, t) items := []model.StreamItem{ item, item, } rItems, _ := model.ToRoshiStreamItem(items) output3, _ := json.Marshal(rItems) var fromJSON3 []model.RoshiStreamItem err = json.Unmarshal(output3, &fromJSON3) log.WithFields(log.Fields{ "Source": rItems, "JSON": string(output3), "fromJSON": fromJSON3, "ERROR": err, }).Debug("RoshiStreamItem Example") checkAllRoshi(rItems, fromJSON3, t) }
It("sanity?", func() { s, err := service.NewRoshiStreamService(util.GetEnvWithDefault("ROSHI_URL", "http://localhost:6302"), (5 * time.Second)) Expect(err).To(BeNil()) Expect(s).NotTo(BeNil()) }) }) var s service.StreamService BeforeEach(func() { s, _ = service.NewRoshiStreamService(util.GetEnvWithDefault("ROSHI_URL", "http://localhost:6302"), (5 * time.Second)) }) Context(".Add", func() { It("will add a single content item", func() { chanID, _ := uuid.V4() contentID, _ := uuid.V4() content := model.StreamItem{ ID: contentID.String(), Timestamp: time.Now(), Type: model.TypePost, StreamID: chanID.String(), } items := []model.StreamItem{ content, } Expect(s).NotTo(BeNil()) err := s.Add(items) Expect(err).To(BeNil()) })
func GenThoughtID() ThoughtsID { id, _ := uuid.V4() return ThoughtsID{id} }
import ( "encoding/json" "net/http" "time" "github.com/ello/streams/model" "github.com/m4rw3r/uuid" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("StreamController", func() { var id uuid.UUID BeforeEach(func() { id, _ = uuid.V4() }) Context("when adding content via PUT /streams", func() { It("should return a status 201 when passed a correct body", func() { item1ID, _ := uuid.V4() item2ID, _ := uuid.V4() items := []model.StreamItem{{ StreamID: id.String(), Timestamp: time.Now(), Type: 0, ID: item1ID.String(), }, { StreamID: id.String(), Timestamp: time.Now(),
"url": r.URL, "method": r.Method, "headers": r.Header, "body": body, "errors": err, }).Debug("About to issue request") router.ServeHTTP(response, r) } var _ = BeforeSuite(func() { log.SetLevel(log.DebugLevel) router = httprouter.New() StreamID, _ := uuid.V4() streamService = &mockStreamService{ internal: generateFakeResponse(StreamID), } authConfig := api.AuthConfig{ Username: []byte("ello"), Password: []byte("password"), Enabled: false, } streamController := api.NewStreamController(streamService, authConfig) streamController.Register(router) })