Skip to content

Commit

Permalink
Adjust boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
sujit-baniya committed Jan 30, 2021
1 parent c48eb04 commit f6b3572
Show file tree
Hide file tree
Showing 13 changed files with 653 additions and 91 deletions.
5 changes: 4 additions & 1 deletion config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func (d *AuthConfig) Setup(db *gorm.DB, file string) {
panic(err)
}
enforcer.SetAdapter(adapter)
_ = enforcer.LoadPolicy()
err = enforcer.LoadPolicy()
if err != nil {
panic(err)
}
d.Enforcer = enforcer
authConf := CasbinAuthConfig{
Enforcer: d.Enforcer,
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/alexedwards/argon2id v0.0.0-20201228115903-cf543ebc1f7b
github.com/casbin/casbin/v2 v2.22.0
github.com/casbin/gorm-adapter/v3 v3.0.6
github.com/casbin/gorm-adapter/v3 v3.1.0
github.com/form3tech-oss/jwt-go v3.2.2+incompatible
github.com/gofiber/fiber/v2 v2.3.3
github.com/gofiber/session/v2 v2.0.2
Expand All @@ -16,6 +16,7 @@ require (
github.com/gosimple/slug v1.9.0
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/joho/godotenv v1.3.0
github.com/klauspost/pgzip v1.2.5
github.com/markbates/pkger v0.17.1
github.com/mattn/go-colorable v0.1.8
github.com/mattn/go-isatty v0.0.12
Expand All @@ -25,11 +26,11 @@ require (
github.com/sujit-baniya/ip v0.0.1
github.com/sujit-baniya/log v1.0.56
github.com/sujit-baniya/sblogger v0.0.9
github.com/sujit-baniya/utils v0.0.4
github.com/sujit-baniya/xid v1.2.2
github.com/valyala/bytebufferpool v1.0.0
github.com/xhit/go-simple-mail/v2 v2.7.0
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
gorm.io/driver/mysql v1.0.4
gorm.io/driver/postgres v1.0.7
gorm.io/gorm v1.20.12
Expand Down
79 changes: 10 additions & 69 deletions go.sum

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions pkg/models/casbin_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
)

type RoleAndPermission struct {
ID uint `gorm:"primarykey"`
ID uint `gorm:"primaryKey;autoIncrement"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
PType string `gorm:"p_type" json:"p_type"`
V0 string `gorm:"v0" json:"v0"`
V1 string `gorm:"v1" json:"v1"`
V2 string `gorm:"v2" json:"v2"`
V3 string `gorm:"v3" json:"v3"`
V4 string `gorm:"v4" json:"v4"`
V5 string `gorm:"v5" json:"v5"`
Ptype string `gorm:"size:100;uniqueIndex:unique_index"`
V0 string `gorm:"size:100;uniqueIndex:unique_index"`
V1 string `gorm:"size:100;uniqueIndex:unique_index"`
V2 string `gorm:"size:100;uniqueIndex:unique_index"`
V3 string `gorm:"size:100;uniqueIndex:unique_index"`
V4 string `gorm:"size:100;uniqueIndex:unique_index"`
V5 string `gorm:"size:100;uniqueIndex:unique_index"`
Category string `gorm:"category" json:"category"`
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/services/mail_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/sujit-baniya/fiber-boilerplate/app"
"github.com/sujit-baniya/utils"
"github.com/sujit-baniya/fiber-boilerplate/utils"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion rest/controllers/file_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/sujit-baniya/fiber-boilerplate/app"
"github.com/sujit-baniya/fiber-boilerplate/pkg/auth"
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
"github.com/sujit-baniya/utils/xopen"
"github.com/sujit-baniya/fiber-boilerplate/utils/xopen"
"github.com/sujit-baniya/xid"
)

Expand Down
14 changes: 7 additions & 7 deletions rest/controllers/role_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func AssignRoleToUser(c *fiber.Ctx) error {
var role1 models.RoleAndPermission
c.BodyParser(&roleRequest)
role := models.RoleAndPermission{
PType: "g",
Ptype: "g",
V0: fmt.Sprintf("%d", roleRequest.UserID),
V1: roleRequest.Role,
}
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{PType: role.PType,V0: role.V0, V1: role.V1}).Error
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{Ptype: role.Ptype,V0: role.V0, V1: role.V1}).Error
if err != nil {
app.Http.Database.Create(&role)
app.Http.Auth.Enforcer.LoadPolicy()
Expand All @@ -64,7 +64,7 @@ func ChangeRoleForUser(c *fiber.Ctx) error {
var role models.RoleRequest
var role1 models.RoleAndPermission
c.BodyParser(&role)
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{PType: "g",V0: fmt.Sprintf("%d", role.UserID), V1: role.OldRole}).Error
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{Ptype: "g",V0: fmt.Sprintf("%d", role.UserID), V1: role.OldRole}).Error
if err == nil {
role1.V1 = role.Role
role1.DeletedAt = gorm.DeletedAt{Valid: false}
Expand All @@ -81,13 +81,13 @@ func AddPermissionOnRole(c *fiber.Ctx) error {
c.BodyParser(&permission)
if permission.Role != "" && permission.Module != "" && permission.Action != "" {
role := models.RoleAndPermission{
PType: "p",
Ptype: "p",
V0: permission.Role,
V1: permission.Module,
V2: permission.Action,
Category: "permission",
}
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{PType: role.PType,V0: role.V0, V1: role.V1, V2: role.V2}).Error
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{Ptype: role.Ptype,V0: role.V0, V1: role.V1, V2: role.V2}).Error
if err != nil {
app.Http.Database.Create(&role)
}
Expand All @@ -97,13 +97,13 @@ func AddPermissionOnRole(c *fiber.Ctx) error {
if permission.Role != "" && permission.Route != "" && permission.Method != "" {
fmt.Println(1)
role := models.RoleAndPermission{
PType: "p",
Ptype: "p",
V0: permission.Role,
V1: permission.Route,
V2: permission.Method,
Category: "route",
}
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{PType: role.PType,V0: role.V0, V1: role.V1, V2: role.V2}).Error
err := app.Http.Database.Unscoped().First(&role1, models.RoleAndPermission{Ptype: role.Ptype,V0: role.V0, V1: role.V1, V2: role.V2}).Error
if err != nil {
app.Http.Database.Create(&role)
}
Expand Down
2 changes: 1 addition & 1 deletion rest/middlewares/password_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package middlewares

import (
"errors"
"github.com/sujit-baniya/utils"
"github.com/sujit-baniya/fiber-boilerplate/utils"
"strconv"
"strings"
"time"
Expand Down
2 changes: 1 addition & 1 deletion rest/middlewares/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package middlewares

import (
"fmt"
"github.com/sujit-baniya/utils"
"github.com/sujit-baniya/fiber-boilerplate/utils"

"github.com/gofiber/fiber/v2"
"github.com/gookit/validate"
Expand Down
73 changes: 73 additions & 0 deletions utils/encrypter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package utils

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
)

func Encrypt(stringToEncrypt string, keyString string) (encryptedString string) {

//Since the key is in string, we need to convert decode it to bytes
key, _ := hex.DecodeString(keyString)
plaintext := []byte(stringToEncrypt)

//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}

//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode
//https://golang.org/pkg/crypto/cipher/#NewGCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}

//Create a nonce. Nonce should be from GCM
nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}

//Encrypt the data using aesGCM.Seal
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix.
ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil)
return fmt.Sprintf("%x", ciphertext)
}

func Decrypt(encryptedString string, keyString string) (decryptedString string) {

key, _ := hex.DecodeString(keyString)
enc, _ := hex.DecodeString(encryptedString)

//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}

//Create a new GCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}

//Get the nonce size
nonceSize := aesGCM.NonceSize()

//Extract the nonce from the encrypted data
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]

//Decrypt the data
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
panic(err.Error())
}

return fmt.Sprintf("%s", plaintext)
}
80 changes: 80 additions & 0 deletions utils/xopen/fastgzreader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package xopen

import (
"compress/gzip"
"io"
"os/exec"
)

// from: https://gist.github.com/rasky/d42a52c16683f1a2f4dccdef80e2712d

// fastGzReader is an API-compatible drop-in replacement
// for gzip.Reader, that achieves a higher decoding speed
// by spawning an external gzip instance and pipeing data
// through it.
// Go's native gzip implementation is about 2x slower at
// decompressing data compared to zlib (mostly due to Go compiler
// inefficiencies). So for tasks where the gzip decoding
// speed is important, this is a quick workaround that doesn't
// require cgo.
// gzip is part of the gzip package and comes preinstalled on
// most Linux distributions and on OSX.
type fastGzReader struct {
io.ReadCloser
}

func hasProg(prog ...string) bool {
var cmd *exec.Cmd
if len(prog) > 1 {
cmd = exec.Command(prog[0], prog[1:]...)
} else {
cmd = exec.Command(prog[0])
}
err := cmd.Start()
has := err == nil
cmd.Wait()
return has
}

var hasZlib = hasProg("gzip", "-d")
var hasPigz = hasProg("pigz", "-d")

func newFastGzReader(r io.Reader) (io.ReadCloser, error) {

if hasZlib || hasPigz {
var gz fastGzReader
if err := gz.Reset(r); err != nil {
return nil, err
}
return gz, nil
}
return gzip.NewReader(r)

}

func (gz *fastGzReader) Reset(r io.Reader) error {
if gz.ReadCloser != nil {
gz.Close()
}
var cmd *exec.Cmd
if hasPigz {
cmd = exec.Command("pigz", "-d")
} else {
cmd = exec.Command("gzip", "-d")
}
cmd.Stdin = r

rpipe, err := cmd.StdoutPipe()
if err != nil {
return err
}

err = cmd.Start()
if err != nil {
rpipe.Close()
return err
}

gz.ReadCloser = rpipe
return nil
}

0 comments on commit f6b3572

Please sign in to comment.