Compare commits
3 Commits
a10358ff91
...
f39cfee6e8
Author | SHA1 | Date | |
---|---|---|---|
f39cfee6e8 | |||
83eb5581d4 | |||
b55ad0a99b |
57
apiclient/articles.go
Normal file
57
apiclient/articles.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package apiclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-api/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ArticlesBaseRoute = "api/v1/articles"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Articles interface {
|
||||||
|
List(jwt string, page int) (domain.ArticleResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type articlesClient struct {
|
||||||
|
serverAddress string
|
||||||
|
client http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func newArticleService(serverAddress string) articlesClient {
|
||||||
|
return articlesClient{
|
||||||
|
serverAddress: serverAddress,
|
||||||
|
client: http.Client{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c articlesClient) List(jwt string, page int) (domain.ArticleResponse, error) {
|
||||||
|
endpoint := fmt.Sprintf("%s/%s?page=%U", c.serverAddress, ArticlesBaseRoute, page)
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
||||||
|
if err != nil {
|
||||||
|
return domain.ArticleResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
//req.Header.Set(HeaderContentType, ApplicationJson)
|
||||||
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt))
|
||||||
|
resp, err := c.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return domain.ArticleResponse{}, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var bind domain.ArticleResponse
|
||||||
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
err = decoder.Decode(&bind)
|
||||||
|
if err != nil {
|
||||||
|
return domain.ArticleResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return bind, nil
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -3,14 +3,17 @@ package apiclient
|
|||||||
const (
|
const (
|
||||||
HeaderContentType = "Content-Type"
|
HeaderContentType = "Content-Type"
|
||||||
MIMEApplicationForm = "application/x-www-form-urlencoded"
|
MIMEApplicationForm = "application/x-www-form-urlencoded"
|
||||||
|
ApplicationJson = "application/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiClient struct {
|
type ApiClient struct {
|
||||||
|
Articles Articles
|
||||||
Users Users
|
Users Users
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(serverAddress string) ApiClient {
|
func New(serverAddress string) ApiClient {
|
||||||
return ApiClient{
|
return ApiClient{
|
||||||
|
Articles: newArticleService(serverAddress),
|
||||||
Users: newUserService(serverAddress),
|
Users: newUserService(serverAddress),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UserLoginRoute = "api/v1/users/login"
|
UserBaseRoute = "api/v1/users"
|
||||||
UserRegisterRoute = "api/v1/users/register"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Users interface {
|
type Users interface {
|
||||||
@ -31,7 +30,7 @@ func newUserService(serverAddress string) userClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a userClient) Login(username, password string) (domain.LoginResponse, error) {
|
func (a userClient) Login(username, password string) (domain.LoginResponse, error) {
|
||||||
endpoint := fmt.Sprintf("%s/%s", a.serverAddress, UserLoginRoute)
|
endpoint := fmt.Sprintf("%s/%s/login", a.serverAddress, UserBaseRoute)
|
||||||
|
|
||||||
param := url.Values{}
|
param := url.Values{}
|
||||||
param.Set("username", username)
|
param.Set("username", username)
|
||||||
@ -48,7 +47,7 @@ func (a userClient) Login(username, password string) (domain.LoginResponse, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a userClient) SignUp(username, password string) (domain.BaseResponse, error) {
|
func (a userClient) SignUp(username, password string) (domain.BaseResponse, error) {
|
||||||
endpoint := fmt.Sprintf("%s/%s", a.serverAddress, UserRegisterRoute)
|
endpoint := fmt.Sprintf("%s/%s/register", a.serverAddress, UserBaseRoute)
|
||||||
|
|
||||||
param := url.Values{}
|
param := url.Values{}
|
||||||
param.Set("username", username)
|
param.Set("username", username)
|
||||||
|
28
internal/handlers/articles.go
Normal file
28
internal/handlers/articles.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/articles"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handler) ArticlesList(c echo.Context) error {
|
||||||
|
userToken, err := c.Cookie(domain.CookieToken)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := h.api.Articles.List(userToken.Value, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
vm := models.ListArticlesViewModel {
|
||||||
|
Items: resp.Payload,
|
||||||
|
}
|
||||||
|
|
||||||
|
return Render(c, http.StatusOK, articles.List(vm))
|
||||||
|
}
|
23
internal/handlers/debug.go
Normal file
23
internal/handlers/debug.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/debug"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handler) DebugCookies(c echo.Context) error {
|
||||||
|
user, _ := c.Cookie(domain.CookieUser)
|
||||||
|
token, _ := c.Cookie(domain.CookieToken)
|
||||||
|
refresh, _ := c.Cookie(domain.CookieRefreshToken)
|
||||||
|
|
||||||
|
model := models.DebugCookiesViewModel{
|
||||||
|
Username: user.Value,
|
||||||
|
Token: token.Value,
|
||||||
|
RefreshToken: refresh.Value,
|
||||||
|
}
|
||||||
|
return Render(c, http.StatusOK, debug.Cookies(model))
|
||||||
|
}
|
@ -60,6 +60,12 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.
|
|||||||
router.GET("/", s.HomeIndex)
|
router.GET("/", s.HomeIndex)
|
||||||
router.GET("/about", s.HomeAbout)
|
router.GET("/about", s.HomeAbout)
|
||||||
|
|
||||||
|
debug := router.Group("/debug")
|
||||||
|
debug.GET("/cookies", s.DebugCookies)
|
||||||
|
|
||||||
|
articles := router.Group("/articles")
|
||||||
|
articles.GET("", s.ArticlesList)
|
||||||
|
|
||||||
users := router.Group("/users")
|
users := router.Group("/users")
|
||||||
users.GET("/login", s.UserLogin)
|
users.GET("/login", s.UserLogin)
|
||||||
users.POST("/login", s.UserAfterLogin)
|
users.POST("/login", s.UserAfterLogin)
|
||||||
|
@ -22,20 +22,9 @@ func (h *Handler) UserAfterLogin(c echo.Context) error {
|
|||||||
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
|
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
|
||||||
}
|
}
|
||||||
|
|
||||||
cookie := new(http.Cookie)
|
SetCookie(c, domain.CookieToken, resp.Token)
|
||||||
cookie.Name = domain.CookieToken
|
SetCookie(c, domain.CookieRefreshToken, resp.RefreshToken)
|
||||||
cookie.Value = resp.Token
|
SetCookie(c, domain.CookieUser, user)
|
||||||
c.SetCookie(cookie)
|
|
||||||
|
|
||||||
cookie = new(http.Cookie)
|
|
||||||
cookie.Name = domain.CookieRefreshToken
|
|
||||||
cookie.Value = resp.RefreshToken
|
|
||||||
c.SetCookie(cookie)
|
|
||||||
|
|
||||||
cookie = new(http.Cookie)
|
|
||||||
cookie.Name = domain.CookieUser
|
|
||||||
cookie.Value = user
|
|
||||||
c.SetCookie(cookie)
|
|
||||||
|
|
||||||
return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true))
|
return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true))
|
||||||
}
|
}
|
||||||
|
14
internal/handlers/util.go
Normal file
14
internal/handlers/util.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetCookie(c echo.Context, key string, value string) {
|
||||||
|
cookie := new(http.Cookie)
|
||||||
|
cookie.Name = key
|
||||||
|
cookie.Value = value
|
||||||
|
c.SetCookie(cookie)
|
||||||
|
}
|
7
internal/models/articles.go
Normal file
7
internal/models/articles.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "git.jamestombleson.com/jtom38/newsbot-api/domain"
|
||||||
|
|
||||||
|
type ListArticlesViewModel struct {
|
||||||
|
Items []domain.ArticleDto
|
||||||
|
}
|
7
internal/models/debug.go
Normal file
7
internal/models/debug.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type DebugCookiesViewModel struct {
|
||||||
|
Username string
|
||||||
|
Token string
|
||||||
|
RefreshToken string
|
||||||
|
}
|
15
internal/views/articles/list.templ
Normal file
15
internal/views/articles/list.templ
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package articles
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
templ List(model models.ListArticlesViewModel) {
|
||||||
|
@layout.WithTemplate() {
|
||||||
|
for _, item := range model.Items {
|
||||||
|
{ item.Title }
|
||||||
|
}
|
||||||
|
<div>hi </div>
|
||||||
|
}
|
||||||
|
}
|
12
internal/views/debug/cookies.templ
Normal file
12
internal/views/debug/cookies.templ
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package debug
|
||||||
|
|
||||||
|
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
|
||||||
|
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
|
||||||
|
|
||||||
|
templ Cookies(vm models.DebugCookiesViewModel) {
|
||||||
|
@layout.WithTemplate() {
|
||||||
|
Token: { vm.Token }
|
||||||
|
RefreshToken: { vm.RefreshToken }
|
||||||
|
UserName: { vm.Username }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user