1
0
Fork 0
mirror of https://github.com/Luzifer/tex-api.git synced 2024-11-09 16:50:03 +00:00

Apply linter advices

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-03-06 09:10:36 +01:00
parent f2d676c7db
commit 1d4f316f88
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

45
main.go
View file

@ -53,17 +53,17 @@ const (
sleepBase = 1.5 sleepBase = 1.5
) )
type statusOutput struct { type jobStatus struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
Status status `json:"status"` Status status `json:"status"`
} }
func loadStatusByUUID(uid uuid.UUID) (*statusOutput, error) { func loadStatusByUUID(uid uuid.UUID) (*jobStatus, error) {
statusFile := pathFromUUID(uid, filenameStatus) statusFile := pathFromUUID(uid, filenameStatus)
status := statusOutput{} status := jobStatus{}
if f, err := os.Open(statusFile); err == nil { if f, err := os.Open(statusFile); err == nil {
defer f.Close() defer f.Close()
if err = json.NewDecoder(f).Decode(&status); err != nil { if err = json.NewDecoder(f).Decode(&status); err != nil {
@ -76,12 +76,12 @@ func loadStatusByUUID(uid uuid.UUID) (*statusOutput, error) {
return &status, nil return &status, nil
} }
func (s *statusOutput) UpdateStatus(st status) { func (s *jobStatus) UpdateStatus(st status) {
s.Status = st s.Status = st
s.UpdatedAt = time.Now() s.UpdatedAt = time.Now()
} }
func (s statusOutput) Save() error { func (s jobStatus) Save() error {
uid, _ := uuid.FromString(s.UUID) uid, _ := uuid.FromString(s.UUID)
f, err := os.Create(pathFromUUID(uid, filenameStatus)) f, err := os.Create(pathFromUUID(uid, filenameStatus))
if err != nil { if err != nil {
@ -139,8 +139,8 @@ 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 _, err := 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, err) log.Errorf("Unable to copy input file %q: %s", inputFile, copyErr)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
@ -151,7 +151,7 @@ func startNewJob(res http.ResponseWriter, r *http.Request) {
return return
} }
status := statusOutput{ status := jobStatus{
UUID: jobUUID.String(), UUID: jobUUID.String(),
CreatedAt: time.Now(), CreatedAt: time.Now(),
UpdatedAt: 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) 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) { func getJobStatus(res http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
uid, err := uuid.FromString(vars["uid"]) 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 status, err := loadStatusByUUID(uid); err == nil {
if err := json.NewEncoder(res).Encode(status); err != nil { if encErr := json.NewEncoder(res).Encode(status); err != nil {
log.Errorf("Unable to serialize status file: %s", err) log.Errorf("Unable to serialize status file: %s", encErr)
http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError) http.Error(res, "An error ocurred. See details in log.", http.StatusInternalServerError)
return return
} }
@ -217,7 +200,7 @@ func waitForJob(res http.ResponseWriter, r *http.Request) {
var loop int var loop int
if v := r.URL.Query().Get("loop"); v != "" { 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 loop = pv
} }
} }
@ -390,14 +373,14 @@ func jobProcessor(uid uuid.UUID) {
status.UpdateStatus(statusStarted) status.UpdateStatus(statusStarted)
if err := status.Save(); err != nil { if err := status.Save(); err != nil {
log.Errorf("Unable to save status file: %s") log.Errorf("Unable to save status file")
return return
} }
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
status.UpdateStatus(statusError) status.UpdateStatus(statusError)
if err := status.Save(); err != nil { if err := status.Save(); err != nil {
log.Errorf("Unable to save status file: %s") log.Errorf("Unable to save status file")
return return
} }
return return
@ -405,7 +388,7 @@ func jobProcessor(uid uuid.UUID) {
status.UpdateStatus(statusFinished) status.UpdateStatus(statusFinished)
if err := status.Save(); err != nil { if err := status.Save(); err != nil {
log.Errorf("Unable to save status file: %s") log.Errorf("Unable to save status file")
return return
} }
} }