-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
39 lines (32 loc) · 840 Bytes
/
server.go
File metadata and controls
39 lines (32 loc) · 840 Bytes
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
package main
import (
"net/http"
"encoding/json"
"fmt"
"log"
"github.com/gorilla/mux"
)
func main() {
fmt.Println("Server started and running at port 8421")
r := mux.NewRouter()
r.HandleFunc("/{room}", RoomHandler).Queries("location", "{link}")
r.HandleFunc("/", req)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":8421", nil))
}
func req(w http.ResponseWriter, r *http.Request) {
laundryRooms := Rooms{}
laundryRooms = Scrape()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(laundryRooms)
}
func RoomHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
room := Room {
Name: vars["room"],
Url: "?location=" + vars["link"],
}
room = GetInfo(room)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(room)
}