Skip to content

AAronL1968/golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

golang

A place for my Go tutorials, etc.

The Go tutorials demonstrated separately how to create and access modules, how to connect to a postgres database, and how to create api endpoints. I am combining the three lessons for the "Albums" project.

This application will create a schema named tutorial_sandbox in the postgres database indicated by the operating system environment variables described below. The schema will contain a single table named tutorial_sandbox.album, which we can query and modify via the API endpoints we are creating.

Prerequisites:
1. Go must be installed
2. The user must have write access credentials to a postgres instance.

Usage instructions:
1. Set the following operating system environment variables to values that are appropriate for the postgres instance you will be using (using environment variables avoids the need for hard-coding the server connection info):
    PQHOST (defaults to "localhost" if the PQHOST environment variable is empty or not found)
    PQUSER (defaults to "postgres" if the PQUSER environment variable is empty or not found)
    PQPW (defaults to "password" if the PQPW environment variable is empty or not found)
    PQDB (defaults to "postgres" if the PQDB environment variable is empty or not found)
    PQSSL (defaults to "disable" if the PQSSL environment variable is empty or not found)
    PQPORT (defaults to 5432 if the PQPORT environment variable is empty or not found)

To set the environment variables, open a Terminal or Command Prompt window. Linux/Bash Terminal uses the export command, and Windows Command Prompt uses the set command.

    Examples for setting environment variables in Windows and in Linux/Mac:
    Set the PQHOST environment variable to "mypostgres.example.com", PQUSER to "test.username", and PQPW to "test.pw":
        In Windows, you would type the following into a Command Prompt window after the > prompt:
          set PQHOST=mypostgres.example.com
          set PQUSER=test.username
          set PQPW=test.pw
        In a Linux/Bash Terminal (Mac), you would type the following into a Terminal window after the $ prompt:
          export PQHOST=mypostgres.example.com
          export PQUSER=test.username
          export PQPW=test.pw

2. After setting all of the necessary environment variables, in the same Terminal or Command Prompt window, navigate to ./albums/web-service-gin and type the following commands at the prompt ($ or >):
    go mod init
    go work use .
    go mod edit -replace example.com/dbaccess=../dbaccess
    go mod tidy
    go run .

3. Use curl in a separate Terminal or Command Prompt window to select and modify values.

    Examples for selecting and modifying data with curl:
    Get all albums:
      curl http://localhost:8080/albums --header "Content-Type: application/json" --request "GET"

    Get the album having ID 3:
      curl http://localhost:8080/albums/3 --header "Content-Type: application/json" --request "GET"

    Post an insert/update:
      curl http://localhost:8080/albums --include --header "Content-Type: application/json" --request "POST" --data "{ \"id\": 4,\"title\": \"The Modern Sound of Betty Carter\",\"artist\": \"Betty Carter\",\"price\": 56.78}"

4. In a postgres session connected to the instance indicated by the environment variable settings that were configured in step 1 above, run the following query to verify any inserts/updates:
    select * from tutorial_sandbox.album;

Notes about the jazz album project itself:  a real record store would have far more properties for albums, as the price would be dependent on the condition of the vinyl, the condition of the cover/sleeve/insert, whether or not it is an original pressing, etc. There is not even a quantity. I decided to stop overthinking it after a while.

About

A place for my Go tutorials, etc.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages