-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from itishrishikesh/auth
Authentication middleware added. Added feed entity's schema, and handler.
- Loading branch information
Showing
13 changed files
with
340 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/itishrishikesh/gofeed/auth" | ||
"github.com/itishrishikesh/gofeed/constants" | ||
"github.com/itishrishikesh/gofeed/internal/database" | ||
) | ||
|
||
type authHeader func(http.ResponseWriter, *http.Request, database.User) | ||
|
||
func (config *apiConfig) authMiddleware(handler authHeader) http.HandlerFunc { | ||
return func(writer http.ResponseWriter, request *http.Request) { | ||
apiKey, err := auth.GetAPIKey(request.Header) | ||
if err != nil { | ||
respondWithError(writer, constants.HTTP_FORBIDDEN, fmt.Sprintf("E#1OV41H - Authentication Error: %v", err)) | ||
return | ||
} | ||
user, err := config.DB.GetUserByAPIKey(request.Context(), apiKey) | ||
if err != nil { | ||
respondWithError(writer, constants.HTTP_BAD_REQUEST, fmt.Sprintf("E#1OV44M - Couldn't get user: %v", err)) | ||
return | ||
} | ||
handler(writer, request, user) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/itishrishikesh/gofeed/constants" | ||
"github.com/itishrishikesh/gofeed/internal/database" | ||
) | ||
|
||
func (apiCfg *apiConfig) createFeedHandler(writer http.ResponseWriter, request *http.Request, user database.User) { | ||
type parameters struct { | ||
Name string `json:"name"` | ||
URL string `json:"url"` | ||
} | ||
params := parameters{} | ||
decoder := json.NewDecoder(request.Body) | ||
err := decoder.Decode(¶ms) | ||
if err != nil { | ||
respondWithError(writer, constants.HTTP_BAD_REQUEST, "Error parsing JSON") | ||
log.Println("D#1OV4W5 - User passed incorrect JSON") | ||
return | ||
} | ||
feed, err := apiCfg.DB.CreateFeed(request.Context(), database.CreateFeedParams{ | ||
ID: uuid.New(), | ||
CreatedAt: time.Now(), | ||
UpdatedAt: time.Now(), | ||
Name: params.Name, | ||
Url: params.URL, | ||
UserID: user.ID, | ||
}) | ||
if err != nil { | ||
respondWithError(writer, constants.HTTP_BAD_REQUEST, fmt.Sprintf("E#1OV5DR - Couldn't create feed %v", err)) | ||
return | ||
} | ||
|
||
respondWithJSON(writer, constants.HTTP_CREATED, databaseFeedToFeed(feed)) | ||
} | ||
|
||
func (config *apiConfig) getFeedsHandler(writer http.ResponseWriter, request *http.Request) { | ||
feeds, err := config.DB.GetFeed(request.Context()) | ||
if err != nil { | ||
respondWithError(writer, constants.HTTP_BAD_REQUEST, fmt.Sprintf("E#1OV63D - Couldn't get feed %v", err)) | ||
return | ||
} | ||
respondWithJSON(writer, constants.HTTP_CREATED, databaseFeedToFeeds(feeds)) | ||
} | ||
|
||
func (config *apiConfig) createFeedFollowHandler(writer http.ResponseWriter, request *http.Request, user database.User) { | ||
type parameters struct { | ||
FeedID uuid.UUID `json:"feed_id"` | ||
} | ||
params := parameters{} | ||
decoder := json.NewDecoder(request.Body) | ||
err := decoder.Decode(¶ms) | ||
if err != nil { | ||
respondWithError(writer, constants.HTTP_BAD_REQUEST, "Error parsing JSON") | ||
log.Println("D#1OV7DL - User passed incorrect JSON") | ||
return | ||
} | ||
feedFollow, err := config.DB.CreateFeedFollow(request.Context(), database.CreateFeedFollowParams{ | ||
ID: uuid.New(), | ||
CreatedAt: time.Now(), | ||
UpdatedAt: time.Now(), | ||
UserID: user.ID, | ||
FeedID: params.FeedID, | ||
}) | ||
if err != nil { | ||
respondWithError(writer, constants.HTTP_BAD_REQUEST, fmt.Sprintf("E#1OV7DV - Couldn't create feed follow %v", err)) | ||
return | ||
} | ||
respondWithJSON(writer, constants.HTTP_CREATED, databaseFeedFollowToFeedFollow(feedFollow)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- name: CreateFeed :one | ||
INSERT INTO feeds (id, created_at, updated_at, name, url, user_id) | ||
VALUES ($1, $2, $3, $4, $5, $6) | ||
RETURNING *; | ||
|
||
-- name: GetFeed :many | ||
SELECT * from feeds; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- name: CreateFeedFollow :one | ||
INSERT INTO feed_follows (id, created_at, updated_at, user_id, feed_id) | ||
VALUES ($1, $2, $3, $4, $5) | ||
RETURNING *; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- +goose Up | ||
CREATE TABLE feeds ( | ||
id UUID PRIMARY KEY, | ||
created_at TIMESTAMP NOT NULL, | ||
updated_at TIMESTAMP NOT NULL, | ||
name TEXT NOT NULL, | ||
url TEXT UNIQUE NOT NULL, | ||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE | ||
); | ||
|
||
-- +goose Down | ||
DROP TABLE feeds; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- +goose Up | ||
CREATE TABLE feed_follows ( | ||
id UUID PRIMARY KEY, | ||
created_at TIMESTAMP NOT NULL, | ||
updated_at TIMESTAMP NOT NULL, | ||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, | ||
feed_id UUID NOT NULL REFERENCES feeds(id) ON DELETE CASCADE, | ||
UNIQUE(user_id, feed_id) | ||
); | ||
|
||
-- +goose Down | ||
DROP TABLE feed_follows; |
Oops, something went wrong.