From 1d4f316f886ed063965b015eb094defaba14072d Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 6 Mar 2017 09:10:36 +0100 Subject: [PATCH 1/4] Apply linter advices Signed-off-by: Knut Ahlers --- main.go | 45 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index 4939e96..415304d 100644 --- a/main.go +++ b/main.go @@ -53,17 +53,17 @@ const ( sleepBase = 1.5 ) -type statusOutput struct { +type jobStatus struct { UUID string `json:"uuid"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` Status status `json:"status"` } -func loadStatusByUUID(uid uuid.UUID) (*statusOutput, error) { +func loadStatusByUUID(uid uuid.UUID) (*jobStatus, error) { statusFile := pathFromUUID(uid, filenameStatus) - status := statusOutput{} + status := jobStatus{} if f, err := os.Open(statusFile); err == nil { defer f.Close() if err = json.NewDecoder(f).Decode(&status); err != nil { @@ -76,12 +76,12 @@ func loadStatusByUUID(uid uuid.UUID) (*statusOutput, error) { return &status, nil } -func (s *statusOutput) UpdateStatus(st status) { +func (s *jobStatus) UpdateStatus(st status) { s.Status = st s.UpdatedAt = time.Now() } -func (s statusOutput) Save() error { +func (s jobStatus) Save() error { uid, _ := uuid.FromString(s.UUID) f, err := os.Create(pathFromUUID(uid, filenameStatus)) if err != nil { @@ -139,8 +139,8 @@ func startNewJob(res http.ResponseWriter, r *http.Request) { if f, err := os.Create(inputFile); err == nil { defer f.Close() - if _, err := io.Copy(f, r.Body); err != nil { - log.Errorf("Unable to copy input file %q: %s", inputFile, err) + if _, copyErr := io.Copy(f, r.Body); err != nil { + log.Errorf("Unable to copy input file %q: %s", inputFile, copyErr) http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) return } @@ -151,7 +151,7 @@ func startNewJob(res http.ResponseWriter, r *http.Request) { return } - status := statusOutput{ + status := jobStatus{ UUID: jobUUID.String(), CreatedAt: time.Now(), UpdatedAt: time.Now(), @@ -169,23 +169,6 @@ func startNewJob(res http.ResponseWriter, r *http.Request) { http.Redirect(res, r, u.String(), http.StatusFound) } -func checkJobStatus(res http.ResponseWriter, r *http.Request) (uuid.UUID, string) { - vars := mux.Vars(r) - uid, err := uuid.FromString(vars["uid"]) - if err != nil { - http.Error(res, "UUID had unexpected format!", http.StatusBadRequest) - return uid, "" - } - - statusFile := pathFromUUID(uid, filenameStatus) - if _, err := os.Stat(statusFile); err != nil { - http.Error(res, "Status for this UUID not found.", http.StatusNotFound) - return uid, "" - } - - return uid, statusFile -} - func getJobStatus(res http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) uid, err := uuid.FromString(vars["uid"]) @@ -195,8 +178,8 @@ func getJobStatus(res http.ResponseWriter, r *http.Request) { } if status, err := loadStatusByUUID(uid); err == nil { - if err := json.NewEncoder(res).Encode(status); err != nil { - log.Errorf("Unable to serialize status file: %s", err) + if encErr := json.NewEncoder(res).Encode(status); err != nil { + log.Errorf("Unable to serialize status file: %s", encErr) http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) return } @@ -217,7 +200,7 @@ func waitForJob(res http.ResponseWriter, r *http.Request) { var loop int if v := r.URL.Query().Get("loop"); v != "" { - if pv, err := strconv.Atoi(v); err == nil { + if pv, convErr := strconv.Atoi(v); convErr == nil { loop = pv } } @@ -390,14 +373,14 @@ func jobProcessor(uid uuid.UUID) { status.UpdateStatus(statusStarted) if err := status.Save(); err != nil { - log.Errorf("Unable to save status file: %s") + log.Errorf("Unable to save status file") return } if err := cmd.Run(); err != nil { status.UpdateStatus(statusError) if err := status.Save(); err != nil { - log.Errorf("Unable to save status file: %s") + log.Errorf("Unable to save status file") return } return @@ -405,7 +388,7 @@ func jobProcessor(uid uuid.UUID) { status.UpdateStatus(statusFinished) if err := status.Save(); err != nil { - log.Errorf("Unable to save status file: %s") + log.Errorf("Unable to save status file") return } } From 0e10f17640f94742fef409a238a87b023284a389 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 6 Mar 2017 09:41:58 +0100 Subject: [PATCH 2/4] Add tests Signed-off-by: Knut Ahlers --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ada6ff1 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +auto-hook-pre-push: + +test: + gometalinter -D errcheck -D gas --deadline 20s . From 3da686bec00a0455d1b04b123322461cdc9c2ea6 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 6 Mar 2017 11:16:11 +0100 Subject: [PATCH 3/4] Remove code duplications Signed-off-by: Knut Ahlers --- main.go | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 415304d..60b7dc7 100644 --- a/main.go +++ b/main.go @@ -120,6 +120,11 @@ func main() { log.Fatalf("%s", http.ListenAndServe(cfg.Listen, router)) } +func serverErrorf(res http.ResponseWriter, tpl string, args ...interface{}) { + log.Errorf(tpl, args...) + http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) +} + func pathFromUUID(uid uuid.UUID, filename string) string { return path.Join(cfg.StorageDir, uid.String(), filename) } @@ -140,14 +145,12 @@ func startNewJob(res http.ResponseWriter, r *http.Request) { if f, err := os.Create(inputFile); err == nil { defer f.Close() if _, copyErr := io.Copy(f, r.Body); err != nil { - log.Errorf("Unable to copy input file %q: %s", inputFile, copyErr) - http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) + serverErrorf(res, "Unable to copy input file %q: %s", inputFile, copyErr) return } f.Sync() } else { - log.Errorf("Unable to write input file %q: %s", inputFile, err) - http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) + serverErrorf(res, "Unable to write input file %q: %s", inputFile, err) return } @@ -158,8 +161,7 @@ func startNewJob(res http.ResponseWriter, r *http.Request) { Status: statusCreated, } if err := status.Save(); err != nil { - log.Errorf("Unable to create status file %q: %s", statusFile, err) - http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) + serverErrorf(res, "Unable to create status file %q: %s", statusFile, err) return } @@ -179,13 +181,11 @@ func getJobStatus(res http.ResponseWriter, r *http.Request) { if status, err := loadStatusByUUID(uid); err == nil { if encErr := json.NewEncoder(res).Encode(status); err != nil { - log.Errorf("Unable to serialize status file: %s", encErr) - http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) + serverErrorf(res, "Unable to serialize status file: %s", encErr) return } } else { - log.Errorf("Unable to read status file: %s", err) - http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) + serverErrorf(res, "Unable to read status file: %s", err) return } } @@ -208,8 +208,7 @@ func waitForJob(res http.ResponseWriter, r *http.Request) { status, err := loadStatusByUUID(uid) if err != nil { - log.Errorf("Unable to read status file: %s", err) - http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) + serverErrorf(res, "Unable to read status file: %s", err) return } @@ -347,8 +346,7 @@ func downloadAssets(res http.ResponseWriter, r *http.Request) { } if err != nil { - log.Errorf("Unable to generate downloadable asset: %s", err) - http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) + serverErrorf(res, "Unable to generate downloadable asset: %s", err) return } From 81e4d4b2d79e09008b6828ca352714a5b5bc824f Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 6 Mar 2017 21:59:53 +0100 Subject: [PATCH 4/4] Move builders to own file Signed-off-by: Knut Ahlers --- .gitignore | 1 + assets.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 95 ------------------------------------------------ 3 files changed, 105 insertions(+), 95 deletions(-) create mode 100644 .gitignore create mode 100644 assets.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7840f2d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tex-api diff --git a/assets.go b/assets.go new file mode 100644 index 0000000..049ba05 --- /dev/null +++ b/assets.go @@ -0,0 +1,104 @@ +package main + +import ( + "archive/tar" + "archive/zip" + "bytes" + "io" + "os" + "path" + "path/filepath" + "strings" + + "github.com/Luzifer/go_helpers/str" + uuid "github.com/satori/go.uuid" +) + +func shouldPackFile(extension string) bool { + return str.StringInSlice(extension, []string{ + ".log", + ".pdf", + }) +} + +func buildAssetsZIP(uid uuid.UUID) (io.Reader, error) { + buf := new(bytes.Buffer) + w := zip.NewWriter(buf) + + basePath := pathFromUUID(uid, filenameOutputDir) + err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !shouldPackFile(path.Ext(info.Name())) { + return nil + } + + zipInfo, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + zipInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") + zipFile, err := w.CreateHeader(zipInfo) + if err != nil { + return err + } + osFile, err := os.Open(p) + if err != nil { + return err + } + + io.Copy(zipFile, osFile) + osFile.Close() + + return nil + }) + + if err != nil { + return nil, err + } + + return buf, w.Close() +} + +func buildAssetsTAR(uid uuid.UUID) (io.Reader, error) { + buf := new(bytes.Buffer) + w := tar.NewWriter(buf) + + basePath := pathFromUUID(uid, filenameOutputDir) + err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !shouldPackFile(path.Ext(info.Name())) { + return nil + } + + tarInfo, err := tar.FileInfoHeader(info, "") + if err != nil { + return err + } + tarInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") + err = w.WriteHeader(tarInfo) + if err != nil { + return err + } + osFile, err := os.Open(p) + if err != nil { + return err + } + + io.Copy(w, osFile) + osFile.Close() + + return nil + }) + + if err != nil { + return nil, err + } + + return buf, w.Close() +} diff --git a/main.go b/main.go index 60b7dc7..0db67cc 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,6 @@ package main import ( - "archive/tar" - "archive/zip" - "bytes" "encoding/json" "fmt" "io" @@ -13,12 +10,9 @@ import ( "os" "os/exec" "path" - "path/filepath" "strconv" - "strings" "time" - "github.com/Luzifer/go_helpers/str" "github.com/Luzifer/rconfig" log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" @@ -234,95 +228,6 @@ func waitForJob(res http.ResponseWriter, r *http.Request) { } } -func shouldPackFile(extension string) bool { - return str.StringInSlice(extension, []string{ - ".log", - ".pdf", - }) -} - -func buildAssetsZIP(uid uuid.UUID) (io.Reader, error) { - buf := new(bytes.Buffer) - w := zip.NewWriter(buf) - - basePath := pathFromUUID(uid, filenameOutputDir) - err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if !shouldPackFile(path.Ext(info.Name())) { - return nil - } - - zipInfo, err := zip.FileInfoHeader(info) - if err != nil { - return err - } - zipInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") - zipFile, err := w.CreateHeader(zipInfo) - if err != nil { - return err - } - osFile, err := os.Open(p) - if err != nil { - return err - } - - io.Copy(zipFile, osFile) - osFile.Close() - - return nil - }) - - if err != nil { - return nil, err - } - - return buf, w.Close() -} - -func buildAssetsTAR(uid uuid.UUID) (io.Reader, error) { - buf := new(bytes.Buffer) - w := tar.NewWriter(buf) - - basePath := pathFromUUID(uid, filenameOutputDir) - err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if !shouldPackFile(path.Ext(info.Name())) { - return nil - } - - tarInfo, err := tar.FileInfoHeader(info, "") - if err != nil { - return err - } - tarInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") - err = w.WriteHeader(tarInfo) - if err != nil { - return err - } - osFile, err := os.Open(p) - if err != nil { - return err - } - - io.Copy(w, osFile) - osFile.Close() - - return nil - }) - - if err != nil { - return nil, err - } - - return buf, w.Close() -} - func downloadAssets(res http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) uid, err := uuid.FromString(vars["uid"])