2023-01-17 09:40:22 +00:00
|
|
|
package pkg
|
2023-01-13 19:43:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-02-03 11:30:56 +00:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2023-01-13 19:43:42 +00:00
|
|
|
)
|
|
|
|
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
func NewRouter(db *Db) http.Handler {
|
2023-02-03 11:30:56 +00:00
|
|
|
companyRouter := httprouter.New()
|
|
|
|
companyRouter.GET("/profile", GetProfileForm)
|
|
|
|
companyRouter.POST("/profile", HandleProfileForm)
|
|
|
|
companyRouter.GET("/tax-details", GetCompanyTaxDetailsForm)
|
|
|
|
companyRouter.POST("/tax-details", HandleCompanyTaxDetailsForm)
|
|
|
|
companyRouter.POST("/tax", HandleAddCompanyTax)
|
|
|
|
companyRouter.DELETE("/tax/:taxId", HandleDeleteCompanyTax)
|
|
|
|
companyRouter.GET("/contacts", IndexContacts)
|
|
|
|
companyRouter.POST("/contacts", HandleAddContact)
|
2023-02-03 12:29:10 +00:00
|
|
|
companyRouter.GET("/contacts/:slug", GetContactForm)
|
|
|
|
companyRouter.PUT("/contacts/:slug", HandleUpdateContact)
|
2023-02-03 11:30:56 +00:00
|
|
|
companyRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2023-01-30 15:48:21 +00:00
|
|
|
mustRenderAppTemplate(w, r, "dashboard.gohtml", nil)
|
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
|
|
|
})
|
|
|
|
|
2023-02-03 11:30:56 +00:00
|
|
|
router := httprouter.New()
|
|
|
|
router.ServeFiles("/static/*filepath", http.Dir("web/static"))
|
|
|
|
router.GET("/login", GetLoginForm)
|
|
|
|
router.POST("/login", HandleLoginForm)
|
|
|
|
router.POST("/logout", Authenticated(HandleLogout))
|
|
|
|
|
|
|
|
companyHandler := Authenticated(CompanyHandler(companyRouter))
|
|
|
|
router.GET("/company/:slug/*rest", companyHandler)
|
|
|
|
router.POST("/company/:slug/*rest", companyHandler)
|
|
|
|
router.PUT("/company/:slug/*rest", companyHandler)
|
|
|
|
router.DELETE("/company/:slug/*rest", companyHandler)
|
|
|
|
|
|
|
|
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
user := getUser(r)
|
|
|
|
if user.LoggedIn {
|
Add the company relation and read-only form to edit
I do not have more time to update the update to the company today, but i
believe this is already a good amount of work for a commit.
The company is going to be used for row level security, as users will
only have access to the data from companies they are granted access, by
virtue of being in the company_user relation.
I did not know how add a row level security policy to the company_user
because i needed the to select on the same relation and this is not
allowed, because it would create an infinite loop.
Had to add the vat, pg_libphonenumber, and uri extensions in order to
validate VAT identification numbers, phone numbers, and URIs,
repectively. These libraries are not in Debian, but i created packages
for them all in https://dev.tandem.ws/tandem.
2023-01-24 20:46:07 +00:00
|
|
|
conn := getConn(r)
|
2023-02-03 09:51:48 +00:00
|
|
|
slug := conn.MustGetText(r.Context(), "", "select slug::text from company order by company_id limit 1")
|
2023-02-03 11:30:56 +00:00
|
|
|
http.Redirect(w, r, "/company/"+slug+"/", http.StatusFound)
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
} else {
|
Add Catalan and Spanish translation with gotext[3]
I had to choose between [1], [2], and [3].
As far as i could find, [1] is not easy to work with templates[4] and at
the moment is not maintained[5].
Both [2] and [3] use the same approach to be used from within templates:
you have to define a FuncMap with template functions that call the
message catalog. Also, both libraries seems to be reasonably
maintained, and have packages in Debian’s repository.
However, [2] repeats the same mistakes that POSIX did with its
catalogs—using identifiers that are not the strings in the source
language—, however this time the catalogs are written in JSON or YAML!
This, somehow, makes things worse….
[3], the one i settled with, is fine and decently maintained. There are
some surprising things, such as to be able to use directly the PO file,
and that it has higher priority over the corresponding MO, or that the
order of parameters is reversed in respect to gettext. However, it uses
a saner format, and is a lot easier to work with than [3].
The problem, of course, is that xgettext does not know how to find
translatable strings inside the template. [3] includes a CLI tool
similar to xgettext, but is not a drop-in replacement[6] and does not
process templates.
The proper way to handle this would be to add a parser to xgettext, but
for now i found out that if i surround the call to the translation
functions from within the template with parentheses, i can trick
xgettext into believing it is parsing Scheme code, and extracts the
strings successfully—at least, for what i have tried. Had to add the
keyword for pgettext, because Schemed does not have it, but at least i
can do that with command line parameters.
For now i left only Spanish and Catalan as the two available languages,
even though the source text is written in English, because that way i
can make sure i do not leave strings untranslated.
[1]: https://golang.org/x/text
[2]: https://github.com/nicksnyder/go-i18n
[3]: https://github.com/leonelquinteros/gotext
[4]: https://github.com/golang/go/issues/39954
[5]: https://github.com/golang/go/issues/12750
[6]: https://github.com/leonelquinteros/gotext/issues/38
2023-01-18 18:07:42 +00:00
|
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
2023-01-13 19:43:42 +00:00
|
|
|
}
|
|
|
|
})
|
2023-02-03 11:30:56 +00:00
|
|
|
|
2023-01-13 19:43:42 +00:00
|
|
|
var handler http.Handler = router
|
2023-02-03 11:30:56 +00:00
|
|
|
handler = MethodOverrider(handler)
|
|
|
|
handler = LocaleSetter(db, handler)
|
|
|
|
handler = LoginChecker(db, handler)
|
Implement login cookie, its verification, and logout
At first i thought that i would need to implement sessions, the ones
that keep small files onto the disk, to know which user is talking to
the server, but then i realized that, for now at least, i only need a
very large number, plus the email address, to be used as a lookup, and
that can be stored in the user table, in a separate schema.
Had to change login to avoid raising exceptions when login failed
because i now keep a record of login attemps, and functions are always
run in a single transaction, thus the exception would prevent me to
insert into login_attempt. Even if i use a separate procedure, i could
not keep the records.
I did not want to add a parameter to the logout function because i was
afraid that it could be called from separate users. I do not know
whether it is possible with the current approach, since the settings
variable is also set by the same applications; time will tell.
2023-01-17 19:48:50 +00:00
|
|
|
handler = Recoverer(handler)
|
2023-01-17 09:40:22 +00:00
|
|
|
handler = Logger(handler)
|
2023-01-13 19:43:42 +00:00
|
|
|
return handler
|
|
|
|
}
|
2023-02-03 11:30:56 +00:00
|
|
|
|
|
|
|
func MethodOverrider(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-02-03 12:29:10 +00:00
|
|
|
override := r.FormValue(overrideMethodName)
|
2023-02-03 11:30:56 +00:00
|
|
|
if override == http.MethodDelete || override == http.MethodPut {
|
|
|
|
r2 := new(http.Request)
|
|
|
|
*r2 = *r
|
|
|
|
r2.Method = override
|
|
|
|
r = r2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|