This project implements a simple API in Go that returns the current time in Toronto in JSON format. Each request made to the API logs the current time into a MySQL database. The application uses the time package for time zone conversion and MySQL for data persistence.
- API Endpoint:
/current-timethat returns the current time in Toronto. - Database Logging: Each API request logs the current time into a MySQL database.
- Time Zone Handling: Automatically adjusts to Toronto's local time (
America/Toronto). - MySQL Integration: Logs time to a MySQL database running locally or in a Docker container.
To run this project, you will need:
- Go
- MySQL
git remote add origin https://github.com/Krishna868601/Week13.git
cd Week13-
Install MySQL:
- Download and install MySQL from here.
- Start the MySQL service on your machine.
-
Connect to MySQL:
-
Open MySQL Workbench or open a command line terminal (e.g., Command Prompt or PowerShell).
-
Use the following command to connect to MySQL as the root user:
mysql -u root -p
-
Enter your MySQL root password when prompted.
-
-
Create a new database:
-
After successfully logging in, create a new database named
toronto_time:CREATE DATABASE toronto_time; USE toronto_time;
-
-
Create the
time_logtable:-
Create a table named
time_logthat will store the timestamp of each API request:CREATE TABLE IF NOT EXISTS time_log ( id INT AUTO_INCREMENT PRIMARY KEY, timestamp DATETIME NOT NULL );
-
This will create a table with two columns:
id: An auto-incrementing integer to serve as the primary key.timestamp: ADATETIMEfield that stores the timestamp of each logged request.
-
To start the Go application, run the following command:
go run main.goThe API will start and listen on port 8090
You can now test the API by making a GET request to:
http://localhost:8090/current-timeThis will return the current time in Toronto in JSON format, Each request will also log the current time into the time_log table in MySQL.

