Initial commit
This commit is contained in:
commit
2fa110e760
28 changed files with 2346 additions and 0 deletions
27
internal/lib/config.go
Normal file
27
internal/lib/config.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Env string `env:"ENV" env-default:"prod"`
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
func MustSetupConfig() *Config {
|
||||
var cfg Config
|
||||
err := cleanenv.ReadConfig(".env", &cfg)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error reading config: %s", err.Error()))
|
||||
}
|
||||
return &cfg
|
||||
}
|
22
internal/lib/errors.go
Normal file
22
internal/lib/errors.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInternal = errors.New("internal")
|
||||
ErrUnexpected = errors.New("unexpected")
|
||||
ErrNoPermission = errors.New("no permission")
|
||||
)
|
||||
|
||||
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")
|
||||
)
|
40
internal/lib/lib.go
Normal file
40
internal/lib/lib.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package lib
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func AsInt32P(v int32) *int32 {
|
||||
return &v
|
||||
}
|
||||
|
||||
func AsStringP(str string) *string {
|
||||
return &str
|
||||
}
|
19
internal/lib/mail.go
Normal file
19
internal/lib/mail.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
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
|
||||
}
|
44
internal/lib/validation.go
Normal file
44
internal/lib/validation.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
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