Skip to content

Commit

Permalink
adding things
Browse files Browse the repository at this point in the history
  • Loading branch information
b-b3rn4rd committed Jul 10, 2019
1 parent bae175f commit 2f5f0ae
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 71 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.idea/
.DS_Store
vendor/
./lambda/handler
./lambda/sam.out.yaml
./lambda/handler.zip
lambda/handler
lambda/sam.out.yaml
lambda/handler.zip
release*.zip
26 changes: 26 additions & 0 deletions .gometalinter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"EnableGC": true,
"Deadline": "2m",
"DisableAll": true,
"Enable": [
"deadcode",
"gocyclo",
"gofmt",
"goimports",
"golint",
"gosimple",
"ineffassign",
"interfacer",
"lll",
"misspell",
"unconvert",
"unparam",
"unused",
"vet"
],
"Exclude": [
"vendor"
],
"Cyclo": 20,
"LineLength": 220
}
35 changes: 27 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,45 @@ WORKDIR /go/src/github.com/b-b3rn4rd/4-state-barcode/

ARG IMAGE_TAG=1.0.0
COPY . ./
RUN go get -u github.com/golang/dep/cmd/dep \
RUN apt-get -y update \
&& apt-get install -y zip \
&& go get -u github.com/golang/dep/cmd/dep \
&& dep ensure \
# && go get gopkg.in/alecthomas/gometalinter.v2 \
# && gometalinter.v2 --install \
# && gometalinter.v2 ./... \
&& go get gopkg.in/alecthomas/gometalinter.v2 \
&& gometalinter.v2 --install \
&& gometalinter.v2 ./... \
&& go test -v ./...

RUN CGO_ENABLED=0 \
GOOS=linux \
go build \
-ldflags "-X main.Version=${IMAGE_TAG}" \
-a -o app cli/main.go

-a -o app-linux cli/main.go \
&& mkdir -p /tmp/release/linux \
&& cp app-linux /tmp/release/linux/4-state-barcode
RUN CGO_ENABLED=0 \
GOOS=darwin \
go build \
-ldflags "-X main.Version=${IMAGE_TAG}" \
-a -o app-darwin cli/main.go \
&& mkdir -p /tmp/release/darwin \
&& cp app-darwin /tmp/release/darwin/4-state-barcode
RUN CGO_ENABLED=0 \
GOOS=windows \
go build \
-ldflags "-X main.Version=${IMAGE_TAG}" \
-a -o app-windows cli/main.go \
&& mkdir -p /tmp/release/windows \
&& cp app-windows /tmp/release/windows/4-state-barcode
RUN cd /tmp/release && zip -r /tmp/release.zip *
FROM alpine:latest
WORKDIR /home/appuser
ENTRYPOINT ["./app"]
ENTRYPOINT ["./app-linux"]
CMD ["--help"]

RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser -h /home/appuser

COPY --chown=appuser:appuser --from=builder /go/src/github.com/b-b3rn4rd/4-state-barcode/app .
COPY --chown=appuser:appuser --from=builder /go/src/github.com/b-b3rn4rd/4-state-barcode/app-linux .
COPY --chown=appuser:appuser --from=builder /tmp/release.zip /tmp/release.zip
USER appuser
20 changes: 12 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ BUILD_ID ?= 1
BUILD_SHA1 = $(shell git rev-parse --short=7 --verify HEAD)
BUILD_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
REPOSITORY ?= bernard/4-state-barcode
GITHUB_REPOSITORY ?= b-b3rn4rd/australia-post-barcode
MAJOR ?= 1
MINOR ?= 0
ifeq ($(BUILD_BRANCH),master)
Expand All @@ -18,17 +19,21 @@ ci: push
clean:
REPOSITORY=$(REPOSITORY) \
IMAGE_TAG=$(IMAGE_TAG) \
BUILD_SHA1=$(BUILD_SHA1) \
docker-compose --project-name app down || true
docker-compose --project-name barcode down || true
.PHONY: clean

build: clean
REPOSITORY=$(REPOSITORY) \
IMAGE_TAG=$(IMAGE_TAG) \
BUILD_SHA1=$(BUILD_SHA1) \
docker-compose --project-name app up --detach
E=$$(docker wait sut-$(IMAGE_TAG)) && exit $$E

docker-compose --project-name barcode up
docker cp 4-state-barcode-${IMAGE_TAG}:/tmp/release.zip .

@curl -s \
--data-binary @release.zip \
-H "Content-Type: application/zip" \
"https://uploads.github.com/repos/$(GITHUB_REPOSITORY)/releases/$$(curl -s \
--data "{\"tag_name\": \"$(IMAGE_TAG)\"}" \
"https://api.github.com/repos/$(GITHUB_REPOSITORY)/releases?access_token=${GITHUB_TOKEN}" | jq '.id')/assets?name=release-$(IMAGE_TAG).zip&access_token=${GITHUB_TOKEN}"
@echo successfully built docker image
.PHONY: build

Expand All @@ -37,8 +42,7 @@ push: build

REPOSITORY=$(REPOSITORY) \
IMAGE_TAG=$(IMAGE_TAG) \
BUILD_SHA1=$(BUILD_SHA1) \
docker-compose --project-name app push app
docker-compose --project-name barcode push barcode
.PHONY: push

latest:
Expand Down
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The guidelines how to generate 4 state barcode required by Australia Post are ta
## Usage
The library can used in 3 different ways.
### As code
```bash
$ go get -u github.com/b-b3rn4rd/australia-post-barcode
```
```go
package main

Expand All @@ -36,13 +39,14 @@ func main() {
```

There are several optional configuration functions available:
* `OptionPadding(padding int) option`
* `OptionLogger(logger Logger) option`
* `OptionRatio(ratio int) option`
* `OptionFontSize(fontSize int) option`
* `OptionBackgroundColor(color string) option`
* `OptionFontColor(color string) option`
* `OptionalEncoder(encoder Encoder) option`
* `OptionPadding(padding int) Option`
* `OptionLogger(logger Logger) Option`
* `OptionRatio(ratio int) Option`
* `OptionFontSize(fontSize int) Option`
* `OptionBackgroundColor(color string) Option`
* `OptionFontColor(color string) Option`
* `OptionalEncoder(encoder Encoder) Option`
* `OptionBarWidth(width int) Option`

The following example generates barcode's content into a variable

Expand Down Expand Up @@ -76,7 +80,8 @@ func main() {
}
```

## As CLI
###As CLI
Download the latest release and run a suitable executable file

```bash
$ 4-state-barcode --help
Expand All @@ -97,18 +102,39 @@ Flags:

```

## Using Docker Image
### Using Docker Image

```bash
$ docker run \
-v /tmp/barcode:/tmp/barcode \
-it \
--rm 4-state-barcode:latest \
--rm bernard/4-state-barcode:latest \
--barcode "6256439111HELLO" \
--filename /tmp/barcode/barcode.svg

$ cat /tmp/barcode/barcode.svg
<?xml version="1.0"?>
<!-- Generated by SVGo -->
.....
```

## Barcode generation using HTTP server
The `lambda` directory contains AWS CloudFormation and Lambda function's handler to generate
4-state barcodes using HTTP protocol.

![Australia Post barcode](./examples/http-server.png)

### AWS Provisioning
In order to deploy the required infrastructure ensure that you have the correct credentials assumed and run the provided `Makefile`.

```bash
$ S3_BUCKET_NAME=<EXISTING_S3_BUCKET_NAME> make deploy
```
### API calls example
The following HTTP request will generate barcode with barcode value of `5956439111ABA 9` and optional text `Hello World`
and save it as svg file. Notice that by default API endpoint base64 encodes barcode content, hence we are using `base64 --decode`
before saving the response.

```bash
$ curl -s "https://<API_ENDPOINT_ID>.execute-api.<AWS_REGION>.amazonaws.com/prod?barcode=5956439111ABA%209&text=Hello%20World" | base64 --decode > barcode.svg
```
1 change: 1 addition & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

var (
// Version application version, set using ldflags
Version = "undefined"
)

Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3.4"
services:
barcode:
container_name: 4-state-barcode-${IMAGE_TAG}
image: bernard/4-state-barcode:${IMAGE_TAG}
build:
dockerfile: Dockerfile
context: .
args:
IMAGE_TAG: ${IMAGE_TAG}
networks:
- cicd
networks:
cicd:
driver: "bridge"
Binary file added examples/http-server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
144 changes: 144 additions & 0 deletions examples/website.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Australia Post - 4 State Barcode Generator</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<style type="text/css">
.footer {
position: absolute;
bottom: 0;
width: 100%;
height: 60px; /* Set the fixed height of the footer here */
background-color: #f5f5f5;
}

</style>
</head>
<body>
<div class="container" role="main">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="page-header">
<h1>
AUSTRALIA POST 4 STATE BARCODE<small></small>
</h1>
</div>
<p id="barcode" class="text-center"></p>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-barcode"></span> Barcode Generator</h3>
</div>
<div class="panel-body">
<p id="form-error" class="text-capitalize alert alert-danger"></p>


<form id="generate-form" class="form-horizontal">
<div class="form-group">
<label class="col-sm-3 control-label" for="barcode_type">Barcode Type</label>
<div class="col-sm-2">
<select class="form-control" id="barcode_type">
<option value="11">11</option>
<option value="59">59</option>
<option value="62">62</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="dpip">DPID</label>
<div class="col-sm-3">
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-home"></span></div>
<input class="form-control" type="text" maxlength="8" id="dpip"/>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="customer_info">Customer Information (Optional)</label>
<div class="col-sm-4">
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-tag"></span></div>
<input class="form-control" type="text" maxlength="15" id="customer_info"/>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="banner_text">Barcode Text (Optional)</label>
<div class="col-sm-4">
<input class="form-control" id="banner_text"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" id="generate" class="btn btn-default">Generate</button>
</div>
</div>
</form>

</div>
</div>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<div class="col-md-8 col-md-offset-2">
<p class="text-muted text-center" style="padding:15px;">
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> <a href="mailto:bernard@runawaylover.info">Questions ?</a>&nbsp;|&nbsp;
<span class="glyphicon glyphicon-download" aria-hidden="true"></span> <a href="https://github.com/b-b3rn4rd/australia-post-barcode">github</a>
</p>
</div>
</div>
</footer>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
var resetForm = function() {
$('#form-error').html("").hide();
$('#barcode').html("").hide();
}
resetForm();
$('#generate-form').submit(function () {
event.preventDefault();
resetForm();

var type = $("#barcode_type").val();
var dpip = $("#dpip").val();
var customerInfo = $("#customer_info").val();
var barcode = type + dpip + customerInfo
var text = $("#banner_text").val();

if (dpip.length != 8) {
$('#form-error').text('DPID field must be exactly 8 digits long');
$('#form-error').show();
return;
}

$.ajax({
type: "GET",
url: "https://0tqfil7kbc.execute-api.ap-southeast-2.amazonaws.com/prod",
data: "barcode="+barcode+"&text="+ text,
success: function(data){
$('#barcode').html('<img src="data:image/svg+xml;base64,'+data+'" />');
$('#barcode').show();
},
error: function(jqXHR, textStatus, errorThrown) {
var errorMessage = jqXHR.responseText;
var err = jQuery.parseJSON(errorMessage);
$('#form-error').text(err.error);
$('#form-error').show();
}
});
return false;
});
})

</script>
</body>
</html>
Loading

0 comments on commit 2f5f0ae

Please sign in to comment.