Activity app is a golang based application used to track the daily activites of a user.This app supports following functionalities:
Create a user
Create an activity for user
Update the activity attributes
Retrieve the status of an activity
Retrieve the activities of a user
Retrieve all the user
This implementation at present supports only four activities namely PLAY,SLEEP,READ,EAT
This application uses following tech stack
Language: Golang
Database: MongoDB
Communication: gRPC
Message Formats: ProtoBuffer
If this is your first time encountering Go, please follow the instructions to install Go on your computer. The application requires Go 1.13 or above and mongoDB v4.2.1 Make sure you install and run mongo service before continuing. After installing go run the following commands to start using this application
# clone the repo from github
git clone github.com/rahullenkala/activityapp
cd activityapp
# navigate to server repo
cd activityapp/cmd/server
# run the server...!
go run server.go
You have successfully started gRPC server listening on "127.0.0.1:50052"
Let us start the client program.
Note:-Client implements only few methods of the activity app service.
# navigate to the client repo
cd activityapp/cmd/server
# run the client...!!!
go run client.go
Now let us take a look at each rpc call and their usage, supported by this application.
This call is used to create a new user in the application, this call returns an error if an invalid user-name or phone-number are provided. Following are the definitions of User and Response
message User {
string name=1;
string email=2;
string phone=3; #Phone is used as a primary key in db
}
message Response{
string message=1;
}
#Example
response, err := appService.CreateUser(ctx, &pb.User{
Name: name,
Email: emailID,
Phone: Phone,
})
This call is used to create and assign an activity to a user, this call return an error in the following cases.
- Invalid User
- Redundant Activity on the same day
Activites can be created in advance by specifying the timestamp of the date. Following are the definitions of Activity and CreateActivityRequest
message Activity {
ActivityType type=1;
int64 timestamp=2;
bool status=3;
uint64 duration=4;
}
message CreateActivityRequest{
Activity Activity=1;
string Phone=2;
}
#Example
act := &pb.Activity{
Type: pb.ActivityType(option),
Duration: duration,
}
response, creationerr := appService.CreateActivity(ctx, &pb.CreateActivityRequest{
Activity: act,
Phone: Phone,
})
This call is used to update the attributes of an activity,users can only update the status and the duration of the activity. Following are the definitions of UpdateActivityRequest
message UpdateActivityRequest{
Activity Activity=1;
string Phone=2;
int64 Time=3; #Timestamp to identify the activity based on the date
UpdateParam Parameter=4;
}
#Example
response,err:=appService.UpdateActivity(ctx,&pb.UpdateActivityRequest{
Phone:"1234567890",
Time:12345678,
Parameter: pb.UpdateParam_STATUS,
})
This call is used to retrieve the status of the activity by calling two in-built methods.
- isDone()
- isValid()
Following are the definitions for ActivityStatus Request/Response.
message ActivityStatusRequest{
string Phone=1;
ActivityType Activitytype=2;
StatusMethod Method=3;
int64 Time=4;
}
message ActivityStatusResponse{
bool status=1;
}
#Example
request,err:=appService.GetActivityStatus(ctx,&pb.ActivityStatusRequest{
Phone:"",
Method:pb.StatusMethod_DONE,
Activitytype: pb.ActivityType_EAT,
})
This call is used to retrieve activity/activities of a user on a particular date. Following are the definitions of UserActivity Request/Response
message UserActivityRequest{
string Phone=1;
int64 Time=2;
ActivityType Type=3;
bool batch=4;
}
message UserActivityResponse{
repeated Activity activities=1;
}
#Example
response,err:=appService.GetUserActivities(ctx,&pb.UserActivityRequest{
Phone: "0987654321",
Time: 1234567,
Type: pb.ActivityType_EAT,
Batch: false,
})
This call is used to retrieve all the users registered with this application,this call returns a uni-directional gRPC stream. Data can be retrieved from this stream.
stream, streamErr := appService.GetUsers(ctx, &pb.Empty{})
if err != nil {
log.Fatal(streamErr)
}