This commit is contained in:
Ole-Morten Duesund 2025-06-06 18:50:32 +02:00
commit 7640f045f1
6 changed files with 448 additions and 0 deletions

34
Containerfile Normal file
View file

@ -0,0 +1,34 @@
# Use official Ruby image
FROM ruby:3.2-slim
# Set working directory
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Copy Gemfile and Gemfile.lock
COPY Gemfile* ./
# Install Ruby gems
RUN bundle install --without development
# Copy application code
COPY . .
# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser appuser
RUN chown -R appuser:appuser /app
USER appuser
# Expose port
EXPOSE 4567
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:4567/health || exit 1
# Default command
CMD ["ruby", "app.rb"]

9
Gemfile Normal file
View file

@ -0,0 +1,9 @@
source 'https://rubygems.org'
gem 'sinatra', '~> 3.0'
gem 'puma', '~> 6.0'
gem 'json', '~> 2.6'
group :development do
gem 'rerun'
end

62
Gemfile.lock Normal file
View file

@ -0,0 +1,62 @@
GEM
remote: https://rubygems.org/
specs:
base64 (0.3.0)
ffi (1.17.2)
ffi (1.17.2-aarch64-linux-gnu)
ffi (1.17.2-aarch64-linux-musl)
ffi (1.17.2-arm-linux-gnu)
ffi (1.17.2-arm-linux-musl)
ffi (1.17.2-arm64-darwin)
ffi (1.17.2-x86-linux-gnu)
ffi (1.17.2-x86-linux-musl)
ffi (1.17.2-x86_64-darwin)
ffi (1.17.2-x86_64-linux-gnu)
ffi (1.17.2-x86_64-linux-musl)
json (2.12.2)
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mustermann (3.0.3)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.4)
puma (6.6.0)
nio4r (~> 2.0)
rack (2.2.17)
rack-protection (3.2.0)
base64 (>= 0.1.0)
rack (~> 2.2, >= 2.2.4)
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
rerun (0.14.0)
listen (~> 3.0)
ruby2_keywords (0.0.5)
sinatra (3.2.0)
mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4)
rack-protection (= 3.2.0)
tilt (~> 2.0)
tilt (2.6.0)
PLATFORMS
aarch64-linux-gnu
aarch64-linux-musl
arm-linux-gnu
arm-linux-musl
arm64-darwin
ruby
x86-linux-gnu
x86-linux-musl
x86_64-darwin
x86_64-linux-gnu
x86_64-linux-musl
DEPENDENCIES
json (~> 2.6)
puma (~> 6.0)
rerun
sinatra (~> 3.0)
BUNDLED WITH
2.6.7

120
README.md Normal file
View file

@ -0,0 +1,120 @@
# No as a Service (NaaS)
A simple Sinatra-based web service that provides creative "no" responses in both HTML and JSON formats.
## Features
- **Dual Format Support**: Serves both HTML (for browsers) and JSON (for APIs)
- **Random Responses**: Returns a random "no" response from a curated list
- **Modern Styling**: Beautiful glassmorphism design for the HTML interface
- **Health Checks**: Built-in health check endpoint for monitoring
- **Containerized**: Ready for deployment with Podman/Docker
## API Endpoints
- `GET /` - Returns a random "no" (HTML by default, JSON with Accept header)
- `GET /api/no` - Explicit JSON endpoint with additional metadata
- `GET /health` - Health check endpoint
- `GET /*` - Catch-all that returns "no" for any other path
## Content Negotiation
The root endpoint (`/`) supports content negotiation:
- Browser requests get HTML
- Requests with `Accept: application/json` get JSON
## Usage Examples
### HTML (Browser)
```
curl http://localhost:4567/
```
### JSON
```bash
# Using Accept header
curl -H "Accept: application/json" http://localhost:4567/
# Using explicit API endpoint
curl http://localhost:4567/api/no
```
### Sample JSON Response
```json
{
"answer": "Absolutely not",
"timestamp": "2025-06-06T12:00:00Z",
"service": "No as a Service",
"version": "1.0.0"
}
```
## Local Development
### Prerequisites
- Ruby 3.2+
- Bundler
### Setup
```bash
# Install dependencies
bundle install
# Run the application
ruby app.rb
# Or use rerun for auto-reload during development
bundle exec rerun ruby app.rb
```
The application will be available at `http://localhost:4567`
## Container Deployment
### Build with Podman
```bash
podman build -t naas .
```
### Run with Podman
```bash
# Basic run
podman run -p 4567:4567 naas
# Run in background with restart policy
podman run -d --name naas-service -p 4567:4567 --restart=always naas
# Run with custom port
podman run -p 8080:4567 -e PORT=4567 naas
```
### Docker Commands
The same commands work with Docker by replacing `podman` with `docker`.
## Environment Variables
- `PORT` - Port to bind to (default: 4567)
## Health Monitoring
The service includes a health check endpoint at `/health` that returns:
```json
{
"status": "healthy",
"timestamp": "2025-06-06T12:00:00Z"
}
```
## File Structure
```
.
├── app.rb # Main Sinatra application
├── Gemfile # Ruby dependencies
├── Containerfile # Container build instructions
└── README.md # This file
```
## License
This project is released under the MIT License. Feel free to use it for your negative response needs!

11
TLDR.md Normal file
View file

@ -0,0 +1,11 @@
# Create project directory
mkdir no-as-a-service
cd no-as-a-service
# Create the files above, then:
bundle install
ruby app.rb
# Or build container:
podman build -t naas .
podman run -p 4567:4567 naas

212
app.rb Normal file
View file

@ -0,0 +1,212 @@
require 'sinatra'
require 'json'
# Configure Sinatra
set :port, ENV['PORT'] || 4567
set :bind, '0.0.0.0'
# Array of creative "no" responses
NO_RESPONSES = [
"No",
"Nope",
"Absolutely not",
"Not a chance",
"Never",
"No way",
"Negative",
"Not happening",
"Nah",
"No siree",
"Not today",
"Dream on",
"Over my dead body",
"When pigs fly",
"Not in a million years",
"Forget about it",
"No dice",
"Not on your life",
"Fat chance",
"No can do"
].freeze
# Helper method to get a random "no"
def get_no_response
NO_RESPONSES.sample
end
# Root endpoint - returns HTML by default
get '/' do
no_response = get_no_response
case request.accept.first&.to_s
when /application\/json/
content_type :json
{ answer: no_response, timestamp: Time.now.iso8601 }.to_json
else
content_type :html
erb :index, locals: { no_response: no_response }
end
end
# Explicit JSON endpoint
get '/api/no' do
content_type :json
{
answer: get_no_response,
timestamp: Time.now.iso8601,
service: "No as a Service",
version: "1.0.0"
}.to_json
end
# Health check endpoint
get '/health' do
content_type :json
{ status: "healthy", timestamp: Time.now.iso8601 }.to_json
end
# Catch-all route for any other endpoint
get '/*' do
no_response = get_no_response
case request.accept.first&.to_s
when /application\/json/
content_type :json
{ answer: no_response, timestamp: Time.now.iso8601 }.to_json
else
content_type :html
erb :index, locals: { no_response: no_response }
end
end
__END__
@@index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>No as a Service</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.container {
text-align: center;
max-width: 600px;
padding: 2rem;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
border: 1px solid rgba(255, 255, 255, 0.2);
}
h1 {
font-size: 3rem;
margin-bottom: 1rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.subtitle {
font-size: 1.2rem;
margin-bottom: 2rem;
opacity: 0.9;
}
.no-response {
font-size: 4rem;
font-weight: bold;
color: #ff6b6b;
text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.5);
margin: 2rem 0;
padding: 1rem;
background: rgba(255, 255, 255, 0.1);
border-radius: 15px;
border: 2px solid rgba(255, 107, 107, 0.3);
}
.refresh-btn {
background: linear-gradient(45deg, #ff6b6b, #ff8e8e);
color: white;
border: none;
padding: 1rem 2rem;
font-size: 1.1rem;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
}
.refresh-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
}
.api-info {
margin-top: 2rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.05);
border-radius: 10px;
font-size: 0.9rem;
}
.api-info code {
background: rgba(0, 0, 0, 0.2);
padding: 0.2rem 0.5rem;
border-radius: 4px;
font-family: 'Courier New', monospace;
}
@media (max-width: 600px) {
.container {
margin: 1rem;
padding: 1.5rem;
}
h1 {
font-size: 2rem;
}
.no-response {
font-size: 2.5rem;
}
}
</style>
</head>
<body>
<div class="container">
<h1>No as a Service</h1>
<p class="subtitle">The definitive API for negative responses</p>
<div class="no-response">
<%= no_response %>
</div>
<button class="refresh-btn" onclick="location.reload()">
Get Another No
</button>
<div class="api-info">
<h3>API Usage</h3>
<p>JSON API: <code>GET /api/no</code></p>
<p>Accept JSON: <code>curl -H "Accept: application/json" /</code></p>
<p>Health Check: <code>GET /health</code></p>
</div>
</div>
</body>
</html>