Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
INitial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Bill Maxwell <[email protected]>
  • Loading branch information
cloudnautique committed Dec 14, 2023
0 parents commit 22df23c
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish Acorn image
on:
push:
tags:
- "v[0-9]*"

jobs:
publish:
name: Publish
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: acorn-io/actions-setup@v2
with:
acorn-version: "main"
- name: Login to GHCR
uses: acorn-io/actions-login@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Publish Acorn image
run: |
TAG=${GITHUB_REF#refs/*/}
USER=${{ github.repository_owner }}
REPO=${{ github.event.repository.name }}
acorn build --platform linux/amd64 --push -t ghcr.io/$USER/$REPO:$TAG .
acorn build --platform linux/amd64 --push -t ghcr.io/$USER/$REPO/examples:$TAG examples
62 changes: 62 additions & 0 deletions Acornfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
services: mongo: {
generated: job: "create"
default: true
}

jobs: create: {
image: "alpine"
env: {
"secret://config": ""
}
dirs: {
"/acorn/scripts": "./scripts"
}
entrypoint: ["/acorn/scripts/render.sh"]
}

secrets: user: {
type: "generated"
params: job: "create"
}

secrets: admin: {
type: "generated"
params: job: "create"
}

secrets: config: {
type: "credential.acorn.io/mongodb"
params: {
instructions: localData.instructions
promptOrder: ["proto", "address", "port", "adminUsername", "adminPassword", "username", "password"]
}
data: {
address: ""
port: ""
username: ""
password: ""
adminUsername: ""
adminPassword: ""
proto: ""
dbName: ""
}
}

localData: instructions: """
## Overview

This will create the service from an existing MongoDB so that it can be swapped with the MongoDB container service.

## Instructions

fill in:

- address: the address of the MongoDB server (mongo.example.com)
- port: the port of the MongoDB server (27017)
- username: the username to use to connect to the MongoDB server
- password: the password to use to connect to the MongoDB server
- adminUsername: the username to use to connect to the MongoDB server as an admin
- adminPassword: the password to use to connect to the MongoDB server as an admin
- proto: the protocol to use to connect to the MongoDB server (mongodb or mongodb+srv)
- dbName: the name of the database to use
"""
33 changes: 33 additions & 0 deletions examples/Acornfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//profiles: prod: {
//image: images.prod.image
//}

services: db: {
//image: args.image
build: {
context: "../"
acornfile: "Acornfile"
}
}

containers: {
app: {
build: {
context: "."
dockerfile: "./Dockerfile"
target: "dev"
}
consumes: ["db"]
ports: publish: "8000/http"
env: {
DB_HOST: "@{service.db.address}"
DB_NAME: "@{service.db.data.dbName}"
DB_PROTO: "@{service.db.data.proto}"
DB_USER: "@{service.db.secrets.user.username}"
DB_PASS: "@{service.db.secrets.user.password}"
}
}
}

//images: dev: image: "ghcr.io/acorn-io/mongodb:v#.#-#"
//images: prod: image: "ghcr.io/cloudnautique/mongodb-generic:v#.#.#"
15 changes: 15 additions & 0 deletions examples/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.9 as base
WORKDIR /app

FROM base as build
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY . .

FROM build as dev
EXPOSE 8000
CMD ["uvicorn", "main:app", "--reload", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers"]

FROM build as production
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Binary file added examples/images/ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions examples/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pymongo import MongoClient

app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")

DB_HOST = os.environ.get('DB_HOST')
DB_PORT = os.environ.get('DB_PORT', '27017')
DB_USER = os.environ.get('DB_USER')
DB_PASS = os.environ.get('DB_PASS')
DB_NAME = os.environ.get('DB_NAME')
DB_PROTO = os.environ.get('DB_PROTO', 'mongodb+svr')

# Default mongo connection string
MONGO_URI = f"{DB_PROTO}://{DB_USER}:{DB_PASS}@{DB_HOST}/{DB_NAME}?authSource=admin"

if DB_PROTO == "mongodb":
MONGO_URI = f"{DB_PROTO}://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}?authSource=admin"

client = MongoClient(MONGO_URI)
db = client[DB_NAME]
collection = db.hits_collection

@app.get('/', response_class=HTMLResponse)
async def hello(request: Request):
# If the counter does not exist, we initialize it with a value of 0
counter_data = collection.find_one({"_id": "page_counter"})
if not counter_data:
collection.insert_one({"_id": "page_counter", "count": 0})
counter = 0
else:
counter = counter_data["count"]

# Increment the counter
collection.update_one({"_id": "page_counter"}, {"$inc": {"count": 1}})
counter += 1

return templates.TemplateResponse("index.html", {"request": request, "counter": counter})
4 changes: 4 additions & 0 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fastapi == 0.101.0
uvicorn[standard] == 0.23.2
pymongo[srv]==3.12.0
Jinja2 == 3.1.2
Binary file added examples/static/acorn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions examples/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
html, body {
height: 100%;
margin: 0;
padding: 0;
background: #483285;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
color: white;
font-family: Arial, sans-serif;
}
.content {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0;
padding: 0;
}
img {
width: 100%;
max-width: 100%;
height: auto;
margin-bottom: 20px;
}
.counter {
font-size: 4rem;
width: 100%;
margin-top: 0;
}
16 changes: 16 additions & 0 deletions examples/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stylish Counter</title>
<link href="/static/style.css" rel="stylesheet">
</head>
<body>
<div class="content">
<img src="/static/acorn.png" alt="Acorn logo" />
<div class="counter">Page views: {{counter}}</div>
</div>
</body>
</html>
30 changes: 30 additions & 0 deletions scripts/render.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

cat > /run/secrets/output<<EOF
services: mongo: {
address: "${address}"
default: true
secrets: ["admin", "user"]
ports: "${port}"
data: {
proto: "${proto}"
dbName: "${dbName}"
}
}
secrets: admin: {
type: "basic"
data: {
username: "${adminUsername}"
password: "${adminPassword}"
}
}
secrets: user: {
type: "basic"
data: {
username: "${username}"
password: "${password}"
}
}
EOF

0 comments on commit 22df23c

Please sign in to comment.