Skip to content

Commit de34642

Browse files
warrnkochman
authored andcommitted
Address code quality issues (#19)
* Address code quality issues * Post processing work is offloaded to goroutine, happens in background after newly generated ID is returned * Added overwrite functionality to post endpoint * Job status is stored in the Photo struct * Channels are used to return required content from processing * Sync WaitGroup is utilized for syncing of processing routines * Using 3 separate buffers instead of copying on write with pipe * Remove unnecessary debug info * Fix JSON naming * Add testing for Photo structure marshaling/unmarshaling * Add testing for Photo Processing * Create testing for request helper functions; mock out Storm functionality * Shouldn't 500 on empty database * Fix for go test -race
1 parent ccbe70f commit de34642

16 files changed

+1157
-149
lines changed

cmd/pusher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var pusherCmd = &cobra.Command{
2020

2121
_, err := config.New()
2222
if err != nil {
23-
log.WithError(err).Error("Unable to create config")
23+
log.WithError(err).Error("unable to create config")
2424
return
2525
}
2626
},

cmd/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ var serverCmd = &cobra.Command{
2222

2323
config, err := config.New()
2424
if err != nil {
25-
log.WithError(err).Error("Unable to create config")
25+
log.WithError(err).Error("unable to create config")
2626
return
2727
}
2828
runner := runner.New()
2929

3030
server, err := server.New(config)
3131
if err != nil {
32-
log.WithError(err).Error("Unable to create server")
32+
log.WithError(err).Error("unable to create server")
3333
return
3434
}
3535
runner.Add(server)

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ func New() (*Config, error) {
1919
c.PhotosDirectory = dir
2020
}
2121

22+
listen, ok := os.LookupEnv("HOTSHOTS_LISTEN_URL")
23+
if ok {
24+
c.ListenURL = listen
25+
}
26+
2227
return c, nil
2328
}
2429

server/_testdata/emptyFile.jpg

Loading

server/_testdata/exiflessFile.jpg

3.94 MB
Loading

server/_testdata/garbageFile.jpg

Lines changed: 1 addition & 0 deletions
Loading

server/_testdata/validFile.jpg

4.27 MB
Loading

server/helpers.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"math"
66
"net/http"
77
"strconv"
8+
"sync"
9+
"time"
810

911
"github.com/asdine/storm"
1012
"github.com/asdine/storm/index"
@@ -48,21 +50,35 @@ func GetPaginateValues(r *http.Request) (func(*index.Options), func(*index.Optio
4850
}
4951

5052
func WriteError(s string, status int, w http.ResponseWriter) {
51-
w.WriteHeader(status)
5253
v := ErrorResponse{
5354
Success: false,
5455
Error: s,
5556
}
56-
WriteJsonResponse(v, w)
57+
WriteJsonResponse(v, status, w)
5758
}
5859

59-
func WriteJsonResponse(v interface{}, w http.ResponseWriter) {
60+
func WriteJsonResponse(v interface{}, status int, w http.ResponseWriter) {
6061
js, err := json.Marshal(v)
6162
if err != nil {
6263
http.Error(w, err.Error(), http.StatusInternalServerError)
6364
return
6465
}
6566

6667
w.Header().Set("Content-Type", "application/json")
68+
w.WriteHeader(status)
6769
w.Write(js)
6870
}
71+
72+
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
73+
c := make(chan struct{})
74+
go func() {
75+
wg.Wait()
76+
close(c)
77+
}()
78+
select {
79+
case <-c:
80+
return false // compl eted normally
81+
case <-time.After(timeout):
82+
return true // timed out
83+
}
84+
}

0 commit comments

Comments
 (0)