Created
January 13, 2019 19:26
-
-
Save cometkim/41f9d6e8dfbabc78b8ecbcac0b926e81 to your computer and use it in GitHub Desktop.
qor admin with echo
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"github.com/jinzhu/gorm" | |
"github.com/labstack/echo" | |
_ "github.com/mattn/go-sqlite3" | |
"github.com/qor/admin" | |
"github.com/qor/auth" | |
"github.com/qor/auth/auth_identity" | |
"github.com/qor/auth_themes/clean" | |
"github.com/qor/qor" | |
"github.com/qor/session/manager" | |
) | |
type User struct { | |
gorm.Model | |
Email string | |
Password string | |
Name string | |
Gender string | |
Role string | |
} | |
type AdminAuth struct{} | |
var ( | |
DB, _ = gorm.Open("sqlite3", "example.db") | |
Auth = clean.New(&auth.Config{ | |
DB: DB, | |
UserModel: &User{}, | |
AuthIdentityModel: &auth_identity.AuthIdentity{}, | |
}) | |
) | |
func (user *User) DisplayName() string { | |
return user.Name | |
} | |
func (AdminAuth) LoginURL(c *admin.Context) string { | |
return "/auth/login" | |
} | |
func (AdminAuth) LogoutURL(c *admin.Context) string { | |
return "/auth/logout" | |
} | |
func (AdminAuth) GetCurrentUser(c *admin.Context) qor.CurrentUser { | |
currentUser := Auth.GetCurrentUser(c.Request) | |
if currentUser != nil { | |
qorCurrentUser, ok := currentUser.(qor.CurrentUser) | |
if !ok { | |
fmt.Printf("User %#v haven't implement qor.CurrentUser interface\n", currentUser) | |
} | |
return qorCurrentUser | |
} | |
return nil | |
} | |
func main() { | |
// Setup DB | |
DB.AutoMigrate(&User{}) | |
DB.LogMode(true) | |
// Setup Admin | |
Admin := admin.New(&admin.AdminConfig{ | |
DB: DB, | |
Auth: &AdminAuth{}, | |
}) | |
Admin.AddResource(&User{}) | |
// Setup Echo | |
e := echo.New() | |
// Middleware | |
authMiddleware := echo.WrapMiddleware(manager.SessionManager.Middleware) | |
e.Use(authMiddleware) | |
// Handler | |
adminHandler := echo.WrapHandler(Admin.NewServeMux("/admin")) | |
e.GET("/admin", adminHandler) | |
e.Any("/admin/*", adminHandler) | |
authHandler := echo.WrapHandler(Auth.NewServeMux()) | |
e.Any("/auth/*", authHandler) | |
// Start server | |
e.Logger.Fatal(e.Start(":1323")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment