Skip to content

Commit

Permalink
Merge pull request #1 from codescalersinternships/development
Browse files Browse the repository at this point in the history
datetime Server
  • Loading branch information
xmonader authored Jan 13, 2025
2 parents 6bb8798 + 80a52f4 commit 4a86312
Show file tree
Hide file tree
Showing 21 changed files with 831 additions and 1 deletion.
45 changes: 45 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Go CI

on:
push:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23.1'
- name: Build
run: go build -v ./...


golangci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
- name: Go Format
uses: Jerome1337/[email protected]
with:
gofmt-path: './src'
gofmt-flags: '-l -d'
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: Test http server
run: go test -v ./pkg/httpserver
- name: Test gin server
run: go test -v ./pkg/ginserver
17 changes: 17 additions & 0 deletions Dockerfile.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:latest AS builder

WORKDIR /build

COPY . .

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -o ginout ./cmd/ginserver/main.go

EXPOSE 8083

FROM scratch
WORKDIR /app
COPY --from=builder /build/ginout .

ENTRYPOINT [ "/app/ginout" ]
17 changes: 17 additions & 0 deletions Dockerfile.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:latest AS builder

WORKDIR /build

COPY . .

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -o httpout ./cmd/httpserver/main.go

EXPOSE 8080

FROM scratch
WORKDIR /app
COPY --from=builder /build/httpout .

ENTRYPOINT [ "/app/httpout" ]
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
build:
go build -o httpout ./cmd/httpserver/main.go
go build -o ginout ./cmd/ginserver/main.go

format:
go fmt ./...

lint:
go install github.com/golangci/golangci-lint/cmd/[email protected]
golangci-lint run ./...

build-run-Images:
docker-compose up

all: build format lint build-run-Images
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
# Date-time-server-RawanMostafa
# Datetime Server

This repository implements http and Gin datetime servers.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)


## Installation

1. Clone the repository

```bash
git clone https://github.com/codescalersinternships/Datetime-server-RawanMostafa.git
```

2. Install the dependencies
```bash
go mod download
```

## Usage

### 1. Using Makefile

To run all make targets

```bash
make all
```

### 2. Using docker-compose


```bash
docker-compose up
```

### 2. Using kubernetes deployed server


```bash
curl http://185.206.122.17:30100/
```

### 3. Using main.go

```bash
go run cmd/httpserver/main.go
```
```bash
go run cmd/ginserver/main.go
```
49 changes: 49 additions & 0 deletions cmd/ginserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"flag"
"fmt"
"log"
"os"

ginhandler "github.com/codescalersinternships/Datetime-server-RawanMostafa/pkg/ginserver"

"github.com/gin-gonic/gin"
)

const defaultPort = "8083"

func getFlags() string {
var port string
flag.StringVar(&port, "port", "", "Specifies the port")

flag.Parse()
return port
}

func decideConfigs() string {

port := getFlags()

if port == "" {
envPort, found := os.LookupEnv("DATETIME_PORT")

if found {
port = envPort
} else {
port = defaultPort
}
}
return port

}
func main() {
port := decideConfigs()
r := gin.Default()
r.GET("/", ginhandler.GinHome)
r.GET("/datetime", ginhandler.GinHandler)
err := r.Run(fmt.Sprintf(":%s", port))
if err != nil {
log.Fatalf("Error: impossible to start server: %s", err)
}
}
53 changes: 53 additions & 0 deletions cmd/httpserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"os"

httphandler "github.com/codescalersinternships/Datetime-server-RawanMostafa/pkg/httpserver"
)

const defaultPort = "8083"

func getFlags() string {
var port string
flag.StringVar(&port, "port", "", "Specifies the port")

flag.Parse()
return port
}

func decideConfigs() string {

port := getFlags()

if port == "" {
envPort, found := os.LookupEnv("DATETIME_PORT")

if found {
port = envPort
} else {
port = defaultPort
}
}
return port

}
func main() {
port := decideConfigs()

fmt.Println("Starting our simple http server.")

http.HandleFunc("/", httphandler.HttpHome)
http.HandleFunc("/datetime", httphandler.HttpHandler)

fmt.Printf("Started on port :%s\n", port)

err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
log.Fatal(err)
}
}
23 changes: 23 additions & 0 deletions datetime-server/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions datetime-server/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: datetime-server
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
21 changes: 21 additions & 0 deletions datetime-server/templates/gin-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: gin-deployment
labels:
app: {{ .Values.ginAppName }}
spec:
replicas: 1
selector:
matchLabels:
app: {{ .Values.ginAppName }}
template:
metadata:
labels:
app: {{ .Values.ginAppName }}
spec:
containers:
- name: {{ .Values.ginAppName }}
image: "{{ .Values.ginImage.name }}:{{ .Values.ginImage.tag }}"
ports:
- containerPort: 8083
13 changes: 13 additions & 0 deletions datetime-server/templates/gin-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: gin-service
spec:
type: NodePort
selector:
app: {{ .Values.ginAppName }}
ports:
- protocol: TCP
port: 80
targetPort: 8083
nodePort: 30500
22 changes: 22 additions & 0 deletions datetime-server/templates/http-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: http-deployment
labels:
app: {{ .Values.httpAppName }}
spec:
replicas: 1
selector:
matchLabels:
app: {{ .Values.httpAppName }}
template:
metadata:
labels:
app: {{ .Values.httpAppName }}
spec:
containers:
- name: {{ .Values.httpAppName }}
image: "{{ .Values.httpImage.name }}:{{ .Values.httpImage.tag }}"
ports:
- containerPort: 8080

13 changes: 13 additions & 0 deletions datetime-server/templates/http-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: http-service
spec:
type: NodePort
selector:
app: {{ .Values.httpAppName }}
ports:
- protocol: TCP
port: 8083
targetPort: 8080
nodePort: 30400
10 changes: 10 additions & 0 deletions datetime-server/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ginAppName: gin-server
httpAppName: http-server

ginImage:
name: rawanmostafa/ginserver
tag: latest

httpImage:
name: rawanmostafa/httpserver
tag: latest
Loading

0 comments on commit 4a86312

Please sign in to comment.