Compare commits

...

6 Commits

17 changed files with 283 additions and 41 deletions

3
.gitignore vendored
View File

@ -25,4 +25,5 @@ go.work
.vscode
*_templ.go
server
server
.env

16
apiclient/client.go Normal file
View File

@ -0,0 +1,16 @@
package apiclient
const (
HeaderContentType = "Content-Type"
MIMEApplicationForm = "application/x-www-form-urlencoded"
)
type ApiClient struct {
Users Users
}
func New(serverAddress string) ApiClient {
return ApiClient{
Users: newUserService(serverAddress),
}
}

47
apiclient/users.go Normal file
View File

@ -0,0 +1,47 @@
package apiclient
import (
"fmt"
"net/http"
"net/url"
"git.jamestombleson.com/jtom38/newsbot-api/domain"
)
const (
UserLoginRoute = "api/v1/users/login"
UserRegisterRoute = "api/v1/users/register"
)
type Users interface {
Login(username, password string) (domain.LoginResponse, error)
}
type userClient struct {
serverAddress string
client http.Client
}
func newUserService(serverAddress string) userClient {
return userClient{
serverAddress: serverAddress,
client: http.Client{},
}
}
func (a userClient) Login(username, password string) (domain.LoginResponse, error) {
endpoint := fmt.Sprintf("%s/%s", a.serverAddress, UserLoginRoute)
param := url.Values{}
param.Set("username", username)
param.Set("password", password)
// Create the struct we are expecting back from the API so we can have it populated
var bind = domain.LoginResponse{}
err := PostUrlForm(a.client, endpoint, param, &bind)
if err != nil {
return domain.LoginResponse{}, err
}
return bind, nil
}

31
apiclient/util.go Normal file
View File

@ -0,0 +1,31 @@
package apiclient
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
)
func PostUrlForm(client http.Client, endpoint string, param url.Values, t any) error {
payload := bytes.NewBufferString(param.Encode())
req, err := http.NewRequest(http.MethodPost, endpoint, payload)
if err != nil {
return err
}
req.Header.Set(HeaderContentType, MIMEApplicationForm)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&t)
if err != nil {
return err
}
return nil
}

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"git.jamestombleson.com/jtom38/newsbot-portal/apiclient"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/config"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/handlers"
)
@ -12,7 +13,8 @@ func main() {
ctx := context.Background()
cfg := config.New()
server := handlers.NewServer(ctx, cfg)
apiClient := apiclient.New(cfg.ServerAddress)
server := handlers.NewServer(ctx, cfg, apiClient)
fmt.Println("The server is online and waiting for requests.")
fmt.Printf("http://%v:8082\r\n", cfg.ServerAddress)

5
go.mod
View File

@ -3,14 +3,14 @@ module git.jamestombleson.com/jtom38/newsbot-portal
go 1.22.1
require (
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240508052157-5b8cf6dfa6cc
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240510021003-4e9a17209f02
github.com/a-h/templ v0.2.680
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.12.0
)
require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/a-h/templ v0.2.680 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
@ -25,7 +25,6 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect

6
go.sum
View File

@ -1,5 +1,5 @@
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240508052157-5b8cf6dfa6cc h1:QhuOntLiCv/kn+ymg/Qw7yKwXX+lPGRjgw8/saqfAeE=
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240508052157-5b8cf6dfa6cc/go.mod h1:A3UdJyQ/IEy3utEwJiC4nbi0ohfgrUNRLTei2iZhLLA=
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240510021003-4e9a17209f02 h1:yvu0Fnpw19YUH+AXvMQxJV8mUyEXkvpZqd8HctpwMrI=
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240510021003-4e9a17209f02/go.mod h1:A3UdJyQ/IEy3utEwJiC4nbi0ohfgrUNRLTei2iZhLLA=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/a-h/templ v0.2.680 h1:TflYFucxp5rmOxAXB9Xy3+QHTk8s8xG9+nCT/cLzjeE=
@ -21,6 +21,8 @@ github.com/go-openapi/swag v0.21.1 h1:wm0rhTb5z7qpJRHBdPOMuY4QjVUMbF6/kwoYeRAOrK
github.com/go-openapi/swag v0.21.1/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=

View File

@ -10,7 +10,6 @@ import (
type Configs struct {
ServerAddress string
JwtSecret string
AdminSecret string
}
func New() Configs {
@ -23,7 +22,6 @@ func getEnvConfig() Configs {
return Configs{
ServerAddress: os.Getenv("ServerAddress"),
JwtSecret: os.Getenv("JwtSecret"),
AdminSecret: os.Getenv("AdminSecret"),
}
}

View File

@ -0,0 +1,7 @@
package domain
const (
CookieToken = "token"
CookieRefreshToken = "refreshToken"
CookieUser = "user"
)

View File

@ -7,16 +7,10 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "git.jamestombleson.com/jtom38/newsbot-api/docs"
"git.jamestombleson.com/jtom38/newsbot-portal/apiclient"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/config"
//"git.jamestombleson.com/jtom38/newsbot-api/domain"
)
type Handler struct {
Router *echo.Echo
config config.Configs
}
const (
ErrParameterIdMissing = "The requested parameter ID was not found."
ErrParameterMissing = "The requested parameter was not found found:"
@ -39,9 +33,16 @@ var (
ErrUnableToConvertToJson string = "Unable to convert to json"
)
func NewServer(ctx context.Context, configs config.Configs) *Handler {
type Handler struct {
Router *echo.Echo
config config.Configs
api apiclient.ApiClient
}
func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.ApiClient) *Handler {
s := &Handler{
config: configs,
api: apiClient,
}
//jwtConfig := echojwt.Config{
@ -57,6 +58,11 @@ func NewServer(ctx context.Context, configs config.Configs) *Handler {
router.Pre(middleware.Recover())
router.GET("/", s.HomeIndex)
router.GET("/about", s.HomeAbout)
users := router.Group("/users")
users.GET("/login", s.UserLogin)
users.POST("/login", s.UserAfterLogin)
s.Router = router
return s

View File

@ -9,4 +9,8 @@ import (
func (h *Handler) HomeIndex(c echo.Context) error {
return Render(c, http.StatusOK, home.Index())
}
func (h *Handler) HomeAbout(c echo.Context) error {
return Render(c, http.StatusOK, home.About())
}

View File

@ -0,0 +1,40 @@
package handlers
import (
"net/http"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/users"
"github.com/labstack/echo/v4"
)
func (h *Handler) UserLogin(c echo.Context) error {
return Render(c, http.StatusOK, users.Login())
}
func (h *Handler) UserAfterLogin(c echo.Context) error {
user := c.FormValue("username")
password := c.FormValue("password")
resp, err := h.api.Users.Login(user, password)
if err != nil {
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
}
cookie := new(http.Cookie)
cookie.Name = domain.CookieToken
cookie.Value = resp.Token
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))
}

View File

@ -0,0 +1,15 @@
package home
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
templ About() {
@layout.WithTemplate(){
<h1 class="title"> About this project</h1>
<section class="section">
Newsbot started a small project to send out notifications to discord servers.
I wanted to be able to keep the small communitiy aware of new posts about a game we all played.
That feature still exists because I think that keeping a communitiy aware and engaged is important and not everyone follows the same news.
</section>
}
}

View File

@ -9,15 +9,19 @@ templ Index() {
<section class="section">
<p>
News bot is a self hostable solution to aggregating your news.
You can run `Newsbot` as an API
You can run `Newsbot` as an API or interact with it with this site.
</p>
</section>
<div class="block">
<h2 class="title">Why Newsbot</h2>
I started to build this tool to help me avoid sitting on the big platform websites.
I wanted a tool that would work for me, not them.
This tool started as a notification system that would let me redirect RSS posts over to Discord servers.
It still has those roots but now its starting to scale up to a full Aggregation platform.
</div>
<p>
This project is a passion project of mine as I

View File

@ -5,6 +5,7 @@ templ WithTemplate() {
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.0/css/bulma.min.css"/>
<script src="https://unpkg.com/htmx.org@1.9.11" integrity="sha384-0gxUXCCR8yv9FM2b+U3FDbsKthCI66oH5IA9fHppQq9DDMHuMauqq1ZHBpJxQ0J0" crossorigin="anonymous"></script>
<meta charset="utf-8"/>
<meta property="og:title" content=""/>
<meta property="og:url" content=""/>
@ -13,31 +14,62 @@ templ WithTemplate() {
<meta property="og:type" content=""/>
</head>
<body>
<!-- Main container -->
<nav class="level">
<!-- Left side -->
<div class="level-left">
<div class="level-item">
<p class="subtitle is-5"><strong>Newsbot</strong></p>
</div>
<div class="level-item">
<div class="field has-addons">
<p class="control">
<input class="input" type="text" placeholder="Find a post"/>
</p>
<p class="control">
<button class="button">Search</button>
</p>
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/">
<svg width="640" height="160" viewBox="0 0 640 160" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M170 132.571V27.5908C170 25.5451 170.915 23.93 172.746 22.7456C174.576 21.5612 176.729 20.969 179.206 20.969H210.377C232.019 20.969 242.84 30.4441 242.84 49.3943C242.84 62.5303 238.264 71.0902 229.112 75.074C234.603 77.2275 238.748 80.2692 241.548 84.1992C244.347 88.1292 245.747 93.8627 245.747 101.4V104.791C245.747 116.743 242.84 125.437 237.026 130.875C231.211 136.312 223.351 139.031 213.445 139.031H179.206C176.514 139.031 174.307 138.385 172.584 137.093C170.861 135.801 170 134.293 170 132.571ZM190.834 120.619H209.085C219.529 120.619 224.751 114.751 224.751 103.015V100.431C224.751 94.401 223.432 90.0404 220.794 87.3486C218.156 84.6568 214.253 83.3109 209.085 83.3109H190.834V120.619ZM190.834 66.8371H208.923C213.122 66.8371 216.326 65.5989 218.533 63.1225C220.74 60.646 221.844 57.2544 221.844 52.9475C221.844 48.7483 220.686 45.4374 218.371 43.0148C216.057 40.5922 212.853 39.3809 208.762 39.3809H190.834V66.8371ZM260.283 103.015V27.4293C260.283 25.2759 261.306 23.6608 263.351 22.5841C265.397 21.5074 267.873 20.969 270.781 20.969C273.688 20.969 276.164 21.5074 278.21 22.5841C280.256 23.6608 281.279 25.2759 281.279 27.4293V103.015C281.279 115.397 287.2 121.588 299.044 121.588C310.888 121.588 316.81 115.397 316.81 103.015V27.4293C316.81 25.2759 317.833 23.6608 319.879 22.5841C321.925 21.5074 324.401 20.969 327.308 20.969C330.215 20.969 332.692 21.5074 334.738 22.5841C336.783 23.6608 337.806 25.2759 337.806 27.4293V103.015C337.806 115.72 334.28 125.061 327.227 131.036C320.175 137.012 310.781 140 299.044 140C287.308 140 277.914 137.039 270.861 131.117C263.809 125.195 260.283 115.828 260.283 103.015ZM356.703 132.409V27.4293C356.703 25.2759 357.725 23.6608 359.771 22.5841C361.817 21.5074 364.293 20.969 367.201 20.969C370.108 20.969 372.584 21.5074 374.63 22.5841C376.676 23.6608 377.699 25.2759 377.699 27.4293V120.619H417.106C419.044 120.619 420.579 121.534 421.709 123.365C422.84 125.195 423.405 127.349 423.405 129.825C423.405 132.301 422.84 134.455 421.709 136.285C420.579 138.116 419.044 139.031 417.106 139.031H365.908C363.432 139.031 361.279 138.439 359.448 137.254C357.618 136.07 356.703 134.455 356.703 132.409ZM434.872 132.409V31.467C434.872 27.9138 435.868 25.2759 437.86 23.5532C439.852 21.8304 442.355 20.969 445.37 20.969C449.354 20.969 452.423 21.6689 454.576 23.0686C456.729 24.4684 459.098 27.4832 461.682 32.1131L481.548 68.2907L501.413 32.1131C503.997 27.4832 506.393 24.4684 508.6 23.0686C510.808 21.6689 513.903 20.969 517.887 20.969C520.902 20.969 523.405 21.8304 525.397 23.5532C527.389 25.2759 528.385 27.9138 528.385 31.467V132.409C528.385 134.455 527.335 136.07 525.236 137.254C523.136 138.439 520.686 139.031 517.887 139.031C514.98 139.031 512.503 138.439 510.458 137.254C508.412 136.07 507.389 134.455 507.389 132.409V62.961L488.493 96.5545C486.985 99.354 484.616 100.754 481.386 100.754C478.264 100.754 475.949 99.354 474.441 96.5545L455.868 61.6689V132.409C455.868 134.455 454.818 136.07 452.719 137.254C450.619 138.439 448.17 139.031 445.37 139.031C442.463 139.031 439.987 138.439 437.941 137.254C435.895 136.07 434.872 134.455 434.872 132.409ZM539.529 130.31C539.529 130.094 539.637 129.556 539.852 128.694L571.023 27.1063C571.669 24.8452 573.257 23.0956 575.787 21.8573C578.318 20.6191 581.198 20 584.428 20C587.658 20 590.565 20.6191 593.149 21.8573C595.734 23.0956 597.349 24.8452 597.995 27.1063L629.166 128.694C629.381 129.556 629.489 130.094 629.489 130.31C629.489 132.678 628.035 134.724 625.128 136.447C622.221 138.17 619.26 139.031 616.245 139.031C612.261 139.031 609.892 137.631 609.139 134.832L603.001 113.351H566.016L559.879 134.832C559.125 137.631 556.756 139.031 552.773 139.031C549.65 139.031 546.662 138.197 543.809 136.528C540.956 134.859 539.529 132.786 539.529 130.31ZM570.377 96.8775H598.479L584.428 47.2948L570.377 96.8775Z" fill="black" class="bd-svg-black"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M0 110L10 40L50 0L100 50L70 80L110 120L50 160L0 110Z" fill="#00D1B2"></path>
</svg>
</a>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item">
Home
</a>
<a class="navbar-item">
Documentation
</a>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
More
</a>
<div class="navbar-dropdown">
<a class="navbar-item">
About
</a>
<a class="navbar-item is-selected">
Jobs
</a>
<a class="navbar-item">
Contact
</a>
<hr class="navbar-divider"/>
<a class="navbar-item">
Report an issue
</a>
</div>
</div>
</div>
<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary">
<strong>Sign up</strong>
</a>
<a class="button is-light" href="/users/login">
Log in
</a>
</div>
</div>
</div>
</div>
<!-- Right side -->
<div class="level-right">
<p class="level-item"><strong>All</strong></p>
<p class="level-item"><a>Published</a></p>
<p class="level-item"><a>Drafts</a></p>
<p class="level-item"><a>Deleted</a></p>
<p class="level-item"><a class="button is-success">New</a></p>
</div>
</nav>
<div class="container is-widescreen">

View File

@ -0,0 +1,15 @@
package users
// This is returned after the user logs into the application.
// It just returns a partial view because it will overlap with the existing template.
templ AfterLogin(message string, success bool) {
if success {
<div class="notification is-success">
{ message }
</div>
} else {
<div class="notification is-error">
{ message }
</div>
}
}

View File

@ -0,0 +1,23 @@
package users
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
templ Login() {
@layout.WithTemplate() {
<form hx-post="/users/login">
<div class="field">
<label class="label">Username</label>
<div class="control">
<input class="input" type="text" name="username" id="username"/>
</div>
</div>
<div class="field">
<label class="label">Password</label>
<div class="control">
<input class="input" type="password" name="password" id="exampleInputPassword1"/>
</div>
</div>
<button type="submit" class="button is-primary">Submit</button>
</form>
}
}