diff --git a/.gitignore b/.gitignore index 76b5cf8..d34b3e2 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ TODO.md logs.txt .idea/ secret.md -app.env \ No newline at end of file +tmp/ \ No newline at end of file diff --git a/app.env b/app.env new file mode 100644 index 0000000..cb1c622 --- /dev/null +++ b/app.env @@ -0,0 +1,19 @@ +POSTGRES_HOST=127.0.0.1 +POSTGRES_USER=postgres +POSTGRES_PASSWORD=password123 +POSTGRES_DB=golang-gorm +POSTGRES_PORT=6500 + +PORT=8000 +CLIENT_ORIGIN=http://localhost:3000 + +ACCESS_TOKEN_PRIVATE_KEY=LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlCUEFJQkFBSkJBTzVIKytVM0xrWC91SlRvRHhWN01CUURXSTdGU0l0VXNjbGFFKzlaUUg5Q2VpOGIxcUVmCnJxR0hSVDVWUis4c3UxVWtCUVpZTER3MnN3RTVWbjg5c0ZVQ0F3RUFBUUpCQUw4ZjRBMUlDSWEvQ2ZmdWR3TGMKNzRCdCtwOXg0TEZaZXMwdHdtV3Vha3hub3NaV0w4eVpSTUJpRmI4a25VL0hwb3piTnNxMmN1ZU9wKzVWdGRXNApiTlVDSVFENm9JdWxqcHdrZTFGY1VPaldnaXRQSjNnbFBma3NHVFBhdFYwYnJJVVI5d0loQVBOanJ1enB4ckhsCkUxRmJxeGtUNFZ5bWhCOU1HazU0Wk1jWnVjSmZOcjBUQWlFQWhML3UxOVZPdlVBWVd6Wjc3Y3JxMTdWSFBTcXoKUlhsZjd2TnJpdEg1ZGdjQ0lRRHR5QmFPdUxuNDlIOFIvZ2ZEZ1V1cjg3YWl5UHZ1YStxeEpXMzQrb0tFNXdJZwpQbG1KYXZsbW9jUG4rTkVRdGhLcTZuZFVYRGpXTTlTbktQQTVlUDZSUEs0PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQ== +ACCESS_TOKEN_PUBLIC_KEY=LS0tLS1CRUdJTiBQVUJ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3dBd1NBSkJBSWFJcXZXeldCSndnYjR1SEhFQ01RdHFZMTI5b2F5Rwo1WTBpRnBudWtCdVR6UWVZUFpBOGx4OC9lTUh3Rys1MlJGR3VxMmE2N084d2s3TDR5dnY5dVY4Q0F3RUFBUT09Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ== +REFRESH_TOKEN_EXPIRED_IN=60m +REFRESH_TOKEN_MAXAGE=60 \ No newline at end of file diff --git a/migrate/migrate.go b/migrate/migrate.go index cf6fb7a..84832c6 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -18,6 +18,7 @@ func init() { } func main() { + initializers.DB.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"") initializers.DB.AutoMigrate(&models.User{}, &models.Post{}) fmt.Println("👍 Migration complete") } diff --git a/readMe.md b/readMe.md index 6bfb0bd..c704481 100644 --- a/readMe.md +++ b/readMe.md @@ -1,4 +1,37 @@ -# Build Golang RESTful API with Gorm, Gin and Postgres +# Build a RESTful CRUD API with Golang + +This article will teach you how to create a CRUD RESTful API in a Golang environment that runs on a Gin Gonic server and uses a PostgreSQL database. We’ll also discuss how you can build models, connect to the running SQL database server, and run database migrations with the GORM library. + +![Build a RESTful CRUD API with Golang](https://codevoweb.com/wp-content/uploads/2022/08/Build-a-RESTful-CRUD-API-with-Golang.webp) + +## Topics Covered + +- What is a REST API +- What is a REST API +- What is GORM +- What is Gin Gonic +- Create the Database Models with GORM +- Generating the SQL Table with GORM +- Creating CRUD Functions in a RESTful API + - Create a Constructor for the CRUD Operations + - Create Operation Route Handler + - Update Operation Route Handler + - Retrieve a Single Record Route Handler + - Retrieve All Records Route Handler + - Delete Operation Route Handler +- Creating Routes for the CRUD Operations +- Update/Configure the Golang API Server +- Testing the Golang CRUD API with Postman + - Log into the API + - Creating a New Record + - Updating the Record + - Request a Single Record + - Retrieve all Records with Paginated Results + - Delete a Record + +Read the entire article here: [https://codevoweb.com/build-restful-crud-api-with-golang](https://codevoweb.com/build-restful-crud-api-with-golang) + +Articles in this series: ### 1. How to Setup Golang GORM RESTful API Project with Postgres