Skip to content

b3tchi/go_training

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Preparation

initiating go project

go mod init web-test 

install external database dependency

go get github.com/lib/pq

Running

starting server

#run local db server
docker start web-hello

#export db connection for the web server
export WEBHELLO_DB_DSN="postgres:https://webhello:pass@localhost/webhello?sslmode=disable"

#start web server
go run ./cmd/api

Tests

testing healthcheck

curl -i localhost:4000/v1/healthcheck

testing healthcheck correct method

curl -i -X POST localhost:4000/v1/healthcheck

testing books api

Create

#add new item
HEADER="Content-Type: application/json"
BODY=$(jo \
  title="The Black Soulstone" \
  published=2001 \
  pages=107 \
  genres=$(jo -a Fiction Mystery) \
  rating=3.5 \
)

echo $BODY
curl -i -H "$HEADER" -d "$BODY" -X POST localhost:4000/v1/books

Read

lastid=$(curl localhost:4000/v1/books | jq '.[-1].id')

#get item
curl -i localhost:4000/v1/books/$lastid
#get item
curl -i localhost:4000/v1/books/125

Update

HEADER="Content-Type: application/json"
BODY=$(jo \
  title="The Black Soulstone" \
  published=2015 \
  pages=207 \
  genres=$(jo -a Mystery Sci-Fi) \
  rating=4.5 \
)

lastid=$(curl localhost:4000/v1/books | jq '.[-1].id')

curl -i -H "$HEADER" -d "$BODY" -X PUT localhost:4000/v1/books/$lastid

Delete

# delete item
lastid=$(curl localhost:4000/v1/books | jq '.[-1].id')

curl -X DELETE localhost:4000/v1/books/$lastid

Delete

# delete item
curl -X DELETE localhost:4000/v1/books/125

Read All

#get collection
curl localhost:4000/v1/books

Database

preparing postgreSQL on local machine

#pull latest pastge image
docker pull postgres

#check if imgage is already there
docker images | grep postgres

#variable must be defined or db will not start
docker run --name web-hello -e POSTGRES_PASSWORD=mylocalpass -d -p 5432:5432 postgres

#check running proces
docker ps

psql -h localhost -p 5432 -U postgres

cleanup docker

docker stop web-hello
docker rm web-hello
docker image rm -f postgres

install psql client(ubuntu)

sudo apt-get install -y postgresql-client

prepare db structure

CREATE DATABASE webhello;
CREATE ROLE webhello WITH LOGIN PASSWORD 'pass';
\c webhello


CREATE TABLE IF NOT EXISTS books (
  id bigserial PRIMARY KEY,
  created_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
  title text NOT NULL,
  published integer NOT NULL,
  pages integer NOT NULL,
  genres text[] NOT NULL,
  rating real NOT NULL,
  version integer NOT NULL DEFAULT 1
);

GRANT SELECT, INSERT, UPDATE, DELETE ON books TO webhello;
GRANT USAGE, SELECT ON SEQUENCE books_id_seq TO webhello;

Go Remote Debugging

installing delve debugger for go

go install github.com/go-delve/delve/cmd/dlv@latest

start remote session

# wait for debugger
dlv debug ./cmd/api --headless --listen=:12345 --api-version=2 --accept-multiclient

# start the procedure --continue flag
dlv debug ./cmd/api --headless --listen=:12345 --api-version=2 --accept-multiclient --continue

connecting vscode to remote session

notes for vscode documentation vscode-go launch.json in folder ./.vscode/launch.json for attach to the session

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Connect to external session",
      "type": "go",
      "debugAdapter": "dlv-dap",
      "request": "attach",
      "mode": "remote",
      "port": 12345
    }
  ]
}

connecting neovim to remote session with nvim-dap

documentation nvim-dap

require("dap").adapters.delve = {
	type = "server",
	host = "127.0.0.1",
	port = "12345",
}

require("dap").configurations.go = {
	{
		type = "delve",
		name = "Attach remote v2",
		mode = "remote",
		request = "attach",
	},
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published