Categorygithub.com/bulbosaur/web-calculator-golang
module
0.0.0-20250301200118-2df749479a88
Repository: https://github.com/bulbosaur/web-calculator-golang.git
Documentation: pkg.go.dev

# README

Web-calculator | English | Русский

web-calculator is a web service that allows users to send arithmetic expressions via HTTP and receive their results.

Functionality

  • Supports addition, subtraction, multiplication, division operations, and expressions in parentheses
  • Expressions can be entered with or without spaces between numbers and operands
  • The calculator accepts positive integers as input

Requirements

  • Go version 1.22 or newer

⚠️ Warning

Dear Yandex Practicum Reviewers The comments in the code, from start to finish, were written by me and comply with the requirements of go-lint, which I use in my project to adhere to the code style.

Installation

  1. Clone the repository
git clone https://github.com/bulbosaur/web-calculator-golang
  1. Run the server from the project repository
go run cmd/main.go

Environment Variables

VariableDescriptionDefault Value
PORTPort for running the server8080
HOSTHost for running the serverlocalhost
TIME_ADDITION_MSExecution time for the addition operation in milliseconds100
TIME_SUBTRACTION_MSExecution time for the subtraction operation in milliseconds100
TIME_MULTIPLICATIONS_MSExecution time for the multiplication operation in milliseconds100
TIME_DIVISIONS_MSExecution time for the division operation in milliseconds100
DATABASE_PATHPath to the database

To change the values of the environment variables, you need to create a file named config.yaml (or edit the existing file example_config.yaml).

Example of a Config File

# web-calculator-golang/config/config.yaml
server:
  host: localhost
  port: 8080

time:
  TIME_ADDITION_MS: 100
  TIME_SUBTRACTION_MS: 100
  TIME_MULTIPLICATIONS_MS: 100
  TIME_DIVISIONS_MS: 100

database:
  DATABASE_PATH: ./db/calc.db

API

Default base URL: http://localhost:8080

API endpointMethodRequest BodyServer ResponseResponse Code
/api/v1/calculatePOST{"expression": "2 * 2"}{"result":"4"}200
/api/v1/calculatePOST"expression": "2 * 2"{"error":"Bad request","error_message":"invalid request body"}400
/api/v1/calculateGET{"expression": "2 * 2"}Method Not Allowed405
/coffeeI'm a teapot418
/api/v1/tea404 page not found404

Response Codes

  • 200 - Successful request
  • 400 - Bad request
  • 404 - Resource not found
  • 405 - Method not allowed
  • 422 - Invalid expression (e.g., English letter instead of a number)
  • 500 - Internal server error

Usage Examples

For sending POST requests, it's most convenient to use Postman.

  1. StatusOK 200
curl 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "42 + 5 * 2"
}'

# {"result":52}
curl 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "6-8"
}'

# {"result":-2}
curl 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "123(3/2)"
}'

# {"result":184.5}
  1. Bad Request 400
curl 'localhost/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "2 * 2
}'

# {"error":"Bad request","error_message":"invalid request body"}
  1. Unprocessable Entity 422
curl 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "cat + 100500"
}'

# {"error":"Expression is not valid","error_message":"invalid characters in expression"}
curl 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "()"
}'

# {"error":"Expression is not valid","error_message":"the brackets are empty"}
curl 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "1/(2 - 3 + 1)"
}'

# {"error":"Expression is not valid","error_message":"division by zero is not allowed"}
curl 'localhost:8080/api/v1/calculate' \
--header 'Content-Type: application/json' \
--data '{
  "expression": "1 000 000 + 6"
}'

# {"error":"Expression is not valid","error_message":"missing operand"}

# Packages

No description provided by the author
No description provided by the author