Compare commits
11 Commits
feature/te
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c41f06b71 | |||
| 3e3f0affc4 | |||
| 32c2e25fb6 | |||
| b5d9e10786 | |||
| cd11a4113f | |||
| e6c65689ab | |||
| 442a22b07b | |||
| 50f6ea7fad | |||
| eebb31becb | |||
| b3d6e53e58 | |||
| 1571f2a25e |
31
.gitea/workflows/build-container.yaml
Normal file
31
.gitea/workflows/build-container.yaml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: Build Cat Web App Container
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: podman
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
run: |
|
||||||
|
git clone https://gitea.montgomeryspot.com/adam/cat-memes.git
|
||||||
|
cd cat-memes
|
||||||
|
|
||||||
|
- name: Build container image
|
||||||
|
run: |
|
||||||
|
cd cat-memes
|
||||||
|
podman build -f Containerfile -t adam/cat-memes:latest .
|
||||||
|
|
||||||
|
# Optional: Push to registry
|
||||||
|
# - name: Push to registry
|
||||||
|
# env:
|
||||||
|
# REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
|
||||||
|
# REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }}
|
||||||
|
# run: |
|
||||||
|
# podman login myregistry.example.com -u $REGISTRY_USER -p $REGISTRY_PASS
|
||||||
|
# podman tag myorg/catapp:latest myregistry.example.com/myorg/catapp:latest
|
||||||
|
# podman push myregistry.example.com/myorg/catapp:latest
|
||||||
|
|
||||||
13
Containerfile
Normal file
13
Containerfile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# Stage 1: Build the Go binary
|
||||||
|
FROM golang:1.21-alpine AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
COPY . .
|
||||||
|
RUN go build -o catapp .
|
||||||
|
|
||||||
|
# Stage 2: Minimal runtime image
|
||||||
|
FROM alpine:latest
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder /app/catapp .
|
||||||
|
EXPOSE 8080
|
||||||
|
CMD ["./catapp"]
|
||||||
|
|
||||||
126
main.go
Normal file
126
main.go
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CatAPIResponse []struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var tpl = template.Must(template.New("index").Parse(`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Random Cat Meme</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 100vh;
|
||||||
|
background: #f2f2f2;
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
#cat-container {
|
||||||
|
width: 1000px;
|
||||||
|
height: 1000px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
background: white;
|
||||||
|
border-radius: 12px;
|
||||||
|
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
#catImage {
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 100%;
|
||||||
|
transition: transform 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
#catImage:hover {
|
||||||
|
transform: scale(1.05);
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
padding: 10px 20px;
|
||||||
|
font-size: 16px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
background-color: #ff6f61;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
background-color: #ff3b2f;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
async function getCat() {
|
||||||
|
const res = await fetch('/cat');
|
||||||
|
const data = await res.json();
|
||||||
|
const img = document.getElementById('catImage');
|
||||||
|
img.src = data.url;
|
||||||
|
}
|
||||||
|
window.onload = getCat;
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Random Cat Meme</h1>
|
||||||
|
<div id="cat-container">
|
||||||
|
<img id="catImage" src="" alt="Random Cat Meme">
|
||||||
|
</div>
|
||||||
|
<button onclick="getCat()">Get a new cat</button>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`))
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tpl.Execute(w, nil)
|
||||||
|
})
|
||||||
|
|
||||||
|
http.HandleFunc("/cat", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
resp, err := http.Get("https://api.thecatapi.com/v1/images/search")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to fetch cat image", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var catResp CatAPIResponse
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&catResp); err != nil {
|
||||||
|
http.Error(w, "Failed to decode API response", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(catResp) == 0 {
|
||||||
|
http.Error(w, "No cat image found", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"url": catResp[0].URL})
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Println("Starting server on :8080")
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user