Documentation ¶
Overview ¶
Copyright 2023 Bill Nixon
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2023 Bill Nixon ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- Constants
- Variables
- func CompareUserPassword(db *sql.DB, userName, password string) error
- func EmailExists(db *sql.DB, email string) (bool, error)
- func GenerateRandomString(n int) (string, error)
- func GetCookieValue(r *http.Request, name string) (string, error)
- func GetRealRemoteAddr(r *http.Request) string
- func GetReqID(ctx context.Context) string
- func GetUserNameForEmail(db *sql.DB, email string) (string, error)
- func GetUserNameForResetToken(db *sql.DB, tokenValue string) (string, error)
- func InitDB(driverName, dataSourceName string) (*sql.DB, error)
- func InitLog(logFileName string, logLevel slog.Level, addSource bool) error
- func InitTemplates(pattern string) (*template.Template, error)
- func IsEmpty(strs ...string) bool
- func LastLoginForUser(db *sql.DB, userName string) (time.Time, string, error)
- func LogRequestHandler(next http.Handler) http.Handler
- func RegisterUser(db *sql.DB, userName, fullName, email, password string) error
- func RemoveToken(db *sql.DB, tType, tValue string) error
- func RenderTemplate(t *template.Template, w http.ResponseWriter, name string, data interface{}) error
- func RequestIDHandler(next http.Handler) http.Handler
- func RowExists(db *sql.DB, qry string, args ...interface{}) (bool, error)
- func SendEmail(smtpUser, smtpPassword, smtpHost, smtpPort, to, subject, body string) error
- func ServeFileHandler(file string) http.HandlerFunc
- func StringContains(slice []string, value string) bool
- func UserExists(db *sql.DB, userName string) (bool, error)
- func ValidMethod(w http.ResponseWriter, r *http.Request, allowed []string) bool
- func WriteEvent(db *sql.DB, name string, result bool, user, message string)
- type App
- func (app *App) ForgotHandler(w http.ResponseWriter, r *http.Request)
- func (app *App) HelloHandler(w http.ResponseWriter, r *http.Request)
- func (app *App) LoginHandler(w http.ResponseWriter, r *http.Request)
- func (app *App) LoginUser(userName, password string) (Token, error)
- func (app *App) LogoutHandler(w http.ResponseWriter, r *http.Request)
- func (app *App) RegisterHandler(w http.ResponseWriter, r *http.Request)
- func (app *App) ResetHandler(w http.ResponseWriter, r *http.Request)
- func (app *App) UsersHandler(w http.ResponseWriter, r *http.Request)
- type Config
- type ConfigSMTP
- type ConfigSQL
- type ConfigServer
- type Event
- type ForgotPageData
- type HelloPageData
- type LoginPageData
- type LogoutPageData
- type MailMessage
- type RedactedConfig
- type RegisterPageData
- type ResetPageData
- type Token
- type User
- type UsersPageData
Constants ¶
const ( EventLogin = "login" EventLogout = "logout" EventRegister = "register" EventSaveToken = "save_token" EventReset = "reset_pass" EventMax = "1234567890" )
const ( MsgMissingEmail = "Please provide an Email." MsgNoSuchUser = "There is no user for the Email provided." MsgMissingAction = "Action is missing." MsgInvalidAction = "Action is invalid." )
const ( MsgMissingUserNameAndPassword = "Missing username and password" MsgMissingUserName = "Missing username" MsgMissingPassword = "Missing password" MsgLoginFailed = "Login Failed" )
const ( MsgMissingRequired = "Please provide all the required values" MsgUserNameExists = "User Name already exists." MsgEmailExists = "Email Address already registered." MsgPasswordsDifferent = "Password values do not match." )
const MsgTemplateError = "Sorry, the server was unable to display this page. Please contact the administrator."
const ReqIDKey ctxReqIDKey = 0
ReqIDKey is the key for the unique request ID in a request context.
const SessionTokenCookieName = "session"
Variables ¶
var ( ErrAppGetConfig = errors.New("failed") ErrAppInvalidConfig = errors.New("invalid config") ErrAppInitDB = errors.New("failed") ErrAppInitTemplates = errors.New("failed") )
var ( ErrConfigOpen = errors.New("failed") ErrConfigDecode = errors.New("failed to decode") )
var ( ErrDBOpen = errors.New("failed to open db") ErrDBPing = errors.New("failed to ping db") )
var ( ErrUserSessionNotFound = errors.New("user session not found") ErrUserNotFound = errors.New("user not found") ErrUserSessionExpired = errors.New("user session expired") ErrUserGetLastLoginFailed = errors.New("user failed to get last login") )
Define command error values.
var ErrInvalidLength = errors.New("invalid length")
var ErrRequestNil = errors.New("request is nil")
Functions ¶
func CompareUserPassword ¶
CompareUserPassword compares the password and hashed password for the user. Returns nil on success or an error on failure.
func EmailExists ¶
EmailExists returns true if the given email already exists.
func GenerateRandomString ¶
GenerateRandomString returns a URL safe base64 encoded string of n random bytes.
func GetCookieValue ¶
GetCookieValue returns the Value for the named cookie or an empty string if not found or an error occurs.
func GetRealRemoteAddr ¶
GetRealRemoteAddr returns X-Real-IP header or r.RemoteAddr.
func GetUserNameForEmail ¶
GetUserNameForEmail returns the userName for a given email.
func GetUserNameForResetToken ¶
GetUserNameForResetToken returns the userName for a given reset token.
func InitTemplates ¶
InitTemplates parses the templates.
func LastLoginForUser ¶
LastLoginForUser retrieves the last login time and result for a given userName. It returns zero values in case of no previous login.
func LogRequestHandler ¶
LogRequestHandler is middleware that logs all HTTP requests.
func RegisterUser ¶
RegisterUser registers a user with the given values. Returns nil on success or an error on failure.
func RemoveToken ¶
RemoveToken removes the given sessionToken.
func RenderTemplate ¶
func RenderTemplate(t *template.Template, w http.ResponseWriter, name string, data interface{}) error
RenderTemplate executes the named template with given data to the writer. If an error occurs, writer is updated to indicate a Internal Server Error. The caller must ensure no further writes are done for a non-nil error.
func RequestIDHandler ¶
RequestIDHandler is middleware that adds a unique request ID to the request context.
func RowExists ¶
RowExists return true if the given query returns at least one row. qry should be of the form "SELECT 1 ..."
func ServeFileHandler ¶
func ServeFileHandler(file string) http.HandlerFunc
ServeFileHandler returns a HandlerFunc to serve the specified file.
func StringContains ¶
StringContains reports if slice contain value.
func UserExists ¶
UserExists returns true if the given userName already exists in db.
func ValidMethod ¶
ValidMethod checks if the given HTTP request method is allowed based on the provided list of allowed methods. It returns true if the method is allowed, and false otherwise. If the method is not allowed or is OPTIONS, the function updates the response writer appropriately and returns false. The calling handler should return without further processing.
Types ¶
type App ¶
App contains common variables to avoid using global variables.
func (*App) ForgotHandler ¶
func (app *App) ForgotHandler(w http.ResponseWriter, r *http.Request)
ForgotHandler handles /forgot requests.
func (*App) HelloHandler ¶
func (app *App) HelloHandler(w http.ResponseWriter, r *http.Request)
HelloHandler prints a simple hello and any user information.
func (*App) LoginHandler ¶
func (app *App) LoginHandler(w http.ResponseWriter, r *http.Request)
LoginHandler handles /login requests.
func (*App) LogoutHandler ¶
func (app *App) LogoutHandler(w http.ResponseWriter, r *http.Request)
LogoutHandler handles /logout requests.
func (*App) RegisterHandler ¶
func (app *App) RegisterHandler(w http.ResponseWriter, r *http.Request)
RegisterHandler handles /register requests.
func (*App) ResetHandler ¶
func (app *App) ResetHandler(w http.ResponseWriter, r *http.Request)
ResetHandler handles /reset requests.
func (*App) UsersHandler ¶
func (app *App) UsersHandler(w http.ResponseWriter, r *http.Request)
UsersHandler prints a simple hello message.
type Config ¶
type Config struct { Title string // title of the application BaseURL string // base URL, e.g., https://host:port ParseGlobPattern string // pattern to use with template.ParseGlob SessionExpiresHours int // number of hours user session is valid Server ConfigServer SQL ConfigSQL SMTP ConfigSMTP }
Config represents the configuration values.
func GetConfigFromFile ¶
GetConfigFromFile returns the Config from filename.
func (Config) MarshalJSON ¶
MarshalJSON is a custom Marshaler to redact some fields.
type ConfigSMTP ¶
ConfigSMTP contains SMTP related configuration values.
type ConfigServer ¶
ConfigServer contains Server related configuration values.
type ForgotPageData ¶
ForgotPageData contains data passed to the HTML template.
type HelloPageData ¶
HelloPageData contains data passed to the HTML template.
type LoginPageData ¶
LoginPageData contains data passed to the HTML template.
type LogoutPageData ¶
LogoutPageData contains data passed to the HTML template.
type MailMessage ¶
MailMessage contains data to include in the email template.
type RedactedConfig ¶
type RedactedConfig Config
RedactedConfig is a copy of Config used to redact values on output.
type RegisterPageData ¶
RegisterPageData contains data passed to the HTML template.
type ResetPageData ¶
ResetPageData contains data passed to the HTML template.
type User ¶
type User struct { UserName string FullName string Email string IsAdmin bool Created time.Time LastLoginTime time.Time LastLoginResult string }
User represents a user stored in the database.
func GetUserForName ¶
GetUserForName returns a user for the given userName.
func GetUserForSessionToken ¶
GetUserForSessionToken returns a user for the given sessionToken.
func GetUserFromRequest ¶
GetUserFromRequest returns the current User or empty User if the session is not found.