-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pretty alerts + user auth + jwt exp check middleware
- Loading branch information
Showing
12 changed files
with
157 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,61 @@ | ||
package alerts | ||
|
||
import "github.com/gin-contrib/sessions" | ||
import ( | ||
"encoding/json" | ||
|
||
const flashesGroup = "alerts" | ||
"github.com/gin-contrib/sessions" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const alertsFlashesGroup = "alerts" | ||
|
||
type AlertType int | ||
type AlertType string | ||
|
||
const ( | ||
AlertSuccess AlertType = iota | ||
AlertWarning | ||
AlertDanger | ||
AlertInfo | ||
TypeSuccess AlertType = "success" | ||
TypeWarning AlertType = "warning" | ||
TypeDanger AlertType = "danger" | ||
TypeInfo AlertType = "info" | ||
) | ||
|
||
type Alert struct { | ||
Message string | ||
Type AlertType | ||
Message string `json:"message"` | ||
Type AlertType `json:"type"` | ||
} | ||
|
||
func Add(session sessions.Session, a Alert) { | ||
session.AddFlash(a, flashesGroup) | ||
_ = session.Save() | ||
func Add(session sessions.Session, a Alert) error { | ||
data, err := json.Marshal(a) | ||
if err != nil { | ||
return err | ||
} | ||
session.AddFlash(string(data), alertsFlashesGroup) | ||
|
||
return session.Save() | ||
} | ||
|
||
func Get(session sessions.Session) []Alert { | ||
flashes := session.Flashes(flashesGroup) | ||
res := make([]Alert, 0, len(flashes)) | ||
flashes := session.Flashes(alertsFlashesGroup) | ||
if len(flashes) > 0 { | ||
_ = session.Save() | ||
} | ||
|
||
result := make([]Alert, 0, len(flashes)) | ||
for _, f := range flashes { | ||
if flash, ok := f.(Alert); ok { | ||
res = append(res, flash) | ||
s, ok := f.(string) | ||
if !ok { | ||
logrus.Errorf("bad flash: %v", f) | ||
|
||
continue | ||
} | ||
|
||
var a Alert | ||
if err := json.Unmarshal([]byte(s), &a); err != nil { | ||
logrus.WithError(err).Errorf("failed to unmarshal flash: %s", s) | ||
|
||
continue | ||
} | ||
result = append(result, a) | ||
} | ||
_ = session.Save() | ||
|
||
return res | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.