Skip to content

Commit

Permalink
chore: commit save point
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmarceleza committed Aug 10, 2023
1 parent 5d2ff28 commit 4c833bc
Show file tree
Hide file tree
Showing 16 changed files with 421 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docker/examples/03/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log

1 change: 1 addition & 0 deletions docker/examples/03/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BACKGROUND_COLOR=#00ff00
27 changes: 27 additions & 0 deletions docker/examples/03/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# === Stage 1: Build the Node.js application ===
FROM node:latest as builder

WORKDIR /app

# Copy the application code and .env file
COPY package.json ./
COPY index.js ./
COPY .env ./

# Install dependencies
RUN npm install --production

# === Stage 2: Create the final lightweight image ===
FROM node:alpine

WORKDIR /app

# Copy the Node.js application and .env file from the builder stage
COPY --from=builder /app .

# Expose port 8000 for the web server
EXPOSE 8000

# Start the Node.js web server when the container starts
CMD ["node", "index.js"]

15 changes: 15 additions & 0 deletions docker/examples/03/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
body {
background-color: #<%= background_color %>;
}
</style>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

75 changes: 75 additions & 0 deletions docker/examples/03/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const http = require('http');
const fs = require('fs');
const path = require('path');

const indexHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
body {
margin: 0;
padding: 0;
--background-color: %s;
background-color: var(--background-color);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-size: 100px;
color: white;
}
</style>
</head>
<body>
<div>Hello World</div>
<script>
function updateBackgroundColor() {
const helloWorldElement = document.querySelector('body');
helloWorldElement.style.backgroundColor = getComputedStyle(helloWorldElement).getPropertyValue('--background-color');
}
// Call the updateBackgroundColor function initially
updateBackgroundColor();
// Periodically check for changes in the BACKGROUND_COLOR variable and update the background color
setInterval(updateBackgroundColor, 1000);
</script>
</body>
</html>
`;

function readBackgroundColorFromEnvFile() {
const envFilePath = path.join(__dirname, '.env');
let backgroundColor;
try {
const envFileContent = fs.readFileSync(envFilePath, 'utf8');
const envVars = envFileContent.split('\n');
for (const envVar of envVars) {
const [key, value] = envVar.split('=');
if (key === 'BACKGROUND_COLOR') {
backgroundColor = value;
break;
}
}
} catch (err) {
console.error('Error reading .env file:', err);
}
return backgroundColor || '#ff0000';
}

const server = http.createServer((req, res) => {
const backgroundColor = readBackgroundColorFromEnvFile();

res.setHeader('Content-Type', 'text/html');
res.write(indexHTML.replace('%s', backgroundColor));
res.end();
});

const port = 8000;
server.listen(port, () => {
console.log(`Server started on http://localhost:${port}`);
});

86 changes: 86 additions & 0 deletions docker/examples/03/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
)

const indexHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
body {
margin: 0;
padding: 0;
--background-color: %s;
background-color: var(--background-color);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-size: 24px;
color: white;
}
</style>
</head>
<body>
<div>Hello World</div>
<script>
function updateBackgroundColor() {
const helloWorldElement = document.querySelector('body');
helloWorldElement.style.backgroundColor = getComputedStyle(helloWorldElement).getPropertyValue('--background-color');
}
// Call the updateBackgroundColor function initially
updateBackgroundColor();
// Periodically check for changes in the BACKGROUND_COLOR variable and update the background color
setInterval(updateBackgroundColor, 1000);
</script>
</body>
</html>
`

func readBackgroundColorFromEnvFile() (string, error) {
envFilePath := filepath.Join("/app", ".env")
content, err := ioutil.ReadFile(envFilePath)
if err != nil {
return "", err
}
return string(content), nil
}

func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
backgroundColor, err := readBackgroundColorFromEnvFile()
if err != nil {
log.Printf("Error reading .env file: %v\n", err)
// Default background color (red) if .env file cannot be read or the value is empty
backgroundColor = "#ff0000"
}

// Set the Content-Type header to serve HTML
w.Header().Set("Content-Type", "text/html")

// Format the HTML page with the correct background color and send it as the response
html := fmt.Sprintf(indexHTML, backgroundColor)
fmt.Fprint(w, html)
}

func main() {
http.HandleFunc("/", helloWorldHandler)

port := "8000"
log.Printf("Server started on http://localhost:%s\n", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err)
}
}

10 changes: 10 additions & 0 deletions docker/examples/03/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "hello-world-app",
"version": "1.0.0",
"description": "Hello World Node.js App",
"main": "index.js",
"author": "Your Name",
"license": "MIT",
"dependencies": {}
}

1 change: 1 addition & 0 deletions docker/examples/04/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BACKGROUND_COLOR=#00ff00
28 changes: 28 additions & 0 deletions docker/examples/04/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# === Stage 1: Build the Go binary ===
FROM golang:latest as builder

# Set the working directory inside the container
WORKDIR /app

# Copy the Go source code and .env file into the container
COPY main.go ./

# Build the Go application inside the container
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o web-server main.go

# === Stage 2: Create the final lightweight image ===
FROM alpine:latest

# Set the working directory inside the container
WORKDIR /app

# Copy the Go binary from the builder stage
COPY --from=builder /app/web-server .

COPY .env ./
# Expose port 8000 for the web server
EXPOSE 8000

# Start the Go web server when the container starts
CMD ["./web-server"]

98 changes: 98 additions & 0 deletions docker/examples/04/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// main.go

package main

import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)

const indexHTML = `
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
body {
margin: 0;
padding: 0;
--background-color: %s;
background-color: var(--background-color);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-size: 24px;
color: white;
}
</style>
</head>
<body>
<div>Hello World</div>
<script>
function updateBackgroundColor() {
const helloWorldElement = document.querySelector('body');
helloWorldElement.style.backgroundColor = getComputedStyle(helloWorldElement).getPropertyValue('--background-color');
}
// Call the updateBackgroundColor function initially
updateBackgroundColor();
// Periodically check for changes in the BACKGROUND_COLOR variable and update the background color
setInterval(updateBackgroundColor, 1000);
</script>
</body>
</html>
`

func readBackgroundColorFromEnvFile() (string, error) {
envFilePath := filepath.Join("/app", ".env")
content, err := os.ReadFile(envFilePath)
if err != nil {
return "", err
}

// Split lines and find the BACKGROUND_COLOR
lines := strings.Split(string(content), "\n")
for _, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 && parts[0] == "BACKGROUND_COLOR" {
return strings.TrimSpace(parts[1]), nil
}
}

return "", fmt.Errorf("BACKGROUND_COLOR not found in .env file")
}

func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
backgroundColor, err := readBackgroundColorFromEnvFile()
if err != nil {
log.Printf("Error reading .env file: %v\n", err)
// Default background color (red) if .env file cannot be read or the value is empty
backgroundColor = "#ff0000"
}

// Set the Content-Type header to serve HTML
w.Header().Set("Content-Type", "text/html")

// Format the HTML page with the correct background color and send it as the response
html := fmt.Sprintf(indexHTML, backgroundColor)
fmt.Fprint(w, html)
}

func main() {
http.HandleFunc("/", helloWorldHandler)

port := "8000"
log.Printf("Server started on http://localhost:%s\n", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err)
}
}

22 changes: 22 additions & 0 deletions kubernetes/kustomize/examples/05/base/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy
namespace: default
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
7 changes: 7 additions & 0 deletions kubernetes/kustomize/examples/05/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: teste
namespace: default
data:
key: value
7 changes: 7 additions & 0 deletions kubernetes/kustomize/examples/05/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy
namespace: default
spec:
replicas: 2
Loading

0 comments on commit 4c833bc

Please sign in to comment.