小狼阳帆

WolfYangFan Blog

你好,我是小狼阳帆,很高兴认识你!
email
misskey

Random Git commit messages, but Cloudflare Workers

You may have used this amazing site, What The Commit. It helps us randomly generate Git commit messages, saving us the headache of "What the hell did I write?"

When using Git, we can do it like this:

git commit -m $(curl -s http://whatthecommit.com/index.txt)

Feeling that this website is interesting, I decided to copy implement one myself.

The website is open source, and the address is: ngerakines/commitment. Since there is source data, it's easy to handle. Here, I will use Cloudflare Workers to simply implement the logic (I haven't written a CFW script in a long time, so I hope the experts go easy on me):

import HTML from './index.html'

const default_names = [
  "Ali",
  "Andy",
  "April",
  "Brannon",
  "Chris",
  ...
]

const commit_messages = [
\\_(ツ)_/¯",
  "\"Get that shit outta my master.\"",
  "#GrammarNazi",
  "$(init 0)",
  "$(rm -rvf .)",
  "(c) Microsoft 1988",
  "--help",
  ...
]

export default {
  async fetch(request) {
    const url = new URL(request.url);
    var commit_message = commit_messages[Math.floor(Math.random() * commit_messages.length)]
      .replace(/XNAMEX/g, default_names[Math.floor(Math.random() * default_names.length)])
      .replace(/XUPPERNAMEX/g, default_names[Math.floor(Math.random() * default_names.length)].toUpperCase())
      .replace(/XLOWERNAMEX/g, default_names[Math.floor(Math.random() * default_names.length)].toLowerCase());

    switch (url.pathname) {
      case '/':
      case '/index.html':
      case '/index.php':
        var OutputHTML = HTML.replace(/%message%/g, commit_message);
        return new Response(OutputHTML, {
          status: 200,
          headers: {
            'Content-Type': 'text/html;charset=UTF-8',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Headers': 'Origin,X-Requested-With,Content-Type,Accept',
          }
        });

      case '/index.txt':
        return new Response(commit_message, {
          status: 200,
          headers: {
            'Content-Type': 'text/plain;charset=UTF-8',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Headers': 'Origin,X-Requested-With,Content-Type,Accept',
          }
        });

      case '/robots.txt':
        return new Response(
          `User-agent: *
Disallow: /`, {
          status: 200,
          headers: {
            'Content-Type': 'text/plain;charset=UTF-8',
          }
        });

      default:
        return new Response(`not found`, {
          status: 404,
          headers: {
            'Content-Type': 'text/plain;charset=UTF-8',
          }
        });
    }
  }
}

As you can see, our core code is very simple. As for deployment, everyone surely knows how to do it. So, is this article over?

If you want to use my instance, feel free to try it:

git commit -m $(curl -s https://wtc.993686.xyz/index.txt)
# There is a chance of triggering WAF, and if the request volume is high, it may interrupt the service. It is recommended to deploy it yourself.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.