Skip to content

Commit

Permalink
add a README
Browse files Browse the repository at this point in the history
  • Loading branch information
veryordinary11 committed Jul 30, 2023
1 parent ef5dc88 commit c3e6142
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
# Go-web-server
# API Router

The API Router is a module in the Chirpy application responsible for handling various API endpoints. It uses the Chi router package to define and handle different routes for the Chirps, Users, Tokens, and Webhooks APIs.

## Endpoints

The following endpoints are defined in the API Router:

### Test API

- `/healthz` (GET): Endpoint for health checks and readiness probes.
- `/metrics` (GET): Endpoint for exposing application metrics.
- `/validate_chirp` (POST): Endpoint for validating a chirp.

### Chirps API

- `/chirps` (GET): Endpoint for fetching all chirps. Supports optional sorting by `sort` query parameter in ascending (`sort=asc`) or descending (`sort=desc`) order.
- `/chirps/{id}` (GET): Endpoint for fetching a single chirp by its ID.
- `/chirps` (POST): Endpoint for creating a new chirp.
- `/chirps/{id}` (DELETE): Endpoint for deleting a chirp by its ID.

### Users API

- `/users` (POST): Endpoint for creating a new user.
- `/login` (POST): Endpoint for user login. It issues a JWT access token for authentication.
- `/users` (PUT): Endpoint for updating a user's email and password.

### Token API

- `/refresh` (POST): Endpoint for refreshing an expired JWT access token.
- `/revoke` (POST): Endpoint for revoking a JWT access token.

### Webhooks API

- `/polka/webhooks` (POST): Endpoint for handling webhooks from the Polka service.

## Handlers

The API Router uses various handler functions to process incoming requests and generate responses. Each endpoint has its corresponding handler function responsible for handling the specific logic associated with that endpoint.

## Dependencies

The API Router relies on the following packages:

- `net/http`: For creating the HTTP server and handling HTTP requests.
- `github.com/go-chi/chi/v5`: A lightweight and fast HTTP router for Go.
- `database`: A custom package that provides access to the database.
- `jwt`: A package for handling JSON Web Tokens (JWT) for authentication.

## Usage

To use the API Router, you need to pass an `apiConfig` object to the `createAPIRouter` function. The `apiConfig` object should contain a database connection (`DB`) and a JWT secret (`jwtSecret`) for handling authentication.

Example:

```go
import (
"net/http"
"github.com/your-username/chirpy/apiRouter"
"github.com/your-username/chirpy/database"
)

func main() {
// Create a new database connection
db := database.New()

// Create an API configuration with the database and JWT secret
apiCfg := &apiRouter.APIConfig{
DB: db,
JWTSecret: "your-jwt-secret-key",
}

// Create the API router
router := apiRouter.CreateAPIRouter(apiCfg)

// Start the HTTP server
http.ListenAndServe(":8080", router)
}
20 changes: 20 additions & 0 deletions handleChirpsGetAll.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"sort"
"strconv"

"github.com/veryordinary11/Go-web-server/database"
Expand All @@ -18,6 +19,25 @@ func handlerChirpsGetAll(db *database.DB) http.HandlerFunc {
return
}

// Get the sort query parameter from the URL
sortParam := r.URL.Query().Get("sort")
if sortParam != "desc" {
sortParam = "asc"
}

// Sort chirps by ID
if sortParam == "asc" {
// Sort chirps by id in ascending order
sort.Slice(chirps, func(i, j int) bool {
return chirps[i].ID < chirps[j].ID
})
} else {
// Sort chirps by id in descending order
sort.Slice(chirps, func(i, j int) bool {
return chirps[i].ID > chirps[j].ID
})
}

// Filter chirps by authorId
if authorId == "" {
responseWithJSON(w, http.StatusOK, chirps)
Expand Down

0 comments on commit c3e6142

Please sign in to comment.