Simple Static Web Server

April 9, 2018
go golang web application letsencrypt

$ ssws

I needed something simple to host this site from. I wanted TLS by default and no crazy heavy web servers. Since I’m using hugo, I just want to push my content and be done with it. Caddyserver looks nice, but not so lightweight, and things like https://github.com/mholt/caddy/issues/2092 always concern me. So I figured why not just write my own that does /exactly/ what I need it to do. So I wrote and published ssws. It’s simple, very simple, and it uses letsencrypt to handle all that TLS business. Now I can just serve my files and be done with it. I’ve included a simple install script that creates a service user, allows that user to bind on port 80443 and enables syslog for it.

$ automate

So I’m a windows person, but I still want to be able to automate deploying my edits in hugo to this site. I tried finding a windows rsync client, but there doesn’t seem to be a good one. Instead I decided to just setup this site as a git server and run git hooks on commit to have it automate the deploy.

$ setup

I downloaded and installed hugo. Ran: hugo new site idawson.me and grabbed the minimal theme. Updated my config.toml and started writing some posts.

$ git

To automate deployment of my edits, I figured I’d just setup git on this host and have a post-receive hook publish it directly to the ssws directory. Setting up git on the server was extremely straightforward. I basically just followed the Setting Up The Server Guide. Don’t forget to replace the git user’s shell with git-shell and disable password auth from the /etc/ssh/sshd_config. I created a new ssh key specifically for deployment using ssh-keygen and copied the key to my windows box, and added the public part to the /home/git/.ssh/authorized_keys file. I needed to setup my ssh config in windows to get git to use the proper key.

Host idawson.me
    Hostname idawson.me
    User git
    IdentityFile C:/path/to/my/key

For git to be able to copy the files to the ssws static directory I needed to add the git user to the group and set group permissions on the static directory. So a little bit of this:

sudo chown sswsservice:sswsservice /opt/ssws/static
sudo chmod 775 /opt/ssws/static // give group rwx permissions
sudo usermod -a -G sswsservice git // add git user to sswsservice group.

So now git can write to the static directory. But I still need to setup a repository and create the post-receive hook.

sudo mkdir -p /srv/git
sudo chown -r git:git /srv/git
su git -s /bin/bash
mkdir /srv/git/site.git
cd !$
git init --bare
touch hooks/post-receive
chmod +x hooks/post-receive

Next up is adding the hook to deploy. I used this wonderful gist as a template. Mine looks like:

#!/bin/bash

DEPLOY_PATH="/opt/ssws/static/"
GIT_PATH="/srv/git/site.git/"

while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/master$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        git --work-tree=${DEPLOY_PATH} --git-dir=${GIT_PATH} checkout -f
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done

Now any time a push to the master branch it will deploy the static content directly to the static directory \o/.

Now that my site has a repository, I can switch back to windows and clone it:

C:\site\idawson.me\>git clone git@idawson.me:/srv/git/site.git public
Cloning into 'public'...
Enter passphrase for key 'C:/path/to/my/key':
warning: You appear to have cloned an empty repository.

C:\site\idawson.me\>

When hugo builds your site it will add the static files to the public directory (hence why I created that as my repository directory). At this time I’m ready to do my first commit.

$ workflow

Here’s how I update my blog. I add a new post using C:\site\idawson.me\>hugo new posts/whatever.md and run the server with the minimal template to monitor my changes: hugo server -D -t minimal. Once finished, I generate the static data using hugo -t minimal and change into the public directory. Then just run git add . and git push. After the push, the hook runs and copies the file over which my ssws server will load on request. Simple! Now I can start recording my various activities while I enjoy my year off. Hope this helps someone out there! Until next time…

comments powered by Disqus