1
0
Fork 0
mirror of https://github.com/Luzifer/tex-api.git synced 2025-01-08 12:52:48 +00:00

Remove code duplications

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-03-06 11:16:11 +01:00
parent 0e10f17640
commit 3da686bec0
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

26
main.go
View file

@ -120,6 +120,11 @@ func main() {
log.Fatalf("%s", http.ListenAndServe(cfg.Listen, router)) 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 { func pathFromUUID(uid uuid.UUID, filename string) string {
return path.Join(cfg.StorageDir, uid.String(), filename) 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 { if f, err := os.Create(inputFile); err == nil {
defer f.Close() defer f.Close()
if _, copyErr := io.Copy(f, r.Body); err != nil { if _, copyErr := io.Copy(f, r.Body); err != nil {
log.Errorf("Unable to copy input file %q: %s", inputFile, copyErr) serverErrorf(res, "Unable to copy input file %q: %s", inputFile, copyErr)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
f.Sync() f.Sync()
} else { } else {
log.Errorf("Unable to write input file %q: %s", inputFile, err) serverErrorf(res, "Unable to write input file %q: %s", inputFile, err)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
@ -158,8 +161,7 @@ func startNewJob(res http.ResponseWriter, r *http.Request) {
Status: statusCreated, Status: statusCreated,
} }
if err := status.Save(); err != nil { if err := status.Save(); err != nil {
log.Errorf("Unable to create status file %q: %s", statusFile, err) serverErrorf(res, "Unable to create status file %q: %s", statusFile, err)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
@ -179,13 +181,11 @@ func getJobStatus(res http.ResponseWriter, r *http.Request) {
if status, err := loadStatusByUUID(uid); err == nil { if status, err := loadStatusByUUID(uid); err == nil {
if encErr := json.NewEncoder(res).Encode(status); err != nil { if encErr := json.NewEncoder(res).Encode(status); err != nil {
log.Errorf("Unable to serialize status file: %s", encErr) serverErrorf(res, "Unable to serialize status file: %s", encErr)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
} else { } else {
log.Errorf("Unable to read status file: %s", err) serverErrorf(res, "Unable to read status file: %s", err)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
} }
@ -208,8 +208,7 @@ func waitForJob(res http.ResponseWriter, r *http.Request) {
status, err := loadStatusByUUID(uid) status, err := loadStatusByUUID(uid)
if err != nil { if err != nil {
log.Errorf("Unable to read status file: %s", err) serverErrorf(res, "Unable to read status file: %s", err)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
@ -347,8 +346,7 @@ func downloadAssets(res http.ResponseWriter, r *http.Request) {
} }
if err != nil { if err != nil {
log.Errorf("Unable to generate downloadable asset: %s", err) serverErrorf(res, "Unable to generate downloadable asset: %s", err)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }