Golang PDF
Golang PDF
with Golang
Esteban Dorado
@Mr_Esti
Esteban Dorado
Pet servers Cattle servers
REST API
What is REST?
● What does it stand for?
Representational State Transfer
● What is it?
A style of software architecture for distributed
systems
● Who/Where/When?
Came about in 2000 doctoral dissertation of Roy
Fielding
REST Protocol
Describes six (6) constraints:
1. Uniform Interface
2. Cacheable
3. Client-Server
4. Stateless
5. Code on Demand
6. Layered System
What is RESTful API?
A RESTful API is an application program interface (API)
that uses HTTP requests to GET, PUT, POST and
DELETE data.
Resources:
Data, files,
methods...
What: Up to you How: HTTP
Example URIs
● GET
http://www.example.com/v1/events/123818237
● POST
http://www.example.com/v1/events
● DELETE
http://www.example.com/v1/events/123818237
● PUT
http://www.example.com/v1/events/123818237
HTTP methods
HTTP Verb CRUD HTTP Codes
http://graphql.org https://grpc.io
EXPLORE
Let’s build your
REST API
API
https://goreportcard.com/report/github.com/mresti/guidetoprodu
ction_api https://github.com/mresti/go_rest_api
api.go Add line
return mux
}
Pipeline: API in golang
Code Goreports
Build
Integration
API: Makefile
# Project specific variables
PROJECT=api
OS=$(shell uname)
GOARCH = amd64
# GO env
GOPATH=$(shell pwd)
GO=go
GOCMD=GOPATH=$(GOPATH) $(GO)
GOBUILD = $(GOCMD) build
.PHONY: compile
compile: darwin linux windows
.PHONY: format
format:
@for gofile in $$(find ./src -name "*.go"); do \
echo "formatting" $$gofile; \
gofmt -w $$gofile; \
done
.PHONY: test
test:
$(GOCMD) test -v -race ./src/...
API: Makefile
.PHONY: run
run:
$(GOCMD) run ./src/main.go
https://goreportcard.com/report/github.com/mresti/go_rest_a
pi
API: dockerfile
FROM golang:1.10-alpine
ENV PATH /api:$PATH
ARG API_PORT
ENV PORT $API_PORT
WORKDIR /api
# Copy binary titan
ADD ./bin/api_linux /api/api
# Modified files for titan
RUN chmod 555 /api/api
# Expose ports
EXPOSE $PORT
# Run Titan
CMD api -port $PORT
API: docker-compose.yml
version: "2.1"
services:
api:
build:
context: .
args:
API_PORT: "8081"
ports:
- "8081:8081"
Research & Development
Beers with friends...
Q&A
<thank-you>
@Mr_Esti