-
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.
- Loading branch information
Showing
8 changed files
with
140 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/cmd/sample-user/sample-user |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.DEFAULT_GOAL := help | ||
|
||
.PHONY: help test | ||
|
||
test: ## Runs tests | ||
go test | ||
|
||
start-db: | ||
@echo "* Creating docker container with PostgreSQL" | ||
docker run --name crud-sample-user-db -d -e POSTGRES_PASSWORD=crudpass -e POSTGRES_USER=cruduser -e POSTGRES_DB=crud -p 54321:5432 postgres:13 | ||
@echo "* Sleeping for 10 seconds to give database time to initialize..." | ||
@sleep 10 | ||
|
||
run-sample-user: clean start-db ## Runs sample-user app | ||
@echo "* Building and starting application..." | ||
@echo "* Please run 'make clean' after terminating the application!" | ||
cd cmd/sample-user && go build . | ||
cd cmd/sample-user && ./sample-user | ||
|
||
clean: ## Removes all created dockers | ||
@echo "* Removing previously created docker container..." | ||
docker rm -f crud-sample-user-db | ||
|
||
help: ## Displays this help | ||
@awk 'BEGIN {FS = ":.*##"; printf "$(MAKEFILE_NAME)\n\nUsage:\n make \033[1;36m<target>\033[0m\n\nTargets:\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[1;36m%-25s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST) |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/go-phings/crud" | ||
structdbpostgres "github.com/go-phings/struct-db-postgres" | ||
_ "github.com/lib/pq" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
const dbDSN = "host=localhost user=cruduser password=crudpass port=54321 dbname=crud sslmode=disable" | ||
const tblPrefix = "p_" | ||
|
||
func main() { | ||
db, err := sql.Open("postgres", dbDSN) | ||
if err != nil { | ||
log.Fatal("Error connecting to db") | ||
} | ||
|
||
orm := structdbpostgres.NewController(db, tblPrefix, &structdbpostgres.ControllerConfig{ | ||
TagName: "crud", | ||
}) | ||
err = orm.CreateTable(&User{}) | ||
if err != nil { | ||
log.Fatalf("Error creating table: %s", err.Error()) | ||
} | ||
|
||
api := crud.NewController(db, tblPrefix, &crud.ControllerConfig{ | ||
PasswordGenerator: func(pass string) string { | ||
passEncrypted, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(passEncrypted) | ||
}, | ||
}) | ||
|
||
var userConstructor = func() interface{} { return &User{} } | ||
var userConstructorForCreate = func() interface{} { return &User_Create{} } | ||
var userConstructorForRead = func() interface{} { return &User_List{} } | ||
var userConstructorForUpdate = func() interface{} { return &User_Update{} } | ||
var userConstructorForList = func() interface{} { return &User_List{} } | ||
|
||
var userConstructorForUpdatePassword = func() interface{} { return &User_UpdatePassword{} } | ||
|
||
http.Handle("/users/", api.Handler("/users/", userConstructor, crud.HandlerOptions{ | ||
CreateConstructor: userConstructorForCreate, // input fields (and JSON payload) for creating | ||
ReadConstructor: userConstructorForRead, // output fields (and JSON output) for reading | ||
UpdateConstructor: userConstructorForUpdate, // input fields (and JSON payload) for updating | ||
ListConstructor: userConstructorForList, // fields to appear when listing items (and JSON output) | ||
})) | ||
http.Handle("/users/password/", api.Handler("/users/password/", userConstructor, crud.HandlerOptions{ | ||
UpdateConstructor: userConstructorForUpdatePassword, // input fields for that one updating endpoint | ||
Operations: crud.OpUpdate, // only updating will be allowed | ||
})) | ||
log.Fatal(http.ListenAndServe(":9001", nil)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
type User struct { | ||
ID int64 `json:"user_id"` | ||
Flags int64 `json:"flags"` | ||
Name string `json:"name" crud:"lenmin:0 lenmax:50"` | ||
Email string `json:"email" crud:"req"` | ||
Password string `json:"password" crud:"hidden password"` | ||
EmailActivationKey string `json:"email_activation_key" crud:"hidden"` | ||
CreatedAt int64 `json:"created_at"` | ||
CreatedBy int64 `json:"created_by"` | ||
LastModifiedAt int64 `json:"last_modified_at"` | ||
LastModifiedBy int64 `json:"last_modified_by"` | ||
} | ||
|
||
type User_Create struct { | ||
ID int `json:"user_id"` | ||
Name string `json:"name" crud:"req lenmin:2 lenmax:50"` | ||
Email string `json:"email" crud:"req"` | ||
Password string `json:"password" crud:"req password"` | ||
} | ||
|
||
type User_Update struct { | ||
ID int `json:"user_id"` | ||
Name string `json:"name" crud:"req lenmin:2 lenmax:50"` | ||
Email string `json:"email" crud:"req"` | ||
} | ||
|
||
type User_UpdatePassword struct { | ||
ID int `json:"user_id"` | ||
Password string `json:"password" crud:"req password"` | ||
} | ||
|
||
type User_List struct { | ||
ID int `json:"user_id"` | ||
Name string `json:"name"` | ||
} |
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,3 +1,3 @@ | ||
package crud | ||
|
||
const VERSION="0.8.0" | ||
const VERSION = "0.8.0" |