#!/usr/bin/perl use strict; use CGI::Carp qw(fatalsToBrowser); use lib qw[/home/wakaba/work/manakai2/lib/]; require 'common.pl'; my $path = $ENV{PATH_INFO}; if ($path eq '/') { if ($ENV{REQUEST_METHOD} eq 'POST') { require Message::CGI::HTTP; require Encode; my $cgi = Message::CGI::HTTP->new; $cgi->{decoder}->{'#default'} = sub { return Encode::decode ('utf-8', $_[1]); }; my $en = $cgi->get_parameter ('en'); my $ja = $cgi->get_parameter ('ja'); my $is_pattern = $cgi->get_parameter ('pattern'); my $tags = [map {normalize ($_)} split /[\x0D\x0A]+/, $cgi->get_parameter ('tags') // '']; my $hash = get_hash ($en); lock_entry ($hash); set_entry ($hash, $is_pattern => {en => $en, ja => $ja, tags => $tags}); commit_entries ("$path: $hash updated by $ENV{AUTH_USER}"); print "Status: 204 Saved\n\n"; exit; } else { print q[Status: 405 Method Not Allowed Content-Type: text/plain ; charset=us-ascii Allow: POST 405]; exit; } } elsif ($path =~ m#^/([0-9a-f]+)\.json$#) { my $hash = $1; my ($is_pattern, $entry) = get_entry ($hash); unless (defined $entry->{en}) { $entry = get_fallback_entry ($hash); } $entry->{tags} ||= [] if defined $entry->{en}; $entry->{isPattern} = 1 if $is_pattern; binmode STDOUT, ':encoding(utf-8)'; print "Content-Type: application/json\n\n"; require JSON; print scalar JSON::objToJson ($entry); exit; } print q[Content-Type: text/plain ; charset=us-ascii Status: 404 Not Found 404];