Skip to content
Snippets Groups Projects
Commit dd235b83 authored by Abdulsamad Sheikh's avatar Abdulsamad Sheikh :cat2:
Browse files

Fixed up a lot of errors, moved files and cleaned

parent 0a1b90eb
No related branches found
No related tags found
No related merge requests found
Pipeline #25516 canceled
package main
import (
"net/http"
"assignment1/handlers"
"time"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/librarystats/v1/bookcount/", handlers.BookCountHandler)
mux.HandleFunc("/librarystats/v1/readership/", handlers.ReadershipHandler)
mux.HandleFunc("/librarystats/v1/status/", handlers.StatusHandler)
srv := &http.Server{
Handler: mux,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
http.ListenAndServe(":8080", mux)
}
File moved
......@@ -8,6 +8,37 @@ import (
"fmt"
)
// BookCountHandler handles requests to the /librarystats/v1/bookcount/ endpoint.
func BookCountHandler(w http.ResponseWriter, r *http.Request) {
// Extract query parameters for language
query := r.URL.Query()
language := query.Get("language")
// Fetch book data from the Gutendex API
booksResponse, err := services.FetchBooksByLanguage(language)
if err != nil {
http.Error(w, "Failed to fetch books: "+err.Error(), http.StatusInternalServerError)
return
}
// Process the books to calculate the book count and author count
bookCount := len(booksResponse.Results)
authorCount := services.CalculateUniqueAuthors(booksResponse.Results)
// Create a response object
response := map[string]interface{}{
"language": language,
"bookCount": bookCount,
"authorCount": authorCount,
}
// Write the response back as JSON
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
}
}
func ReadershipHandler(w http.ResponseWriter, r *http.Request) {
// Extract the language code from the URL path
language := r.URL.Path[len("/librarystats/v1/readership/"):]
......
main.go 0 → 100644
package main
import (
"assignment1/handlers" // Make sure this path is correct
"net/http"
"time"
"log"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/librarystats/v1/bookcount/", handlers.BookCountHandler)
mux.HandleFunc("/librarystats/v1/readership/", handlers.ReadershipHandler)
mux.HandleFunc("/librarystats/v1/status/", handlers.StatusHandler)
srv := &http.Server{
Handler: mux,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
// Use the srv variable to start the server
if err := srv.ListenAndServe(); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
File moved
package models
// ... Other models
// GutenbergBook represents the structure of a book from the Gutenberg API.
type GutenbergBook struct {
ID int `json:"id"`
Title string `json:"title"`
Authors []Person `json:"authors"`
Translators []Person `json:"translators"`
Subjects []string `json:"subjects"`
Bookshelves []string `json:"bookshelves"`
Languages []string `json:"languages"`
Copyright bool `json:"copyright"`
MediaType string `json:"media_type"`
Formats map[string]string `json:"formats"`
DownloadCount int `json:"download_count"`
ID int `json:"id"`
Title string `json:"title"`
Authors []Person `json:"authors"`
Translators []Person `json:"translators"`
Subjects []string `json:"subjects"`
Bookshelves []string `json:"bookshelves"`
Languages []string `json:"languages"`
Copyright bool `json:"copyright"`
MediaType string `json:"media_type"`
Formats map[string]string `json:"formats"`
DownloadCount int `json:"download_count"`
}
// GutenbergResponse represents the paginated response from Gutenberg API.
type GutenbergResponse struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []GutenbergBook `json:"results"`
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []GutenbergBook `json:"results"`
}
// Person represents an author or translator.
type Person struct {
BirthYear *int `json:"birth_year"`
DeathYear *int `json:"death_year"`
Name string `json:"name"`
BirthYear *int `json:"birth_year"`
DeathYear *int `json:"death_year"`
Name string `json:"name"`
}
// LanguageCountriesResponse represents the response structure from the Language2Countries API.
......@@ -47,3 +45,21 @@ type CountryInfo struct {
type CountryResponse struct {
Population int64 `json:"population"`
}
// ReadershipDetail represents the detailed readership information for a country.
type ReadershipDetail struct {
Country string `json:"country"`
ISOCode string `json:"isocode"`
Books int `json:"books"`
Authors int `json:"authors"`
Readership int64 `json:"readership"`
}
// ServiceStatus represents the status of external services your application depends on.
type ServiceStatus struct {
GutendexAPI string `json:"gutendexapi"`
LanguageAPI string `json:"languageapi"`
CountriesAPI string `json:"countriesapi"`
Version string `json:"version"`
Uptime int64 `json:"uptime"`
}
\ No newline at end of file
File moved
......@@ -10,8 +10,7 @@ import (
// FetchBooksByLanguage retrieves books from the Gutendex API by language.
func FetchBooksByLanguage(language string) (*models.GutenbergResponse, error) {
url := "http://129.241.150.113:8000/books?languages=" + language
resp, err := http.Get(url
)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment