diff --git a/cmd/main.go b/cmd/main.go index a323c414..d1562717 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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() { diff --git a/go.mod b/go.mod index 078176c1..e5a8d292 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3cbcbe89..c55e458f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/handler/api.go b/internal/handler/api.go index 7cd22e1c..edf49431 100644 --- a/internal/handler/api.go +++ b/internal/handler/api.go @@ -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 } diff --git a/internal/handler/application/application.go b/internal/handler/application/application.go new file mode 100644 index 00000000..002a8ac6 --- /dev/null +++ b/internal/handler/application/application.go @@ -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) +} diff --git a/internal/handler/user.go b/internal/handler/user.go deleted file mode 100644 index abeebd16..00000000 --- a/internal/handler/user.go +++ /dev/null @@ -1 +0,0 @@ -package handler diff --git a/internal/handler/user/user.go b/internal/handler/user/user.go index 2ab36e81..592978ba 100644 --- a/internal/handler/user/user.go +++ b/internal/handler/user/user.go @@ -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) { diff --git a/internal/object/application.go b/internal/object/application.go new file mode 100644 index 00000000..31aca204 --- /dev/null +++ b/internal/object/application.go @@ -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"` +} diff --git a/internal/store/application.go b/internal/store/application.go new file mode 100644 index 00000000..c9754bf7 --- /dev/null +++ b/internal/store/application.go @@ -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 +} diff --git a/internal/store/shared/db.go b/internal/store/shared/db.go index aed5819c..2ea23fbc 100644 --- a/internal/store/shared/db.go +++ b/internal/store/shared/db.go @@ -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 } diff --git a/internal/store/user.go b/internal/store/user.go index fde791fb..0c7ecb40 100644 --- a/internal/store/user.go +++ b/internal/store/user.go @@ -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) {