refactor:
This commit is contained in:
parent
ad8d145986
commit
3c0f01630f
29 changed files with 360 additions and 1377 deletions
|
@ -6,15 +6,10 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
Env string `env:"ENV" env-default:"prod"`
|
||||
|
||||
Env string `env:"ENV" env-default:"prod"`
|
||||
Pandoc string `env:"PANDOC" required:"true"`
|
||||
PostgresDSN string `env:"POSTGRES_DSN" required:"true"`
|
||||
RedisDSN string `env:"REDIS_DSN" required:"true"`
|
||||
|
||||
Email string `env:"EMAIL" required:"true"`
|
||||
Password string `env:"PASSWORD" required:"true"`
|
||||
|
||||
JWTSecret string `env:"JWT_SECRET" required:"true"`
|
||||
JWTSecret string `env:"JWT_SECRET" required:"true"`
|
||||
}
|
||||
|
||||
func MustSetupConfig() *Config {
|
||||
|
|
|
@ -11,12 +11,5 @@ var (
|
|||
)
|
||||
|
||||
var (
|
||||
ErrBadHandleOrPassword = errors.New("bad handle or password")
|
||||
ErrBadRole = errors.New("bad role")
|
||||
ErrTooShortPassword = errors.New("too short password")
|
||||
ErrTooLongPassword = errors.New("too long password")
|
||||
ErrBadEmail = errors.New("bad email")
|
||||
ErrBadUsername = errors.New("bad username")
|
||||
ErrTooShortUsername = errors.New("too short username")
|
||||
ErrTooLongUsername = errors.New("too long username")
|
||||
ErrBadRole = errors.New("bad role")
|
||||
)
|
||||
|
|
|
@ -4,29 +4,6 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
RoleSpectator int32 = 0
|
||||
RoleParticipant int32 = 1
|
||||
RoleModerator int32 = 2
|
||||
RoleAdmin int32 = 3
|
||||
)
|
||||
|
||||
func IsAdmin(role int32) bool {
|
||||
return role == RoleAdmin
|
||||
}
|
||||
|
||||
func IsModerator(role int32) bool {
|
||||
return role == RoleModerator
|
||||
}
|
||||
|
||||
func IsParticipant(role int32) bool {
|
||||
return role == RoleParticipant
|
||||
}
|
||||
|
||||
func IsSpectator(role int32) bool {
|
||||
return role == RoleSpectator
|
||||
}
|
||||
|
||||
func AsTimeP(t time.Time) *time.Time {
|
||||
return &t
|
||||
}
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
func SendMail(cfg Config, to []string, subject, body string) error {
|
||||
auth := smtp.PlainAuth("", cfg.Email, cfg.Password, "smtp.gmail.com")
|
||||
|
||||
msg := fmt.Sprintf("From: %s\nTo: %s\nSubject: %s\n%s", cfg.Email, "", subject, body)
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, cfg.Email, to, []byte(msg))
|
||||
if err != nil {
|
||||
return err // FIXME
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"net/mail"
|
||||
)
|
||||
|
||||
func ValidPassword(str string) error {
|
||||
if len(str) < 5 {
|
||||
return ErrTooShortPassword
|
||||
}
|
||||
if len(str) > 70 {
|
||||
return ErrTooLongPassword
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidUsername(str string) error {
|
||||
if len(str) < 5 {
|
||||
return ErrTooShortUsername
|
||||
}
|
||||
if len(str) > 70 {
|
||||
return ErrTooLongUsername
|
||||
}
|
||||
if err := ValidEmail(str); err == nil {
|
||||
return ErrBadUsername
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidEmail(str string) error {
|
||||
emailAddress, err := mail.ParseAddress(str)
|
||||
if err != nil || emailAddress.Address != str {
|
||||
return ErrBadEmail
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidRole(role int32) error {
|
||||
switch role {
|
||||
case RoleSpectator, RoleParticipant, RoleModerator, RoleAdmin:
|
||||
return nil
|
||||
}
|
||||
return ErrBadRole
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue