Skip to content

Commit

Permalink
Merge cd8922c into 53d2fca
Browse files Browse the repository at this point in the history
  • Loading branch information
rv404674 committed Aug 21, 2020
2 parents 53d2fca + cd8922c commit 087166d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
26 changes: 16 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,29 @@ BINARY_UNIX=$(BINARY_NAME)_unix

# if we do "make run main", it will do "go run main.go"
execute:
# will run the go executable, and hence the server.
~/go/bin/goRubu
- @echo "** Please wait. Connecting with Mongo, Memcached and Spinning up the Go Server **"
- ~/go/bin/goRubu

setup:
export GOBIN=~/go/bin/
- export GOBIN=~/go/bin/
# my pwd is "/Users/home"
$(GOCMD) mod init goRubu
- $(GOCMD) mod init goRubu

install:
# will build the package into a single binary.
echo "Executing go install"
$(GOINSTALL)
- @echo "** Will build the package into a single binary **"
- $(GOINSTALL)

# make all -> will first install and then run execute
all:
install execute
- install execute

docker:
- chmod 777 scripts/build_docker.sh
- scripts/build_docker.sh
- @echo "** Spinning up the GoRubu, Mongo and Memcached Docker Containers **"
- @chmod 777 scripts/build_docker.sh
- @scripts/build_docker.sh

#NOTE: @ before a command will stop showing that command.
test:
- @echo "** Running Tests **"
- $(GOTEST) ./tests -v

15 changes: 9 additions & 6 deletions commands_benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
telnet localhost 11211
get https://goRubu/MTAyMTk=
```
this will give the original url, to corresponding to the shorten url.
If you get nothing, it means there is not entry in memcached.
This will give the original url, corresponding to the shortened url.

2. To check Indexed in Mongo.

2. To check Indexes in Mongo.
```bash
db.collection.getIndexes()
```
Expand All @@ -19,16 +19,19 @@ go test ./tests -v
go test ./tests -v - cover
```

**NOTE** - Normally these two commands work, but with "go 1.13" they are not working.
Hence use these
**NOTE** - Normally these two commands work, but with "go 1.13", second command (the cover one) is not working.

I have integrated [coveralls.io](https://coveralls.io/) with goRubu.
or you can use these.

```bash
go test ./... -v -coverpkg=./... -coverprofile=cover.txt
go tool cover -html=cover.txt -o cover.html
```

## Docker

1. This will give the logs of that container.
1. This will give the logs of a particular container.
```bash
docker logs container_id
```
Expand Down
22 changes: 10 additions & 12 deletions daos/mainDao.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ func CleanDb(uid int) {
}

// GetCounterValue - update counter field in second collection - incrementer
// { "_id" : ObjectId("5e9b7c0e7b3a8740a2f828c4"), "uniqueid" : "counter", "value" : 10000 }
// { "_id" : ObjectId("5e9b7c0e7b3a8740a2f828c4"), "value" : 10000 }
func GetCounterValue() int {
// as there will be one row only
collection := client.Database(DB_NAME).Collection(COLLECTION2_NAME)
filter := bson.D{primitive.E{Key: "uniqueid", Value: "counter"}}

var result models.IncrementerModel
err := collection.FindOne(context.TODO(), filter).Decode(&result)

err := collection.FindOne(context.TODO(), bson.D{}).Decode(&result)
if err != nil {
log.Println("**ERROR while fetching counter value", err)
// its first hit. You need to create a counter value.
insertResult, _ := collection.InsertOne(context.Background(), models.IncrementerModel{UniqueId: "counter", Value: 10000})
log.Println("Initialized counter value InsertedId: ", insertResult.InsertedID)
_ = collection.FindOne(context.TODO(), filter).Decode(&result)
log.Println("** ERROR while fetching counter value", err)
// it's a first hit i.e that is "incrementer" collection is right now empty.
// You need to create a counter value.
insertResult, _ := collection.InsertOne(context.Background(), models.IncrementerModel{Value: 10000})
log.Println(" Initialized counter value InsertedId: ", insertResult.InsertedID)
_ = collection.FindOne(context.TODO(), bson.D{}).Decode(&result)
}

return result.Value
Expand All @@ -138,16 +138,14 @@ func GetCounterValue() int {
// UpdateCounter - update the counter value by 1
func UpdateCounter() {
collection := client.Database(DB_NAME).Collection(COLLECTION2_NAME)
filter := bson.D{primitive.E{Key: "uniqueid", Value: "counter"}}

// $inc will increase value of counter by 1
update := bson.D{
{"$inc", bson.D{
{"value", 1},
}},
}

updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
updateResult, err := collection.UpdateOne(context.TODO(), bson.D{}, update)
if err != nil {
log.Fatal("**ERROR while updating counter value", err)
}
Expand Down
4 changes: 1 addition & 3 deletions models/mainModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ type UrlModel struct {
}

// IncrementerModel - single row table
// uniqueid - counter
// value 10000
type IncrementerModel struct {
UniqueId string `json: "uniqueid"`
Value int `json: "value"`
Value int `json: "value"`
}
1 change: 1 addition & 0 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// main test function. Checks both url creation and url redirection
// should start with "Test" and latter name should start with capital letter
// TODO: Increase the text coverage. Test for negative cases as well.

func commonUtility(input string, wait string) string {
shortenedUrl := service.CreateShortenedUrl(input)
Expand Down

0 comments on commit 087166d

Please sign in to comment.