feat: add application

Signed-off-by: Zixuan Liu <nodeces@gmail.com>
This commit is contained in:
Zixuan Liu
2020-11-23 23:48:29 +08:00
parent b003838b00
commit 218b96ed74
11 changed files with 146 additions and 8 deletions

View File

@ -40,10 +40,11 @@ func main() {
}
userStore := store.NewUserStore(db)
applicationStore := store.NewApplicationStore(db)
srv := &http.Server{
Addr: cfg.HTTPPort,
Handler: handler.New(userStore),
Handler: handler.New(userStore, applicationStore),
}
go func() {

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/go-playground/validator/v10 v10.4.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/uuid v1.1.2
github.com/joho/godotenv v1.3.0
github.com/json-iterator/go v1.1.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

2
go.sum
View File

@ -76,6 +76,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=

View File

@ -3,6 +3,8 @@ package handler
import (
"net/http"
"github.com/casdoor/casdoor/internal/handler/application"
"github.com/casdoor/casdoor/internal/handler/user"
"github.com/casdoor/casdoor/internal/store"
@ -20,7 +22,7 @@ var corsConfig = cors.Config{
MaxAge: 300,
}
func New(userStore *store.UserStore) http.Handler {
func New(userStore *store.UserStore, applicationStore *store.ApplicationStore) http.Handler {
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
@ -36,5 +38,9 @@ func New(userStore *store.UserStore) http.Handler {
apiGroup.POST("/add-user", userHandler.AddUser)
apiGroup.POST("/delete-user", userHandler.DeleteUser)
applicationHandler := application.New(applicationStore)
apiGroup.GET("/applications", applicationHandler.List)
apiGroup.POST("/applications", applicationHandler.Create)
return r
}

View File

@ -0,0 +1,62 @@
// Copyright 2020 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package application
import (
"net/http"
"strconv"
"time"
"github.com/casdoor/casdoor/internal/object"
"github.com/casdoor/casdoor/internal/store"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type Handler struct {
applicationStore *store.ApplicationStore
}
func New(applicationStore *store.ApplicationStore) *Handler {
return &Handler{applicationStore: applicationStore}
}
func (h *Handler) Create(g *gin.Context) {
app := &object.Application{}
err := g.BindJSON(app)
if err != nil {
_ = g.Error(err)
return
}
app.Id = uuid.New().String()
app.CreatedTime = time.Now().Format(time.RFC3339)
app.CreatedBy = "TODO"
err = h.applicationStore.Create(app)
if err != nil {
_ = g.Error(err)
return
}
}
func (h *Handler) List(g *gin.Context) {
limit, _ := strconv.Atoi(g.DefaultQuery("limit", "20"))
offset, _ := strconv.Atoi(g.DefaultQuery("offset", "0"))
apps, err := h.applicationStore.List(limit, offset)
if err != nil {
_ = g.Error(err)
return
}
g.JSON(http.StatusOK, apps)
}

View File

@ -1 +0,0 @@
package handler

View File

@ -18,7 +18,13 @@ func New(userStore *store.UserStore) *Handler {
func (h *Handler) GetUsers(g *gin.Context) {
owner := g.GetString("owner")
g.JSON(http.StatusOK, h.userStore.GetUsers(owner))
user, err := h.userStore.GetUsers(owner)
if err != nil {
_ = g.Error(err)
return
}
g.JSON(http.StatusOK, user)
}
func (h *Handler) GetUser(g *gin.Context) {

View File

@ -0,0 +1,9 @@
package object
type Application struct {
Id string `xorm:"notnull pk" json:"id"`
Name string `xorm:"notnull pk" json:"name"`
CreatedTime string `json:"createdTime"`
CreatedBy string `xorm:"notnull" json:"createdBy"`
}

View File

@ -0,0 +1,41 @@
// Copyright 2020 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"github.com/casdoor/casdoor/internal/object"
"github.com/casdoor/casdoor/internal/store/shared"
)
type ApplicationStore struct {
db *shared.DB
}
func NewApplicationStore(db *shared.DB) *ApplicationStore {
return &ApplicationStore{
db: db,
}
}
func (a *ApplicationStore) Create(app *object.Application) error {
_, err := a.db.GetEngine().Insert(app)
return err
}
func (a *ApplicationStore) List(limit, offset int) ([]*object.Application, error) {
var apps []*object.Application
err := a.db.GetEngine().Desc("created_time").Limit(limit, offset).Find(&apps)
return apps, err
}

View File

@ -38,7 +38,18 @@ func NewDB(cfg *config.Config) (*DB, error) {
if err != nil {
return nil, fmt.Errorf("xorm.NewEngine: %v", err)
}
err = engine.Ping()
if err != nil {
return nil, fmt.Errorf("engine.Ping(): %v", err)
}
db.engine = engine
err = db.createTable()
if err != nil {
return nil, fmt.Errorf("db.createTable(): %v", err)
}
return db, nil
}
@ -47,6 +58,6 @@ func (db *DB) GetEngine() *xorm.Engine {
}
func (db *DB) createTable() error {
err := db.engine.Sync2(new(object.User))
err := db.engine.Sync2(new(object.User), new(object.Application))
return err
}

View File

@ -31,14 +31,14 @@ func NewUserStore(db *shared.DB) *UserStore {
}
}
func (u *UserStore) GetUsers(owner string) []*object.User {
func (u *UserStore) GetUsers(owner string) ([]*object.User, error) {
var users []*object.User
err := u.db.GetEngine().Desc("created_time").Find(&users, &object.User{Owner: owner})
if err != nil {
panic(err)
return nil, err
}
return users
return users, nil
}
func (u *UserStore) getUser(owner string, name string) (*object.User, error) {