From 5474598c6bb19c069e7c8d6aa9d9ccec8819abe0 Mon Sep 17 00:00:00 2001 From: Sebastian Schulze Date: Wed, 23 Mar 2022 21:54:12 +0100 Subject: [PATCH] Add Viper configuration and rework the async process --- go.mod | 1 + go.sum | 20 ++++++++++++++ main.go | 67 +++++++++++++++++++++++++++++++++-------------- scan/command.go | 2 ++ scan/process.go | 66 ++++++++++++++++++++++++---------------------- views/scan.html | 11 ++++++-- views/show.html | 6 ++--- views/status.html | 2 +- 8 files changed, 117 insertions(+), 58 deletions(-) diff --git a/go.mod b/go.mod index 392f15a..e27396b 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gofiber/template v1.6.23 // indirect github.com/google/uuid v1.3.0 // indirect github.com/klauspost/compress v1.14.4 // indirect + github.com/spf13/viper v1.10.1 // indirect github.com/valyala/fasthttp v1.33.0 // indirect golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7 // indirect ) diff --git a/go.sum b/go.sum index 368953a..0b16003 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,7 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/flosch/pongo2/v4 v4.0.2/go.mod h1:B5ObFANs/36VwxxlgKpdchIJHMvHB562PW+BWPhwZD8= +github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -214,6 +215,7 @@ github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pf github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0eJc8R6ouapiM= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/consul/api v1.11.0/go.mod h1:XjsvQN+RJGWI2TWy1/kqaE16HrR2J/FWgkYjdZQsX9M= +github.com/hashicorp/consul/api v1.12.0/go.mod h1:6pVBMo0ebnYdt2S3H87XhekM/HHrUoTD2XXb/VrZVy0= github.com/hashicorp/consul/sdk v0.8.0/go.mod h1:GBvyrGALthsZObzUGsfgHZQDXjg4lOjagTIwIR1vPms= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= @@ -235,6 +237,7 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.1/go.mod h1:4gW7WsVCke5TE7EPeYliwHlRUyBtfCwuFwuMg2DmyNY= @@ -269,6 +272,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w= +github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -291,6 +295,7 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -301,6 +306,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -325,17 +331,24 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sagikazarmark/crypt v0.3.0/go.mod h1:uD/D+6UF4SrIR1uGEv7bBNkNqLGqUr43MRiaGWX1Nig= +github.com/sagikazarmark/crypt v0.4.0/go.mod h1:ALv2SRj7GxYV4HO9elxH9nS6M9gW+xDNxqmyJ6RfDFM= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4= +github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= +github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v1.3.0/go.mod h1:BrRVncBjOJa/eUcVVm9CE+oC6as8k+VYr4NY7WCi9V4= +github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.10.0/go.mod h1:SoyBPwAtKDzypXNDFKN5kzH7ppppbGZtls1UpIy5AsM= +github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk= +github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -344,6 +357,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -561,6 +575,7 @@ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY= golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220224120231-95c6836cb0e7 h1:BXxu8t6QN0G1uff4bzZzSkpsax8+ALqTGUtz08QrV00= @@ -574,6 +589,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -669,6 +685,7 @@ google.golang.org/api v0.57.0/go.mod h1:dVPlbZyBo2/OjBpmvNdpn2GRm6rPy75jyU7bmhdr google.golang.org/api v0.59.0/go.mod h1:sT2boj7M9YJxZzgeZqXogmhfmRWDtPzT31xkieUbuZU= google.golang.org/api v0.61.0/go.mod h1:xQRti5UdCmoCEqFxcz93fTl338AVqDgyaDRuOZ3hg9I= google.golang.org/api v0.62.0/go.mod h1:dKmwPCydfsad4qCH08MSdgWjfHOyfpd4VtDGgRFdavw= +google.golang.org/api v0.63.0/go.mod h1:gs4ij2ffTRXwuzzgJl/56BdwJaA194ijkfn++9tDuPo= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -766,6 +783,7 @@ google.golang.org/grpc v1.39.1/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnD google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.40.1/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= +google.golang.org/grpc v1.43.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -786,6 +804,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= @@ -793,6 +812,7 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index af85f25..521eb65 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "path/filepath" + "strings" "time" "github.com/adrg/xdg" @@ -18,6 +19,7 @@ import ( "net/http" "github.com/gofiber/template/html" + "github.com/spf13/viper" "git.bascht.space/bascht/scanberry/scan" ) @@ -25,7 +27,7 @@ import ( //go:embed views/* var viewsfs embed.FS -var documents = make(map[string]scan.Document) +var documents = make(map[string]*scan.Document) var basedir string func main() { @@ -38,13 +40,28 @@ func main() { // engine := html.NewFileSystem(http.Dir("./views"), ".html") engine := html.NewFileSystem(http.FS(viewsfs), ".html") - basedir = xdg.RuntimeDir + "/himbeerscan" - os.MkdirAll(filepath.Join(basedir, "downloads"), os.ModePerm) - os.Chdir(basedir) + viper.SetConfigName("scanberry") + viper.SetConfigType("yaml") + viper.AddConfigPath(".") + viper.AddConfigPath(filepath.Join(xdg.ConfigHome, "scanberry")) - engine.Reload(true) // Optional. Default: false - engine.Debug(false) // Optional. Default: false - engine.Layout("embed") // Optional. Default: "embed" + viper.SetDefault("listen.host", "0.0.0.0") + viper.SetDefault("listen.port", "0.0.0.0") + // viper.SetDefault("filename.prefixes", [""]) + + err := viper.ReadInConfig() + if err != nil { + log.Fatal("Could not load config") + } + + basedir = xdg.RuntimeDir + "/himbeerscan" + os.MkdirAll(filepath.Join(basedir, "downloads"), 0700) + os.Chdir(basedir) + fmt.Sprintf("Starting to work in %v", basedir) + + engine.Reload(true) // Optional. Default: false + engine.Debug(false) // Optional. Default: false + engine.Layout("embed") // Optional. Default: "embed" engine.Delims("{{", "}}") // Optional. Default: engine delimiters // engine.AddFunc("greet", func(name string) string { @@ -63,25 +80,34 @@ func main() { }) app.Get("/scan", func(c *fiber.Ctx) error { + prefixes := viper.GetStringSlice("filename.prefixes") + timestamp := time.Now().Format("2006-01-02") return c.Render("views/scan", fiber.Map{ - "Title": "Hello, World!", + "Title": "Hello, World!", + "Prefixes": prefixes, + "Timestamp": timestamp, }, "views/layouts/main") }) app.Post("/scan", func(c *fiber.Ctx) error { + name := strings.Join([]string{c.FormValue("timestamp", time.Now().Format("2006-01-02")), c.FormValue("template"), c.FormValue("name")}, "-") + document := scan.Document{ Id: uuid.NewString(), - Name: c.FormValue("name"), + Name: name, Date: time.Now(), Duplex: c.FormValue("duplex") == "on", Events: make(chan scan.Event), } - documents[document.Id] = document + + documents[document.Id] = & document go scan.Process(basedir, &document) + document.Events <- scan.Event{Message: "Geht los", Type: "info"} + // document.Events <- scan.Event{Message: "Scanning initiated", Type: "info"} return c.Render("views/status", fiber.Map{ "id": document.Id, "name": document.Name, @@ -92,14 +118,16 @@ func main() { app.Get("/show/:id", func(c *fiber.Ctx) error { id := c.Params("id") - document := documents[id] - - return c.Render("views/show", fiber.Map{ - "id": document.Id, - "name": document.Name, - "FullName": document.FullName(), - "FullNameWithExtension": document.FullNameWithExtension(), - }, "views/layouts/main") + if document, ok := documents[id]; ok { + return c.Render("views/show", fiber.Map{ + "id": document.Id, + "name": document.Name, + "FullName": document.FullName(), + "FullNameWithExtension": document.FullNameWithExtension(), + }, "views/layouts/main") + } else { + return c.Status(fiber.StatusNotFound).SendString("Sorry, document no longer exists") + } }) app.Get("/events/:id", func(c *fiber.Ctx) error { @@ -117,7 +145,7 @@ func main() { for event := range document.Events { msg, err := json.Marshal(event) - if err != nil { + if err != nil { log.Fatal("Could not encode event") } @@ -135,4 +163,3 @@ func main() { log.Fatal(app.Listen(":3000")) } - diff --git a/scan/command.go b/scan/command.go index b2b16f2..73d37e0 100644 --- a/scan/command.go +++ b/scan/command.go @@ -2,6 +2,7 @@ package scan import ( "bufio" + "fmt" "log" "os/exec" ) @@ -25,6 +26,7 @@ func (command *Command) GetScanner() *bufio.Scanner { } func (command *Command) Start() { + fmt.Sprintf("Running Command %v \n", command) command.cmd.Start() } diff --git a/scan/process.go b/scan/process.go index e4fb885..e919593 100644 --- a/scan/process.go +++ b/scan/process.go @@ -2,9 +2,8 @@ package scan import ( "fmt" - "io" - "os" "path/filepath" + "strings" ) func Process(basedir string, document *Document) string { @@ -22,53 +21,56 @@ func Process(basedir string, document *Document) string { var commands []Command - commands = append(commands, Command{Name: "scanimage", Args: []string{"--device-name=epjitsu:libusb:001:003", "--format=tiff", "--batch=" + filepath.Join(basedir, document.FullName()+"-%d.tif"), "--source=" + feed, "--mode=Gray", "--resolution=300"}}) - commands = append(commands, Command{Name: "convert", Args: []string{filepath.Join(basedir, document.FullName()) + "*tif", filepath.Join(basedir, document.FullName()+"_input.pdf")}}) - commands = append(commands, Command{Name: "convert", Args: []string{filepath.Join(basedir, document.FullName()) + "-1.tif", filepath.Join(basedir, document.FullName()+".thumbnail.jpg")}}) - commands = append(commands, Command{Name: "ocrmypdf", Args: []string{"--language=deu", filepath.Join(basedir, document.FullName()+"_input.pdf"), filepath.Join(basedir, document.FullName()+".pdf")}}) + commands = append(commands, Command{Name: "scanimage", Args: []string{"--device-name=epjitsu", "--format=tiff", "--batch=" + filepath.Join(basedir, document.FullName()+"-%d.tif"), "--source=" + feed, "--mode=Gray", "--resolution=300"}}) + commands = append(commands, Command{Name: "convert", Args: []string{"-verbose", filepath.Join(basedir, document.FullName() + "*tif"), filepath.Join(basedir, document.FullName()+"_input.pdf")}}) + commands = append(commands, Command{Name: "convert", Args: []string{"-verbose", filepath.Join(basedir, document.FullName() + "-1.tif"), filepath.Join(basedir, "downloads", document.FullName()+".thumbnail.jpg")}}) + commands = append(commands, Command{Name: "ocrmypdf", Args: []string{"--language=deu", filepath.Join(basedir, document.FullName()+"_input.pdf"), filepath.Join(basedir, "downloads", document.FullName()+".pdf")}}) + <- document.Events + document.Events <- Event{Message: "Geht los", Type: "info"} for _, command := range commands { - done := make(chan bool) + // done := make(chan bool) scanner := command.GetScanner() - command.Start() + go func() { - document.Events <- Event{Message: command.Name + " Startet", Type: "info"} + document.Events <- Event{Message: command.Name + " Startet mit " + strings.Join(command.Args, " "), Type: "info"} for scanner.Scan() { document.Events <- Event{Message: scanner.Text(), Type: command.Name } } - done <- true + // done <- true }() command.Wait() } document.Events <- Event{Message: "Starting to copy", Type: "info"} - // Open original file - original, err := os.Open(filepath.Join(basedir, document.FullNameWithExtension())) - if err != nil { - document.Events <- Event{Message: "Alles kaputt", Type: "error"} - // log.Fatal(err) - } - defer original.Close() - // Create new file - newPath, err := filepath.Abs("./downloads/" + document.FullNameWithExtension()) - new, err := os.Create(newPath) - if err != nil { - document.Events <- Event{Message: "Alles kaputt", Type: "error"} - // log.Fatal(err) - } - defer new.Close() + // // Open original file + // original, err := os.Open(filepath.Join(basedir, document.FullNameWithExtension())) + // if err != nil { + // document.Events <- Event{Message: "Alles kaputt: " + err.Error(), Type: "error"} + // // log.Fatal(err) + // } + // defer original.Close() - //This will copy - bytesWritten, err := io.Copy(new, original) - if err != nil { - document.Events <- Event{Message: "Alles kaputt", Type: "error"} - // log.Fatal(err) - } - fmt.Printf("Bytes Written: %d\n", bytesWritten) + // // Create new file + // newPath, err := filepath.Abs("./downloads/" + document.FullNameWithExtension()) + // new, err := os.Create(newPath) + // if err != nil { + // document.Events <- Event{Message: "Alles kaputt", Type: "error"} + // // log.Fatal(err) + // } + // defer new.Close() + + // //This will copy + // bytesWritten, err := io.Copy(new, original) + // if err != nil { + // document.Events <- Event{Message: "Alles kaputt", Type: "error"} + // // log.Fatal(err) + // } + // fmt.Printf("Bytes Written: %d\n", bytesWritten) close(document.Events) return filepath.Join(basedir, document.FullNameWithExtension()) diff --git a/views/scan.html b/views/scan.html index c2e3dea..3cdc4c8 100644 --- a/views/scan.html +++ b/views/scan.html @@ -7,9 +7,16 @@
+ +
@@ -17,7 +24,7 @@
diff --git a/views/show.html b/views/show.html index 8743988..acbe60c 100644 --- a/views/show.html +++ b/views/show.html @@ -1,6 +1,6 @@ -

{{ .FullNameWithExtension }}

- -{{.FullNameWithExtension}} +

{{ .FullNameWithExtension }}

+ +{{.FullNameWithExtension}} Zurück diff --git a/views/status.html b/views/status.html index 1e30b3b..6ffbf83 100644 --- a/views/status.html +++ b/views/status.html @@ -54,7 +54,7 @@
📃
👀

-{{.FullNameWithExtension}}
+{{.FullNameWithExtension}}
 
   Zurück