-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d2ff28
commit 4c833bc
Showing
16 changed files
with
421 additions
and
0 deletions.
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,3 @@ | ||
node_modules | ||
npm-debug.log | ||
|
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 @@ | ||
BACKGROUND_COLOR=#00ff00 |
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,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"] | ||
|
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,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Hello World</title> | ||
<style> | ||
body { | ||
background-color: #<%= background_color %>; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Hello World</h1> | ||
</body> | ||
</html> | ||
|
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,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}`); | ||
}); | ||
|
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,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) | ||
} | ||
} | ||
|
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,10 @@ | ||
{ | ||
"name": "hello-world-app", | ||
"version": "1.0.0", | ||
"description": "Hello World Node.js App", | ||
"main": "index.js", | ||
"author": "Your Name", | ||
"license": "MIT", | ||
"dependencies": {} | ||
} | ||
|
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 @@ | ||
BACKGROUND_COLOR=#00ff00 |
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 @@ | ||
# === 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"] | ||
|
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,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) | ||
} | ||
} | ||
|
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,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 |
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,7 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: teste | ||
namespace: default | ||
data: | ||
key: value |
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,7 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: deploy | ||
namespace: default | ||
spec: | ||
replicas: 2 |
Oops, something went wrong.