askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
counter_image.py · 2.3 KB · 93 lines
| 1 | """ |
| 2 | Image Counter Object |
| 3 | |
| 4 | Classic web counter that serves a PNG image with the hit count. |
| 5 | Increments on every request. |
| 6 | |
| 7 | Usage: |
| 8 | <img src="http://localhost:8001/objects/basics_counter_image"> |
| 9 | """ |
| 10 | from PIL import Image, ImageDraw, ImageFont |
| 11 | import io |
| 12 | |
| 13 | |
| 14 | def GET(request): |
| 15 | """Return counter image and increment count""" |
| 16 | |
| 17 | # Get current count (default to 0) |
| 18 | count = _state_manager.get('hits', 0) |
| 19 | |
| 20 | # Increment counter |
| 21 | count += 1 |
| 22 | _state_manager.set('hits', count) |
| 23 | |
| 24 | # Log the hit |
| 25 | _logger.info(f'Counter hit #{count}') |
| 26 | |
| 27 | # Create image with counter |
| 28 | width = 200 |
| 29 | height = 60 |
| 30 | |
| 31 | # Create image with gradient background |
| 32 | img = Image.new('RGB', (width, height), color='#1a1a1a') |
| 33 | draw = ImageDraw.Draw(img) |
| 34 | |
| 35 | # Draw border |
| 36 | draw.rectangle([0, 0, width-1, height-1], outline='#0ea5e9', width=3) |
| 37 | |
| 38 | # Draw "HITS:" label |
| 39 | try: |
| 40 | # Try to use a nice font |
| 41 | font_label = ImageFont.truetype('/System/Library/Fonts/Supplemental/Arial.ttf', 16) |
| 42 | font_count = ImageFont.truetype('/System/Library/Fonts/Supplemental/Arial Bold.ttf', 28) |
| 43 | except: |
| 44 | # Fallback to default font |
| 45 | font_label = ImageFont.load_default() |
| 46 | font_count = ImageFont.load_default() |
| 47 | |
| 48 | # Draw label |
| 49 | label_text = "HITS:" |
| 50 | draw.text((15, 12), label_text, fill='#999999', font=font_label) |
| 51 | |
| 52 | # Draw count |
| 53 | count_text = f"{count:,}" # Format with commas |
| 54 | |
| 55 | # Center the count text |
| 56 | bbox = draw.textbbox((0, 0), count_text, font=font_count) |
| 57 | text_width = bbox[2] - bbox[0] |
| 58 | text_x = (width - text_width) // 2 |
| 59 | |
| 60 | draw.text((text_x, 28), count_text, fill='#0ea5e9', font=font_count) |
| 61 | |
| 62 | # Convert image to bytes |
| 63 | img_bytes = io.BytesIO() |
| 64 | img.save(img_bytes, format='PNG') |
| 65 | img_bytes.seek(0) |
| 66 | content = img_bytes.read() |
| 67 | |
| 68 | # Return image with proper headers |
| 69 | return { |
| 70 | 'status': 'ok', |
| 71 | 'content_type': 'image/png', |
| 72 | 'body': content, |
| 73 | 'count': count |
| 74 | } |
| 75 | |
| 76 | |
| 77 | def POST(request): |
| 78 | """Reset counter""" |
| 79 | action = request.get('action') |
| 80 | |
| 81 | if action == 'reset': |
| 82 | _state_manager.set('hits', 0) |
| 83 | _logger.warning('Counter reset to 0') |
| 84 | |
| 85 | return { |
| 86 | 'status': 'ok', |
| 87 | 'message': 'Counter reset to 0' |
| 88 | } |
| 89 | |
| 90 | return { |
| 91 | 'status': 'error', |
| 92 | 'message': 'Unknown action' |
| 93 | } |