From 448fda02a8172e5939487e9ceeacf02738f2a0d3 Mon Sep 17 00:00:00 2001 From: Tyr Mactire Date: Thu, 1 Sep 2022 13:59:04 -0700 Subject: [PATCH] common login (#124) --- .github/workflows/pull-request.yml | 7 +--- Makefile | 21 ++++------ internal/http/template/common.go | 4 +- internal/http/template/login.go | 56 +++++++++++++++++++++++---- internal/http/template/navbar.go | 8 +--- internal/http/template/template.go | 2 +- internal/http/webapp/login.go | 61 ++++++++++++++++++------------ internal/http/webapp/template.go | 37 +++++++++--------- internal/http/webapp/webapp.go | 3 +- test/test-config.yml | 12 ------ web/template/login.gohtml | 58 ---------------------------- web/template/navbar.gohtml | 6 +-- web/web.go | 1 + 13 files changed, 124 insertions(+), 152 deletions(-) delete mode 100644 web/template/login.gohtml diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index b3ac81b..3b1df58 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -24,12 +24,7 @@ jobs: go-version-file: 'go.mod' cache: true - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - name: Run GoReleaser uses: goreleaser/goreleaser-action@v3 with: - args: build --rm-dist --snapshot \ No newline at end of file + args: build --rm-dist --snapshot --single-target \ No newline at end of file diff --git a/Makefile b/Makefile index 70c165f..84f2a1e 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,9 @@ clean: @rm -Rvf .cache coverage.txt dist relay @find . -name ".DS_Store" -exec rm -v {} \; +docker-pull: + docker-compose --project-name ${PROJECT_NAME} -f deployments/docker-compose-test.yaml pull + docker-restart: docker-stop docker-start docker-start: @@ -31,26 +34,16 @@ fmt: i18n-extract: goi18n extract -format yaml -outdir web/locales -crowdin -i18n-merge: - goi18n merge -format yaml -outdir locales web/locales/active.*.toml web/locales/translate.*.toml -crowdin - -i18n-translations: - goi18n merge -format yaml -outdir locales web/locales/active.*.toml -crowdin - -lint: - @echo linting - @golint $(shell go list ./... | grep -v /vendor/) - release: clean goreleaser release -test: tidy fmt lint #gosec +test: tidy fmt go test -cover ./... -test-ext: tidy fmt lint #gosec +test-ext: tidy fmt go test --tags=postgres -cover ./... -test-bench-ext: tidy fmt lint #gosec +test-bench-ext: tidy fmt go test -run=XXX -bench=. --tags=postgres -cover ./... tidy: @@ -59,4 +52,4 @@ tidy: vendor: tidy go mod vendor -.PHONY: build-snapshot bun-new-migration clean fmt i18n-extract i18n-merge i18n-translations lint stage-static npm-scss npm-upgrade docker-restart docker-start docker-stop release stage-static test test-ext test-race test-race-ext test-verbose tidy vendor +.PHONY: build-snapshot bun-new-migration clean docker-pull docker-restart docker-start docker-stop fmt i18n-extract release stage-static test test-ext test-bench-ext tidy vendor diff --git a/internal/http/template/common.go b/internal/http/template/common.go index 7610147..f3c5d54 100644 --- a/internal/http/template/common.go +++ b/internal/http/template/common.go @@ -18,7 +18,7 @@ type Common struct { FooterExtraScript []string HeadLinks []libtemplate.HeadLink - NavBar Navbar + NavBar libtemplate.Navbar NavBarDark bool PageTitle string } @@ -58,7 +58,7 @@ func (t *Common) SetLocalizer(l *language.Localizer) { } // SetNavbar sets the top level navbar used by the template. -func (t *Common) SetNavbar(nodes Navbar) { +func (t *Common) SetNavbar(nodes libtemplate.Navbar) { t.NavBar = nodes } diff --git a/internal/http/template/login.go b/internal/http/template/login.go index 0075b83..a92758f 100644 --- a/internal/http/template/login.go +++ b/internal/http/template/login.go @@ -1,12 +1,54 @@ package template -// LoginName is the name of the login template. -const LoginName = "login" +import ( + libtemplate "github.com/feditools/go-lib/template" + "github.com/feditools/relay/internal/language" + "github.com/feditools/relay/internal/models" +) -// Login contains the variables for the "login" template. type Login struct { - Common - - FormError string - FormInstance string + libtemplate.Login } + +// AddHeadLink adds a headder link to the template. +func (t *Login) AddHeadLink(l libtemplate.HeadLink) { + if t.HeadLinks == nil { + t.HeadLinks = []libtemplate.HeadLink{} + } + t.HeadLinks = append(t.HeadLinks, l) +} + +// AddFooterScript adds a footer script to the template. +func (t *Login) AddFooterScript(s libtemplate.Script) { + if t.FooterScripts == nil { + t.FooterScripts = []libtemplate.Script{} + } + t.FooterScripts = append(t.FooterScripts, s) +} + +// AddFooterExtraScript adds a footer script to the template. +func (t *Login) AddFooterExtraScript(s string) { + if t.FooterExtraScript == nil { + t.FooterExtraScript = []string{} + } + t.FooterExtraScript = append(t.FooterExtraScript, s) +} + +// SetLanguage sets the template's default language. +func (t *Login) SetLanguage(l string) { + t.Language = l +} + +// SetLocalizer sets the localizer the template will use to generate text. +func (t *Login) SetLocalizer(l *language.Localizer) { + t.TextLogin = l.TextLogin() +} + +// SetNavbar sets the top level navbar used by the template. +func (t *Login) SetNavbar(nodes libtemplate.Navbar) {} + +// SetNavbarDark sets the navbar theme. +func (t *Login) SetNavbarDark(dark bool) {} + +// SetAccount sets the currently logged-in account. +func (t *Login) SetAccount(account *models.Account) {} diff --git a/internal/http/template/navbar.go b/internal/http/template/navbar.go index c6b76f2..60e85ae 100644 --- a/internal/http/template/navbar.go +++ b/internal/http/template/navbar.go @@ -1,11 +1,6 @@ package template -import ( - "regexp" - - libtemplate "github.com/feditools/go-lib/template" -) - +/* // Navbar is a navbar that can be added to a page. type Navbar []NavbarNode @@ -51,3 +46,4 @@ type NavbarNode struct { Children Navbar } +*/ diff --git a/internal/http/template/template.go b/internal/http/template/template.go index 5695b80..6162df2 100644 --- a/internal/http/template/template.go +++ b/internal/http/template/template.go @@ -29,7 +29,7 @@ type InitTemplate interface { SetAccount(account *models.Account) SetLanguage(l string) SetLocalizer(l *language.Localizer) - SetNavbar(nodes Navbar) + SetNavbar(nodes libtemplate.Navbar) SetNavbarDark(dark bool) } diff --git a/internal/http/webapp/login.go b/internal/http/webapp/login.go index e2df794..d774204 100644 --- a/internal/http/webapp/login.go +++ b/internal/http/webapp/login.go @@ -2,50 +2,59 @@ package webapp import ( "errors" + libtemplate "github.com/feditools/go-lib/template" "github.com/feditools/relay/internal/db" "github.com/feditools/relay/internal/http/template" "github.com/feditools/relay/internal/language" - nethttp "net/http" + "net/http" ) // LoginGetHandler serves the login page. -func (m *Module) LoginGetHandler(w nethttp.ResponseWriter, r *nethttp.Request) { - m.displayLoginPage(w, r, "", "") +func (m *Module) LoginGetHandler(w http.ResponseWriter, r *http.Request) { + m.displayLoginPage(w, r, "", nil) } // LoginPostHandler attempts a login. -func (m *Module) LoginPostHandler(w nethttp.ResponseWriter, r *nethttp.Request) { +func (m *Module) LoginPostHandler(w http.ResponseWriter, r *http.Request) { l := logger.WithField("func", "LoginPostHandler") // parse form data if err := r.ParseForm(); err != nil { - m.returnErrorPage(w, r, nethttp.StatusInternalServerError, err.Error()) + m.returnErrorPage(w, r, http.StatusInternalServerError, err.Error()) return } // split account - formInstance := r.Form.Get(FormInstance) + domain := r.Form.Get(FormInstance) + if domain == "" { + m.displayLoginPage(w, r, "", &libtemplate.Alert{ + Color: libtemplate.ColorWarning, + Text: "Please enter an instance domain.", + }) + + return + } // get instance - instance, err := m.db.ReadInstanceByDomain(r.Context(), formInstance) + instance, err := m.db.ReadInstanceByDomain(r.Context(), domain) if err != nil && !errors.Is(err, db.ErrNoEntries) { // actual db error - m.returnErrorPage(w, r, nethttp.StatusInternalServerError, err.Error()) + m.returnErrorPage(w, r, http.StatusInternalServerError, err.Error()) return } if errors.Is(err, db.ErrNoEntries) { - instance, err = m.fedi.GenInstanceFromDomain(r.Context(), formInstance) + instance, err = m.fedi.GenInstanceFromDomain(r.Context(), domain) if err != nil { - m.returnErrorPage(w, r, nethttp.StatusInternalServerError, err.Error()) + m.returnErrorPage(w, r, http.StatusInternalServerError, err.Error()) return } err = m.db.CreateInstance(r.Context(), instance) if err != nil { - m.returnErrorPage(w, r, nethttp.StatusInternalServerError, err.Error()) + m.returnErrorPage(w, r, http.StatusInternalServerError, err.Error()) return } @@ -55,28 +64,32 @@ func (m *Module) LoginPostHandler(w nethttp.ResponseWriter, r *nethttp.Request) loginURL, err := m.logic.GetLoginURL(r.Context(), instance) if err != nil { l.Errorf("get login url: %s", err.Error()) - m.returnErrorPage(w, r, nethttp.StatusInternalServerError, err.Error()) + m.returnErrorPage(w, r, http.StatusInternalServerError, err.Error()) return } - nethttp.Redirect(w, r, loginURL.String(), nethttp.StatusFound) + http.Redirect(w, r, loginURL.String(), http.StatusFound) } -func (m *Module) displayLoginPage(w nethttp.ResponseWriter, r *nethttp.Request, account, formError string) { - // l := logger.WithField("func", "displayLoginPage") - +func (m *Module) displayLoginPage(w http.ResponseWriter, r *http.Request, instance string, formError *libtemplate.Alert) { // get localizer localizer := r.Context().Value(ContextKeyLocalizer).(*language.Localizer) // nolint // Init template variables - tmplVars := &template.Login{} + tmplVars := &template.Login{ + Login: libtemplate.Login{ + FormError: formError, + FormInstance: libtemplate.FormInput{ + Type: libtemplate.FormInputTypeText, + Name: FormInstance, + Placeholder: localizer.TextInstance().String(), + Value: instance, + Required: true, + }, + LogoHRef: m.logoSrcDark, + }, + } - tmplVars.PageTitle = localizer.TextLogin().String() - - // set form values - tmplVars.FormError = formError - tmplVars.FormInstance = account - - m.executeTemplate(w, r, template.LoginName, tmplVars) + m.executeTemplate(w, r, libtemplate.LoginName, tmplVars) } diff --git a/internal/http/webapp/template.go b/internal/http/webapp/template.go index 933f6d4..a1de3c7 100644 --- a/internal/http/webapp/template.go +++ b/internal/http/webapp/template.go @@ -2,6 +2,7 @@ package webapp import ( "bytes" + libtemplate "github.com/feditools/go-lib/template" "github.com/feditools/relay/internal/http/template" "github.com/feditools/relay/internal/language" "github.com/feditools/relay/internal/models" @@ -89,7 +90,7 @@ func (m *Module) initTemplate(_ http.ResponseWriter, r *http.Request, tmpl templ return nil } -func (m *Module) makeNavbar(r *http.Request) (template.Navbar, error) { +func (m *Module) makeNavbar(r *http.Request) (libtemplate.Navbar, error) { tctx, span := m.tracer.Start(r.Context(), "makeNavbar", m.tracerAttrs...) defer span.End() @@ -99,11 +100,11 @@ func (m *Module) makeNavbar(r *http.Request) (template.Navbar, error) { l := r.Context().Value(ContextKeyLocalizer).(*language.Localizer) // nolint // create navbar - newNavbar := template.Navbar{ + newNavbar := libtemplate.Navbar{ { Text: l.TextHomeWeb().String(), MatchStr: path.ReAppHome, - FAIcon: "house", + Icon: "house", URL: path.AppHome, }, } @@ -116,10 +117,10 @@ func (m *Module) makeNavbar(r *http.Request) (template.Navbar, error) { return nil, err } if blockCount > 0 { - newNode := template.NavbarNode{ + newNode := libtemplate.NavbarNode{ Text: l.TextBlocks().String(), MatchStr: path.ReAppBlocks, - FAIcon: "user-slash", + Icon: "user-slash", URL: path.AppBlocks, } newNavbar = append(newNavbar, newNode) @@ -130,34 +131,34 @@ func (m *Module) makeNavbar(r *http.Request) (template.Navbar, error) { if loggedIn { if account.ContactFor != nil { - newNavbar = append(newNavbar, template.NavbarNode{ + newNavbar = append(newNavbar, libtemplate.NavbarNode{ Text: l.TextInstance().String(), MatchStr: path.ReAppInstance, - FAIcon: "server", + Icon: "server", URL: path.AppInstance, }) } - newNavbar = append(newNavbar, template.NavbarNode{ + newNavbar = append(newNavbar, libtemplate.NavbarNode{ Text: l.TextSettings().String(), MatchStr: path.ReAppSettings, - FAIcon: "gear", + Icon: "gear", URL: path.AppSettings, }) if account.IsAdmin || account.IsMod { - newNode := template.NavbarNode{ + newNode := libtemplate.NavbarNode{ Text: l.TextAdmin().String(), MatchStr: path.ReAppAdminPre, - FAIcon: "screwdriver-wrench", + Icon: "screwdriver-wrench", URL: path.AppAdminHome, - Children: template.Navbar{ + Children: libtemplate.Navbar{ { Text: l.TextConfiguration().String(), MatchStr: path.ReAppAdminHome, - FAIcon: "gear", + Icon: "gear", URL: path.AppAdminHome, }, { @@ -166,19 +167,19 @@ func (m *Module) makeNavbar(r *http.Request) (template.Navbar, error) { { Text: l.TextAccounts().String(), MatchStr: path.ReAppAdminAccountPre, - FAIcon: "user", + Icon: "user", URL: path.AppAdminAccount, }, { Text: l.TextBlocks().String(), MatchStr: path.ReAppAdminBlockPre, - FAIcon: "user-slash", + Icon: "user-slash", URL: path.AppAdminBlock, }, { Text: l.TextInstances().String(), MatchStr: path.ReAppAdminInstancesPre, - FAIcon: "server", + Icon: "server", URL: path.AppAdminInstance, }, { @@ -187,13 +188,13 @@ func (m *Module) makeNavbar(r *http.Request) (template.Navbar, error) { { Text: l.TextJobs().String(), MatchStr: path.ReAppAdminJobPre, - FAIcon: "person-running", + Icon: "person-running", URL: path.AppAdminJob, }, { Text: l.TextStatistics().String(), MatchStr: path.ReAppAdminStatisticsPre, - FAIcon: "chart-line", + Icon: "chart-line", URL: path.AppAdminStatistics, }, }, diff --git a/internal/http/webapp/webapp.go b/internal/http/webapp/webapp.go index 13cb487..f18edb2 100644 --- a/internal/http/webapp/webapp.go +++ b/internal/http/webapp/webapp.go @@ -51,8 +51,9 @@ type Module struct { footerScriptChartJS libtemplate.Script } -//revive:disable:argument-limit // New returns a new webapp module. +// +//revive:disable:argument-limit func New( ctx context.Context, d db.DB, diff --git a/test/test-config.yml b/test/test-config.yml index 7750f56..a9aba44 100644 --- a/test/test-config.yml +++ b/test/test-config.yml @@ -88,18 +88,6 @@ db-crypto-key: "test1234test5678test9123test4567" # Default: "" redis-password: "test" -############# -## METRICS ## -############# - -# String. Address and port of the statsd instance -# Default: "localhost:8125" -#statsd-addr: "localhost:8125" - -# String. Prefix to add to statsd metrics -# Default: "democrablock" -#statsd-prefix: "" - ####### # WEB # ####### diff --git a/web/template/login.gohtml b/web/template/login.gohtml deleted file mode 100644 index 6e349a4..0000000 --- a/web/template/login.gohtml +++ /dev/null @@ -1,58 +0,0 @@ -{{ define "login" -}} - - - - - - - {{ .PageTitle }} -{{ range $link := .HeadLinks }} - {{template "head_link" $link -}} -{{- end }} - - - -
-
- -

{{ .Localizer.TextLogin }}

- {{- if .FormError }} - - {{- end }} - - -
-
-{{- range $link := .FooterScripts}} - {{template "script" $link }} -{{- end}} - - -{{ end }} \ No newline at end of file diff --git a/web/template/navbar.gohtml b/web/template/navbar.gohtml index 91dee00..5a7a138 100644 --- a/web/template/navbar.gohtml +++ b/web/template/navbar.gohtml @@ -18,7 +18,7 @@ {{- if .Children }}