Skip to content

iamnalinor/YL-math-calc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YL-math-calc

Final Yandex.Lyceum project

Task: Write a program that can calculate the value of a mathematical expression. But there's a catch: every operation takes a lot of time to be completed, say, 1 minute. The program should use Go parallelism to calculate the expression faster.

Usage

Setup

  1. Install Go from the official website.
  2. Clone the repository or download the source code.
  3. Install dependencies by running go mod tidy.
  4. Run go run cmd/main.go in the project directory.

The recommended Go version is 1.22. It should work on 1.21 too, however, it is not guaranteed.

Web server will start at localhost:8081.

Database

SQLite is used as the database. The database file is db.sqlite3. It is created automatically when the program is run.

Authorization

Create account:

POST http://localhost:8081/api/v1/register

Body:

{
  "login": "your_login",
  "password": "your_password"
}

Curl example:

curl -X POST http://localhost:8081/api/v1/register -d "{\"login\": \"your_login\", \"password\": \"your_password\"}"

Login:

POST http://localhost:8081/api/v1/login

Body:

{
  "login": "your_login",
  "password": "your_password"
}

Result will be json with token:

{
  "token": "<token>"
}

Curl example:

curl -X POST http://localhost:8081/api/v1/login -d "{\"login\": \"your_login\", \"password\": \"your_password\"}"

Token is valid for 24 hours. Add the token to the Authorization Bearer header in the following requests in format Authorization: Bearer <token>.

Creating expression

POST http://localhost:8081/api/v1/createExpression

Body:

{
    "expression": "2+2*2"
}

Additionally, you can specify idempotency token in X-Idempotency-Token.

Result:

{"id":42}

Curl example:

curl -X POST http://localhost:8081/api/v1/createExpression  -H "Authorization: Bearer <token>" -d "{\"expression\": \"2+2*2\"}"

Replace <token> with the token obtained from the /login endpoint.

Examples of expressions:

  • 2+2*2
  • (5 + 5) / (8 * 3)
  • (0 / 0) + 1

If one of the operations results with error, the entire expression will be marked as errored.

Getting result

GET http://localhost:8081/api/v1/expression/42

Replace 42 with the ID of the expression. You can obtain the ID from the /createExpression response.

Result:

{
  "id": 42,
  "type": "expression",
  "expression": "2+2*2",
  "status": "done",
  "result": 6,
  "created_time": "2021-10-10T12:00:00Z",
  "finished_time": "2021-10-10T12:01:00Z"
}

Curl example:

curl http://localhost:8081/api/v1/expression/42 -H "Authorization: Bearer <token>"

Replace <token> with the token obtained from the /login endpoint.

Docs

Documentation is available at GitHub Wiki.

License

This code is licensed under the GNU GPL v3.0. You can find the license in the LICENSE file.

Contact

If you encounter any issues, feel free to contact me at Telegram: @nalinor

About

Graduate project for Yandex Lyceum "Programming on Go" course

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages