-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from codescalersinternships/development
Implementation of DateTimeServer using gin and http packages
- Loading branch information
Showing
16 changed files
with
596 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Format | ||
|
||
on: | ||
push: | ||
branches: [ "development" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
GO_VERSION: '1.22.4' | ||
|
||
jobs: | ||
|
||
fmt: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Install Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ env.GO_VERSION }} | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install dependencies | ||
run: make install_dependencies | ||
|
||
- name: Run format | ||
run: make format | ||
|
||
- name: Check format | ||
uses: Jerome1337/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Lint | ||
|
||
on: | ||
push: | ||
branches: [ "development" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
GO_VERSION: '1.22.4' | ||
|
||
jobs: | ||
|
||
lint: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Install Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ env.GO_VERSION }} | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install dependencies | ||
run: make install_dependencies | ||
|
||
|
||
- name: Run linter | ||
run: make lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [ "development" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
GO_VERSION: '1.22.4' | ||
|
||
jobs: | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Install Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ env.GO_VERSION }} | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install dependencies | ||
run: make install_dependencies | ||
|
||
- name: Run tests | ||
run: make test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Binary files # | ||
* | ||
!*.* | ||
!*/ | ||
|
||
# Bin # | ||
bin/ | ||
|
||
# Coverage file | ||
|
||
*.out | ||
|
||
# Docker file and Make file | ||
|
||
!dockerfile | ||
!Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
all: install_dependencies build_binaries format lint test | ||
|
||
install_dependencies: | ||
go get ./... | ||
|
||
build_binaries: install_dependencies | ||
go build -o ./httpserver cmd/httpserver/main.go | ||
go build -o ./ginserver cmd/ginserver/main.go | ||
|
||
run_binaries: build_binaries | ||
./httpserver | ||
./ginserver | ||
|
||
format: | ||
go fmt ./... | ||
lint: | ||
sudo snap install golangci-lint --classic | ||
golangci-lint run ./... | ||
|
||
test: | ||
go test ./... -v | ||
|
||
build_images: | ||
docker build --tag=buildme-http:v3.0 --target=http | ||
docker build --tag=buildme-gin:v3.0 --target=gin | ||
|
||
run_containers: | ||
docker compose up |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
# DateTime-Server-Abdelrahman-Mahmoud | ||
# DateTime-Server-Abdelrahman-Mahmoud | ||
|
||
## Introduction | ||
|
||
A server is a computer system capable of delivering web content to end users over the internet via a web browser. In this project we deliver the current date and time to the user using two golang packages net/http and gin. | ||
|
||
## Feature /datetime | ||
|
||
This end point returns a message that has the current date and time as a JSON for simplicity and easier exchange of data between different technologies. | ||
|
||
```JavaScript | ||
{"message": "2024-07-07 16:08"} | ||
``` | ||
|
||
## Setup | ||
|
||
1. Clone the Repository to a directory of your choice. | ||
2. Make sure you have go version 1.22.4 installed on your device. | ||
3. Open the directory in a code editor of your choice. | ||
4. Navigate to cmd/ginserver or cmd/httpserver directory | ||
5. Build the project using: | ||
```console | ||
user@user-VirtualBox:~$ go build main.go | ||
``` | ||
6. Run the project using: | ||
```console | ||
user@user-VirtualBox:~$ ./main | ||
``` | ||
7. Open a web browser of your choice. | ||
8. Use http://localhost:8090/datetime for http server or http://localhost:8080/datetime for gin server to perform a request. | ||
9. Shutdown the server using ctrl + c inside the terminal. | ||
|
||
## Tests | ||
|
||
=== RUN TestClientCanHitAPI | ||
=== RUN TestClientCanHitAPI/happy_path_-_can_hit_the_api_and_return_date_&_time | ||
--- PASS: TestClientCanHitAPI (0.00s) | ||
--- PASS: TestClientCanHitAPI/happy_path_-_can_hit_the_api_and_return_date_&_time (0.00s) | ||
PASS | ||
ok github.com/codescalersinternships/DateTime-Server-Abdelrahman-Mahmoud/pkg/httpclient (cached) | ||
=== RUN Test_GetDateTimeHandler | ||
=== RUN Test_GetDateTimeHandler/happy_path_-_can_hit_the_api_and_return_date_&_time | ||
--- PASS: Test_GetDateTimeHandler (0.00s) | ||
--- PASS: Test_GetDateTimeHandler/happy_path_-_can_hit_the_api_and_return_date_&_time (0.00s) | ||
PASS | ||
ok github.com/codescalersinternships/DateTime-Server-Abdelrahman-Mahmoud/pkg/httpserver (cached) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/codescalersinternships/DateTime-Server-Abdelrahman-Mahmoud/pkg/ginserver" | ||
) | ||
|
||
func main() { | ||
r := ginserver.SetupRouter() | ||
|
||
err := r.Run(":8080") | ||
|
||
if err != nil { | ||
log.Fatalf("HTTP server error: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/codescalersinternships/DateTime-Server-Abdelrahman-Mahmoud/pkg/httpserver" | ||
) | ||
|
||
func main() { | ||
log.Println("Server is starting...") | ||
|
||
server := &http.Server{ | ||
Addr: ":8090", | ||
} | ||
|
||
http.HandleFunc("/datetime", httpserver.GetDateTimeHandler) | ||
|
||
go func() { | ||
err := server.ListenAndServe() | ||
if !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatalf("HTTP server error: %v", err) | ||
} | ||
}() | ||
|
||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) | ||
<-sigChan | ||
|
||
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer shutdownRelease() | ||
|
||
if err := server.Shutdown(shutdownCtx); err != nil { | ||
log.Fatalf("HTTP shutdown error: %v", err) | ||
} | ||
log.Println("Graceful shutdown complete.") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
services: | ||
http: | ||
image: buildme-http:v1 | ||
ports: | ||
- "8090:8090" | ||
gin: | ||
image: buildme-gin:v1 | ||
ports: | ||
- "8080:8080" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# base stage that copy files and builds them | ||
|
||
FROM golang:1.22.5-alpine3.20 AS base | ||
|
||
WORKDIR /app | ||
|
||
COPY . . | ||
|
||
RUN go mod download | ||
|
||
RUN go build -o /bin/httpserver ./cmd/httpserver/main.go | ||
RUN go build -o /bin/ginserver ./cmd/ginserver/main.go | ||
|
||
# run stage that run binaries | ||
FROM scratch AS http | ||
COPY --from=base /bin/httpserver /bin/ | ||
ENTRYPOINT [ "/bin/httpserver" ] | ||
|
||
EXPOSE 8090 | ||
|
||
FROM scratch AS gin | ||
COPY --from=base /bin/ginserver /bin/ | ||
ENTRYPOINT [ "/bin/ginserver" ] | ||
|
||
EXPOSE 8085 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module github.com/codescalersinternships/DateTime-Server-Abdelrahman-Mahmoud | ||
|
||
go 1.22.4 | ||
|
||
require ( | ||
github.com/bytedance/sonic v1.11.9 // indirect | ||
github.com/bytedance/sonic/loader v0.1.1 // indirect | ||
github.com/cloudwego/base64x v0.1.4 // indirect | ||
github.com/cloudwego/iasm v0.2.0 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/gabriel-vasile/mimetype v1.4.4 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/gin-gonic/gin v1.10.0 // indirect | ||
github.com/go-playground/locales v0.14.1 // indirect | ||
github.com/go-playground/universal-translator v0.18.1 // indirect | ||
github.com/go-playground/validator/v10 v10.22.0 // indirect | ||
github.com/goccy/go-json v0.10.3 // indirect | ||
github.com/gorilla/mux v1.8.1 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.8 // indirect | ||
github.com/leodido/go-urn v1.4.0 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.9.0 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.12 // indirect | ||
golang.org/x/arch v0.8.0 // indirect | ||
golang.org/x/crypto v0.24.0 // indirect | ||
golang.org/x/net v0.26.0 // indirect | ||
golang.org/x/sys v0.21.0 // indirect | ||
golang.org/x/text v0.16.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.