-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.go
40 lines (35 loc) · 1.02 KB
/
server.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/moficodes/bookdata/api/datastore"
"log"
"net/http"
"time"
)
var (
books datastore.BookStore
)
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
func init() {
defer timeTrack(time.Now(), "file load")
books = &datastore.Books{}
books.Initialize()
}
func main() {
r := mux.NewRouter()
log.Println("bookdata api")
api := r.PathPrefix("/api/v1").Subrouter()
api.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "api v1")
})
api.HandleFunc("/books/authors/{author}", searchByAuthor).Methods(http.MethodGet)
api.HandleFunc("/books/book-name/{bookName}", searchByBookName).Methods(http.MethodGet)
api.HandleFunc("/book/isbn/{isbn}", searchByISBN).Methods(http.MethodGet)
api.HandleFunc("/book/isbn/{isbn}", deleteByISBN).Methods(http.MethodDelete)
api.HandleFunc("/book", createBook).Methods(http.MethodPost)
log.Fatalln(http.ListenAndServe(":8080", r))
}