Remove 'go-playground/form' dependency; just use JSON and 'hx-ext="json-enc"' in all forms
This commit is contained in:
parent
c6ec9be562
commit
42ab53fb73
1
go.mod
1
go.mod
@ -5,7 +5,6 @@ go 1.16
|
||||
require (
|
||||
github.com/Masterminds/sprig/v3 v3.2.3
|
||||
github.com/andybalholm/cascadia v1.3.2
|
||||
github.com/go-playground/form/v4 v4.2.1
|
||||
github.com/go-test/deep v1.0.7
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/jarcoal/httpmock v1.1.0
|
||||
|
4
go.sum
4
go.sum
@ -9,10 +9,6 @@ github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/form/v4 v4.2.1 h1:HjdRDKO0fftVMU5epjPW2SOREcZ6/wLUzEobqUGJuPw=
|
||||
github.com/go-playground/form/v4 v4.2.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U=
|
||||
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
|
||||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
|
||||
|
@ -2,6 +2,8 @@ package webserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
||||
@ -13,10 +15,11 @@ type LoginData struct {
|
||||
}
|
||||
|
||||
type LoginForm struct {
|
||||
Username string `form:"username"`
|
||||
Password string `form:"password"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
FormErrors
|
||||
}
|
||||
type FormErrors map[string]string
|
||||
|
||||
func (f *LoginForm) Validate() {
|
||||
if f.FormErrors == nil {
|
||||
@ -34,11 +37,9 @@ func (app *Application) Login(w http.ResponseWriter, r *http.Request) {
|
||||
app.traceLog.Printf("'Login' handler (path: %q)", r.URL.Path)
|
||||
var form LoginForm
|
||||
if r.Method == "POST" {
|
||||
err := parse_form(r, &form)
|
||||
if err != nil {
|
||||
app.InfoLog.Print("Form error parse: " + err.Error())
|
||||
app.error_400_with_message(w, err.Error())
|
||||
}
|
||||
data, err := io.ReadAll(r.Body)
|
||||
panic_if(err)
|
||||
panic_if(json.Unmarshal(data, &form)) // TODO: HTTP 400 not 500
|
||||
form.Validate()
|
||||
if len(form.FormErrors) == 0 {
|
||||
api := scraper.NewGuestSession()
|
||||
@ -104,14 +105,11 @@ func (app *Application) after_login(w http.ResponseWriter, r *http.Request, api
|
||||
func (app *Application) ChangeSession(w http.ResponseWriter, r *http.Request) {
|
||||
app.traceLog.Printf("'change-session' handler (path: %q)", r.URL.Path)
|
||||
form := struct {
|
||||
AccountName string `form:"account"`
|
||||
AccountName string `json:"account"`
|
||||
}{}
|
||||
err := parse_form(r, &form)
|
||||
if err != nil {
|
||||
app.InfoLog.Print("Form error parse: " + err.Error())
|
||||
app.error_400_with_message(w, err.Error())
|
||||
return
|
||||
}
|
||||
data, err := io.ReadAll(r.Body)
|
||||
panic_if(err)
|
||||
panic_if(json.Unmarshal(data, &form)) // TODO: HTTP 400 not 500
|
||||
err = app.SetActiveUser(scraper.UserHandle(form.AccountName))
|
||||
if err != nil {
|
||||
app.error_400_with_message(w, fmt.Sprintf("User not in database: %s", form.AccountName))
|
||||
|
@ -3,7 +3,6 @@ package webserver
|
||||
import (
|
||||
"crypto/tls"
|
||||
// "encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@ -15,8 +14,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/form/v4"
|
||||
|
||||
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/persistence"
|
||||
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
||||
)
|
||||
@ -183,23 +180,3 @@ func parse_cursor_value(c *persistence.Cursor, r *http.Request) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type FormErrors map[string]string
|
||||
|
||||
var formDecoder = form.NewDecoder()
|
||||
var (
|
||||
ErrCorruptedFormData = errors.New("corrupted form data")
|
||||
ErrIncorrectFormParams = errors.New("incorrect form parameters")
|
||||
)
|
||||
|
||||
func parse_form(req *http.Request, result interface{}) error {
|
||||
err := req.ParseForm()
|
||||
if err != nil {
|
||||
return ErrCorruptedFormData
|
||||
}
|
||||
|
||||
if err = formDecoder.Decode(result, req.PostForm); err != nil {
|
||||
return ErrIncorrectFormParams
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
{{define "main"}}
|
||||
<div class="login">
|
||||
<form hx-post="/change-session" hx-target=".nav-sidebar" hx-swap="outerHTML">
|
||||
<form hx-post="/change-session" hx-target=".nav-sidebar" hx-swap="outerHTML" hx-ext="json-enc">
|
||||
<label for="select-account">Choose account:</label>
|
||||
<select name="account" id="select-account">
|
||||
{{range .ExistingSessions}}
|
||||
@ -17,7 +17,7 @@
|
||||
|
||||
<p>Or log in</p>
|
||||
|
||||
<form class="login-form" hx-post="/login" hx-target="body">
|
||||
<form class="login-form" hx-post="/login" hx-target="body" hx-ext="json-enc">
|
||||
<div class="field-container">
|
||||
<label>Username</label>
|
||||
{{with .FormErrors.username}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user