Initial commit

This commit is contained in:
Weetile 2024-10-21 13:43:01 +01:00
commit 621314b7bc
5 changed files with 2528 additions and 0 deletions

32
.github/workflows/docker-publish.yml vendored Normal file
View File

@ -0,0 +1,32 @@
name: Build and Publish Multi-Architecture Image
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push multi-platform image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/fastfact:latest

18
Dockerfile Normal file
View File

@ -0,0 +1,18 @@
FROM golang:alpine AS builder
WORKDIR /app
COPY go.mod ./
COPY facts.txt ./
COPY main.go ./
RUN go build -o fastfact .
FROM alpine:latest
COPY --from=builder /app/fastfact /usr/local/bin/fastfact
EXPOSE 8080
CMD ["fastfact"]

2395
facts.txt Normal file

File diff suppressed because it is too large Load Diff

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module fastfact
go 1.23.2

80
main.go Normal file
View File

@ -0,0 +1,80 @@
package main
import (
"embed"
"fmt"
"math/rand"
"net/http"
"strings"
)
//go:embed facts.txt
var facts embed.FS
func loadFacts() ([]string, error) {
// Read the embedded file
data, err := facts.ReadFile("facts.txt")
if err != nil {
return nil, err
}
// Split the file into lines and return as a slice
lines := string(data)
return splitLines(lines), nil
}
func splitLines(data string) []string {
return strings.Split(strings.TrimSpace(data), "\n")
}
func randomFactHandler(facts []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Get a random index
index := rand.Intn(len(facts))
// Write the random fact as an HTML response with CSS
fmt.Fprintf(w, `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Fact</title>
<style>
body {
background-color: #24273a;
color: #8aadf4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-size: 24px; /* Increase text size */
font-weight: bold;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<p>%s</p>
</body>
</html>
`, facts[index])
}
}
func main() {
// Load facts from the embedded file
facts, err := loadFacts()
if err != nil {
panic(err)
}
// Set up the HTTP server
http.HandleFunc("/", randomFactHandler(facts))
fmt.Println("Server is running on http://localhost:8080")
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}