-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
176 lines (152 loc) · 4.41 KB
/
Copy pathmain.go
File metadata and controls
176 lines (152 loc) · 4.41 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 (
"context"
"encoding/json"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
type Weather struct {
ID string `json:"id"`
City string `json:"city"`
Temperature float64 `json:"temperature"`
Conditions string `json:"conditions"`
}
var WeatherDB = make(map[string]Weather)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func main() {
// Create a context that we can cancel
ctx, cancel := context.WithCancel(context.Background())
r := mux.NewRouter()
r.HandleFunc("/weather", createWeather).Methods("POST")
r.HandleFunc("/weather/{id}", getWeather).Methods("GET")
r.HandleFunc("/weather/{id}", updateWeather).Methods("PUT")
r.HandleFunc("/weather/{id}", deleteWeather).Methods("DELETE")
r.HandleFunc("/weather-stream", weatherStream)
server := http.Server{
Addr: ":7070",
Handler: r,
}
go func() {
if err := server.ListenAndServe(); err != nil {
// We expect errors to happen when the server is Shutdown or closed,
// but not in other cases.
if err != http.ErrServerClosed {
log.Fatalf("ListenAndServe(): %s", err)
}
}
}()
// Setup the shutdown signal handler
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
// We received an interrupt/kill signal; shut down gracefully.
log.Println("Gracefully shutting down...")
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Could not gracefully shutdown the server: %s", err)
}
cancel()
log.Println("Server stopped")
}
func createWeather(w http.ResponseWriter, r *http.Request) {
var newWeather Weather
err := json.NewDecoder(r.Body).Decode(&newWeather)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
err = newWeather.Validate()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Here we generate a new unique ID
newWeather.ID = uuid.New().String()
WeatherDB[newWeather.ID] = newWeather
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) // Set the status before encoding the body
json.NewEncoder(w).Encode(newWeather)
}
func getWeather(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
if weather, ok := WeatherDB[id]; ok {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(weather)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "No weather data found for provided id"})
}
}
func updateWeather(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
var updatedWeather Weather
err := json.NewDecoder(r.Body).Decode(&updatedWeather)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
err = updatedWeather.Validate()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if _, ok := WeatherDB[id]; ok {
updatedWeather.ID = id
WeatherDB[id] = updatedWeather
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(updatedWeather)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "No weather data found for provided id"})
}
}
func deleteWeather(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
if _, ok := WeatherDB[id]; ok {
delete(WeatherDB, id)
w.WriteHeader(http.StatusNoContent)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
}
func weatherStream(w http.ResponseWriter, r *http.Request) {
conn, _ := upgrader.Upgrade(w, r, nil)
for {
// Assume we have some mechanism to get the latest weather update
weatherUpdate := getLatestWeatherUpdate()
if err := conn.WriteJSON(weatherUpdate); err != nil {
return
}
}
}
func getLatestWeatherUpdate() Weather {
// Return random weather from WeatherDB
for _, weather := range WeatherDB {
return weather
}
return Weather{}
}
func (w *Weather) Validate() error {
if w.City == "" {
return errors.New("Missing required field: city")
}
if w.Temperature == 0 {
return errors.New("Missing required field: temperature")
}
if w.Conditions == "" {
return errors.New("Missing required field: conditions")
}
return nil
}