-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaas.go
More file actions
176 lines (147 loc) · 4.12 KB
/
Copy pathcaas.go
File metadata and controls
176 lines (147 loc) · 4.12 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
)
type Message struct {
Text string
}
type Service struct {
Node string `json:"Node"`
Address string `json:"Address"`
ServiceID string `json:"ServiceID"`
ServiceName string `json:"ServiceName"`
ServiceTags []string `json:"ServiceTags"`
ServiceAddress string `json:"ServiceAddress"`
ServicePort int `json:"ServicePort"`
ServiceEnableTagOverride bool `json:"ServiceEnableTagOverride"`
CreateIndex int `json:"CreateIndex"`
ModifyIndex int `json:"ModifyIndex"`
}
// this should be it's own package but for now it's not
func getConsulService(url string) []Service {
var service []Service
// disable security check (at your own risk)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
request, err := client.Get(url)
if err != nil {
log.Println("WARNING: ", err)
return service
}
defer request.Body.Close()
content, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}
err = json.Unmarshal(content, &service)
if err != nil {
panic(err.Error())
}
return service
}
func downloadCatpic(url string) string {
extension := strings.Split(url, ".")
filename := "cat." + extension[len(extension)-1]
// create filename
file, err := os.Create("/tmp/" + filename)
if err != nil {
fmt.Println("Error while creating", filename, "-", err)
panic(err)
}
defer file.Close()
request, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
panic(err)
}
defer request.Body.Close()
_, err = io.Copy(file, request.Body)
if err != nil {
fmt.Println("Error copying file", file, "-", err)
panic(err)
}
return file.Name()
}
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello 2020!")
}
// display random cat pictures
func pic(w http.ResponseWriter, r *http.Request) {
// testing ELK
log.Println("YAY I AM WORKING")
// set default imageURL in case something fails
imageURL := "http://i.dailymail.co.uk/i/pix/2014/08/05/1407225932091_wps_6_SANTA_MONICA_CA_AUGUST_04.jpg"
// set default image api
catapi := "http://localhost:8888/image?search=cat"
consulURL := "http://consul.service.consul:8500/v1/catalog/service/image-service"
service := getConsulService(consulURL)
if service != nil {
// construct the catapi
catapi = "http://image-service.service.consul:" + strconv.Itoa(service[0].ServicePort) + "/image?search=cats"
}
request, err := http.Get(catapi)
if err != nil {
log.Println("problem with fetching", catapi, ":", err)
} else {
defer request.Body.Close()
content, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}
data := make(map[string][]map[string]string)
//var data interface{}
err = json.Unmarshal(content, &data)
if err != nil {
panic(err.Error())
}
// get random image from the data returned
randomIndex := rand.Intn(len(data["items"]) - 1)
randocat := data["items"][randomIndex]
for _, url := range randocat {
imageURL = url
}
}
image := downloadCatpic(imageURL)
w.Header().Set("Content-Type", "image/jpg")
http.ServeFile(w, r, image)
//fmt.Fprintf(w, "<img src=%s width=\"500\" height=\"500\">", image)
}
func cat(w http.ResponseWriter, r *http.Request) {
m := Message{"MEOW, purrrrr"}
b, err := json.Marshal(m)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func static(w http.ResponseWriter, r *http.Request) {
urlSplit := strings.Split(r.URL.Path, "/")
filename := urlSplit[len(urlSplit)-1]
if len(filename) == 0 {
http.Error(w, http.StatusText(404), 404)
return
}
dirFilename := "/tmp/" + filename
w.Header().Set("Content-Type", "image/jpg")
http.ServeFile(w, r, dirFilename)
}
func main() {
http.HandleFunc("/", hello)
http.HandleFunc("/cat", cat)
http.HandleFunc("/pic", pic)
http.HandleFunc("/static/", static)
http.ListenAndServe(":8080", nil)
}