-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
113 lines (93 loc) · 2.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
b64 "encoding/base64"
"github.com/GetStream/rockets-go-tutorial/seam"
"github.com/GetStream/rockets-go-tutorial/unsplash"
"github.com/flosch/pongo2"
)
const (
IMAGE_URL string = "https://bit.ly/2QGPDkr"
)
type Task struct {
Position int
URL string
}
type TaskResult struct {
Position int
Resized []byte
Err error
}
var spacexTemplate = pongo2.Must(pongo2.FromFile("spacex.html"))
func main() {
fmt.Println("Ready for liftoff! Checkout \n http://localhost:3000/occupymars \n http://localhost:3000/spacex \n http://localhost:3000/spacex_seams")
http.HandleFunc("/occupymars", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("resize") > "" {
resized, err := seam.ContentAwareResize(IMAGE_URL)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "image/jpeg")
io.Copy(w, bytes.NewReader(resized))
} else {
fmt.Fprintf(w, "<html><div>Original image:</div> <img src=\"%s\" /><br/><a href=\"?resize=1\">Resize using Seam Carving</a></html>", IMAGE_URL)
}
})
http.HandleFunc("/spacex", func(w http.ResponseWriter, r *http.Request) {
response, err := unsplash.LoadRockets()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = spacexTemplate.ExecuteWriter(pongo2.Context{"response": response}, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
http.HandleFunc("/spacex_seams", func(w http.ResponseWriter, r *http.Request) {
response, err := unsplash.LoadRockets()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
resultChannel := make(chan TaskResult)
taskChannel := make(chan Task)
imagesToResize := 8
// start 4 workers
for w := 1; w <= 4; w++ {
go worker(w, taskChannel, resultChannel)
}
// write to the taskChannel and close it when we're done
go func() {
for i, r := range response.Results[:imagesToResize] {
taskChannel <- Task{i, r.URLs["small"]}
}
close(taskChannel)
}()
// start listening for results in a separate goroutine
for a := 1; a <= imagesToResize; a++ {
taskResult := <-resultChannel
if taskResult.Err != nil {
log.Printf("Image %d failed to resize", taskResult.Position)
} else {
sEnc := b64.StdEncoding.EncodeToString(taskResult.Resized)
response.Results[taskResult.Position].Resized = sEnc
}
}
err = spacexTemplate.ExecuteWriter(pongo2.Context{"response": response}, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
log.Fatal(http.ListenAndServe(":3000", nil))
}
func worker(id int, taskChannel <-chan Task, resultChannel chan<- TaskResult) {
for j := range taskChannel {
fmt.Println("worker", id, "started job", j.Position)
resized, err := seam.ContentAwareResize(j.URL)
resultChannel <- TaskResult{j.Position, resized, err}
}
}