~trufas/trfs.me

561acdbc763226d67a429f9d6ab3bf77c444fed0 — Rafael Castillo 4 months ago
Initial commit

Includes a small golang service for generating a simple static page and
a couple scripts to add random images.
5 files changed, 178 insertions(+), 0 deletions(-)

A bin/addimage
A bin/resize
A server.go
A static/styles.css
A template.html
A  => bin/addimage +18 -0
@@ 1,18 @@
#!/usr/bin/env python3
import os.path
import sys
from pathlib import Path

if len(sys.argv) != 2:
    sys.exit(1)

url = sys.argv[1]

paths = sorted(Path('./rawimages').iterdir(), key=os.path.getmtime)
filename = os.path.basename(paths[-1])

filecount = max(int(x) for x in os.listdir('./manifests')) + 1
with open('./manifests/{}'.format(filecount), 'w') as fp:
    fp.write(filename)
    fp.write('\n')
    fp.write(url)

A  => bin/resize +7 -0
@@ 1,7 @@
#!/bin/bash

for image in ./rawimages/*; do
  echo $image
  convert $image -resize 500x500\> ${image/rawimages/images}
done


A  => server.go +87 -0
@@ 1,87 @@
package main

import (
	"bufio"
	"errors"
	"flag"
	"html/template"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

type App struct {
	manifestPath *string
	imageRoot    *string
	template     *template.Template
}

type Image struct {
	Filename string
	Source   string
}

func readManifest(path string) (*Image, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	scanner := bufio.NewScanner(file)
	scanner.Scan()
	filename := scanner.Text()
	scanner.Scan()
	source := scanner.Text()

	return &Image{filename, source}, nil
}

func (app *App) randomImage() (*Image, error) {
	files, err := ioutil.ReadDir(*app.manifestPath)
	if err != nil {
		return nil, err
	}

	if len(files) == 0 {
		return nil, errors.New("No images available")
	}

	path := files[rand.Intn(len(files))]

	return readManifest(filepath.Join(*app.manifestPath, path.Name()))
}

func (app *App) handleRequest(w http.ResponseWriter, r *http.Request) {
	image, err := app.randomImage()
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Printf("ERROR: %s", err.Error())
		w.Write([]byte("500 - Something bad happened!"))
		return
	}

	app.template.Execute(w, image)
}

func main() {
	app := App{
		manifestPath: flag.String("manifestpath", "./manifests", "path where images live"),
		imageRoot:    flag.String("root", "/images", "path where images are served"),
	}

	t := template.Must(template.ParseFiles("template.html"))
	app.template = t

	port := flag.Int("port", 4000, "port to listen on")

	flag.Parse()

	http.Handle("/static/", http.StripPrefix(strings.TrimRight("/static", "/"), http.FileServer(http.Dir("./static"))))
	http.Handle("/images/", http.StripPrefix(strings.TrimRight("/images", "/"), http.FileServer(http.Dir("./images"))))
	http.HandleFunc("/", app.handleRequest)
	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
}

A  => static/styles.css +34 -0
@@ 1,34 @@
body {
  margin: 0;
  width: 100%;
  font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
  background-color: #EAE7C4;
}
.box {
  padding: 1rem;
  background-color: #F5F5F5;
  width: 100%;
  box-sizing: border-box;
}
.source {
  font-size: 0.75rem;
  text-align: center;
}
.container {
  padding: 0.5rem;
  margin: 0 auto;
  margin-top: 50px;
  max-width: 400px;
  width: 100%;
  box-sizing: border-box;
}

.image {
  max-width: 100%;
  height: auto;
}
@media only screen and (max-width: 400px) {
  .container {
    margin-top: 0;
  }
}

A  => template.html +32 -0
@@ 1,32 @@
<!DOCTYPE html>
<html>
  <head>
    <title>Hi</title>
    <link rel="stylesheet" type="text/css" href="static/styles.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <div class="container">
      <div class="box">
        <img src="images/{{.Filename}}" alt="Random image" class="image"/>
        <div class="source">
          <span>Source:</span>
          <a href="{{.Source}}">{{.Source}}</a>
        </div>
        <h2>Hi! You're cute</h2>
        <p>
          I'm Rafael Castillo, a professional programmer and aspiring sysadmin based out of Hermosillo, Sonora, Mexico. If you need to contact me for any reason at all you can do so through the channels below.
        </p>
        <p>
          <strong>Email: </strong>
          <span>rafael@trfs.me</span>
        </p>
        <p>
          <strong>Matrix: </strong>
          <span>@trufas:matrix.org</span>
        </p>
        <p>Hope to see you around!</p>
      </div>
    </div>
  </body>
</html>