Skip to content

Learning Go through creating simple REST API HTTP server

Notifications You must be signed in to change notification settings

Blinnikov/go-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-rest-api

Learning Go through creating simple REST API HTTP server

Not to forget links:

Commands:

  • Create user:

    curl --request POST --data '{"email":"[email protected]","password":"1234567"}' --verbose http://localhost:8080/users

  • Log In:

    curl --request POST --data '{"email":"[email protected]","password":"1234567"}' -c cookie.txt --verbose http://localhost:8080/sessions

  • Get current user name (passed by middleware through context):

    curl --request GET -b cookie.txt --verbose http://localhost:8080/private/whoami

  • Headers:

    curl --request GET -b cookie.txt -H "Origin: blinnikov.com" --verbose http://localhost:8080/private/whoami

Migrations

  • create
    migrate create -ext sql -dir migrations migration_name  
  • run migration
     migrate -path migrations -database "postgresql://postgres:changeme@localhost/restapi_test?sslmode=disable" up

Serving through TLS

  • install openssl
    brew update
    brew install openssl
  • genereate Self-Signed Certificate
    openssl req -newkey rsa:4096 \
             -x509 \
             -sha256 \
             -days 3650 \
             -nodes \
             -out go-rest-api.crt \
             -keyout go-rest-api.key
  • use http.ListenAndServeTLS
    return http.ListenAndServe(config.BindAddr, srv)
    ->
    return http.ListenAndServeTLS(config.BindAddr, "go-rest-api.crt", "go-rest-api.key", srv)

Deploy to local k8s

  • Give create-configmap.sh script execution permissions

    chmod +x ./k8s/create-configmap.sh 
  • Run this script to create config maps for certificate files

    ./k8s/create-configmap.sh
  • Install all kubernetes objects

    kubectl apply -f ./k8s --recursive

Push Docker images

docker build -t go-rest-api-ibz .
docker image tag go-rest-api-ibz ibazzva/go-rest-api:latest
docker image tag go-rest-api-ibz ibazzva/go-rest-api:1.0.1 
docker login
docker push ibazzva/go-rest-api -a

OR

docker build -t go-rest-api .
docker image tag go-rest-api ghcr.io/blinnikov/go-rest-api:latest
docker image tag go-rest-api ghcr.io/blinnikov/go-rest-api:1.0.1 
echo $CR_PAT | docker login ghcr.io -u blinnikov --password-stdin
docker push ghcr.io/blinnikov/go-rest-api -a