James Williams
LinkedInMastodonGithub

WWIG - Pastebin

Tags: Grails Groovy

Through random surfing, I came across the site 7days7apps.com. It's an effort to create seven applications using the Python Django framework. I decided to implement the paste bin example for the second edition of WWIG. It's very simple but I think it would be useful to beginners since it's a fully functional Grails application with only one domain class, one controller, one plugin, and ...you guessed it, one taglib(pretty date taglib from GroovyBlogs).

In addition to the basic CRUD operations(1), posts expire after their interval has passed and thanks to google-code-prettify, most languages have syntax highlighting.

Post expiration

class DeleteSnippetJob {
    def timeout = 60000l // execute job once in 60 seconds

    def execute() {
      // execute task
      def oneHourAgo = new Date()
      oneHourAgo.minutes = oneHourAgo.hours - 1;
      def snippets = Snippet.executeQuery("from Snippet s where s.dateCreated < ?", [oneHourAgo])
      for (snippet in snippets) {
        def date = snippet.dateCreated.clone()
        date.hours += Integer.parseInt(snippet.durationHours)
        if (date.before(new Date())) {
           def id = snippet.id
           snippet.delete()
           log.info "deleting snippet ${id}"
        }
      }
    }
}

In the above code, we first find the posts that are at least one hour old (the short snippet duration). After getting that list, we iterate through it and remove the snippets that have expired.

Given that it's a paste bin app, don't expect high design, it is what it is. A simple quick and dirty application. Just in case folks were wondering, the title logo was made in Blender.

Download the source code here.

(1)well actually CR since deletion and updates have been disabled