Bartender.pm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package Bartender;
  2. use Dancer2;
  3. use Data::Dumper;
  4. use POSIX ();
  5. our $VERSION = '0.1';
  6. die "config.yml not loaded" unless defined config->{appname};
  7. get '/' => sub {
  8. return "Welcome on " . config->{appname};
  9. };
  10. get '/shake' => sub {
  11. my %param = params;
  12. my $error = {};
  13. my $opt_dossier = $param{dossier};
  14. my $opt_base = $param{projetPadPrincipal};
  15. my $opt_garde = $param{projetPadGarde};
  16. my $opt_projet = $param{projetId};
  17. for($opt_dossier, $opt_projet) {
  18. if( m{[^a-zA-Z0-9_-]} ) {
  19. $error->{message} .= qq{"$_" est incorrect.};
  20. }
  21. }
  22. for($opt_base, $opt_garde) {
  23. if(not m/^https?:\/\/pad\.exegetes\.eu\.org\/p\//i ) {
  24. $error->{message} .= qq{"$_" est incorrect.};
  25. }
  26. }
  27. my $cocktail_binary = config->{cocktail}{binary};
  28. if(not defined $cocktail_binary) {
  29. $error->{message} .= qq{"cocktail:binary" n'est pas configuré dans config.yml};
  30. }
  31. elsif(not -x $cocktail_binary) {
  32. $error->{message} .= qq{"cocktail:binary" "$cocktail_binary" n'est pas executable};
  33. }
  34. if($error->{message}) {
  35. return template 'error', $error;
  36. }
  37. system("$cocktail_binary -d $opt_dossier -b '$opt_base' -g '$opt_garde' -p $opt_projet &");
  38. redirect request->referer;
  39. };
  40. get '/status' => sub {
  41. my %param = params;
  42. my $error = {};
  43. my $opt_dossier = $param{dossier};
  44. my $opt_projet = $param{projetId};
  45. my $cocktail_store = config->{cocktail}{store};
  46. if(not defined $cocktail_store) {
  47. $error->{message} .= qq{"cocktail:store" n'est pas configuré dans config.yml};
  48. }
  49. if(not defined $opt_projet) {
  50. $error->{message} .= qq{arg:projetId est incorrect};
  51. }
  52. if($error->{message}) {
  53. return template 'error', $error;
  54. }
  55. my $lock_filename = "$cocktail_store/$opt_dossier/$opt_projet.lock";
  56. my $pdf_filename = "$cocktail_store/$opt_dossier/$opt_projet.pdf";
  57. my ($compilation_status, $compilation_text);
  58. if(-e $lock_filename) {
  59. my $date_time = _get_datetime_for($lock_filename);
  60. $compilation_text = "En cours de compilation depuis $date_time";
  61. $compilation_status = 'IN PROGRESS';
  62. }
  63. elsif(-e $pdf_filename) {
  64. my $date_time = _get_datetime_for($pdf_filename);
  65. $compilation_text = $date_time;
  66. $compilation_status = 'COMPLETED';
  67. }
  68. else {
  69. $compilation_text = "Aucune compilation.";
  70. $compilation_status = 'NONE';
  71. }
  72. header 'Content-Type' => 'application/json';
  73. return to_json { text => $compilation_text, status => $compilation_status };
  74. };
  75. sub _get_datetime_for {
  76. my $filename = shift;
  77. return unless -e $filename;
  78. my @stat = stat $filename;
  79. my $ctime = $stat[10];
  80. my $date_time = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($ctime) );
  81. return $date_time;
  82. }
  83. true;