↓Skip to main content

Word Search and Anagram Finder

·3 mins

On most days I attempt the Guardian Cryptic Crossword. On most days I also get stuck and need to look up the words matching a pattern or solve an anagram I can’t work out with pencil and paper. I’m not usually at the computer when doing the crossword so I wanted something I could access from my phone - I used to use an Android app for this, but the ads became too frustrating. So a few months ago I wrote a tiny web app: https://github.com/ray1729/wordsearch. This was also an excuse to try out htmx, although to be fair it’s too simple an app to really demonstrate the power of htmx.

I originally deployed this to Google Cloud: a storage bucket for the static components, and a cloud function to handle the search requests. This was costing about £0.04 a month (I was probably the only user, so it won’t have seen much load). In March this year I moved my main web hosting back to Mythic Beasts and I wondered if I could host a Go web application there. It turns out the answer is yes! Although their documentation focuses on Python hosting the technique works for any server that can listen on a Unix domain socket. So all I had to do was update the standalone version of my wordsearch server to listen on a Unix domain socket. This turned out to be straightforward: instead of calling http.ListenAndServe() you create a net.Listener with network type unix and pass that to the HTTP server:

func main() {
	var listenAddr = flag.String("listen", ":8000", "Address or port to listen on, prefix with unix: to listen on a Unix domain socket")
	flag.Parse()

	server := http.Server{
		Handler: server.New(matchDB, anagramDB),
	}

	var listener net.Listener
	var err error
	if strings.HasPrefix(*listenAddr, "unix:") {
		listener, err = net.Listen("unix", strings.TrimPrefix(*listenAddr, "unix:"))
	} else {
		listener, err = net.Listen("tcp", *listenAddr)
	}
	if err != nil {
		log.Fatalf("error listening on %s: %v", *listenAddr, err)
	}
	log.Printf("Listening on %s", *listenAddr)
	if err := server.Serve(listener); err != nil {
		log.Fatal(err)
	}
}

A tiny gotcha: the Mythic Beasts host has a different version of GLIBC from my home system, so I had to build the binary with CGO_ENABLED=0:

env CGO_ENABLED=0 go build -o wordsearch ./standalone/main.go 

I like to automate my server management with ansible. As well as automating the setup, this serves as useful documentation - very helpful to someone as forgetful as myself! Here’s the ansible playbook to set up the wordsearch server and proxy configuration for Mythic Beasts hosting. The file files/bin/wordsearch is the binary output by the above go build command.

---
- hosts: bobcat
  become: false
  tasks:
    - name: Create bin directory
      ansible.builtin.file:
        path: "{{ansible_user_dir}}/bin"
        state: directory
    - name: Install wordsearch binary
      ansible.builtin.copy:
        dest: "{{ansible_user_dir}}/bin/wordsearch"
        src: files/bin/wordsearch
        mode: 0755
    - name: Create systemd user directory
      ansible.builtin.file:
        path: "{{ansible_user_dir}}/.config/systemd/user"
        state: directory
    - name: Create wordsearch unit file
      ansible.builtin.copy:
        dest: "{{ansible_user_dir}}/.config/systemd/user/wordsearch.service"
        content: |
          [Unit]
          Description=Wordsearch Server

          [Service]
          Type=simple
          StandardOutput=journal
          ExecStart=%h/bin/wordsearch --listen unix:%h/www/wordsearch.1729.org.uk/proxy.sock
          Restart=always
          RestartSec=1

          [Install]
          WantedBy=default.target
      notify:
        - Reload systemd
        - Configure wordsearch service
    - name: Configure proxy
      ansible.builtin.file:
        path: "{{ansible_user_dir}}/www/wordsearch.1729.org.uk/.proxy-to-socket"
        state: touch
  handlers:
    - name: Reload systemd
      ansible.builtin.systemd_service:
        scope: user
        daemon_reload: true
    - name: Configure wordsearch service
      ansible.builtin.systemd_service:
        scope: user
        name: wordsearch.service
        enabled: true
        state: restarted

If you find yourself stuck on a crossword, you can find the running service at https://wordsearch.1729.org.uk/.

Note: the output results include a link to the definition in Dicoweb, a web-based version of the GNU Dictionary Server, but at the time of writing that server is down.