Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Added handler for books.json
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Jun 26, 2017
1 parent c9208bf commit 5bb111c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -211,6 +212,13 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) {
}
}

// JSONHandler handles the books.json file
func JSONHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(books)
w.Write(b)
}

var books []Book

func runServer(bks []Book, addr string) {
Expand All @@ -226,6 +234,7 @@ func runServer(bks []Book, addr string) {
http.HandleFunc("/series/", SeriesHandler)
http.HandleFunc("/books/", BooksHandler)
http.HandleFunc("/search/", SearchHandler)
http.HandleFunc("/books.json", JSONHandler)
http.HandleFunc("/random/", func(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().Unix())
n := rand.Int() % len(books)
Expand Down

0 comments on commit 5bb111c

Please sign in to comment.