Skip to content

Commit

Permalink
migrate to golang from node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
junkboy0315 committed Nov 8, 2019
1 parent 11840d7 commit 1f3e08d
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 4,612 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Expand Up @@ -10,10 +10,10 @@ jobs:
- setup_remote_docker
- run:
name: Build Docker image
command: docker build -t app .
command: docker build --target target_for_development -t app .
- run:
name: Test Docker image
command: docker run -it --rm app bash -c "yarn && yarn test"
command: docker run -it --rm app bash -c "go test"
frontend:
docker:
- image: circleci/node:12.12.0-buster
Expand Down
36 changes: 31 additions & 5 deletions Dockerfile
@@ -1,9 +1,10 @@
FROM golang:1.13.4-buster

###############################
# pict
# for development
###############################

### pict ###
FROM golang:1.13.4-buster AS target_for_development

RUN apt update
RUN apt -y install clang
RUN which clang
Expand All @@ -15,8 +16,33 @@ WORKDIR /root/pict
RUN make && install ./pict /usr/bin
RUN which pict

# for auto reload
### golang ###
RUN go get github.com/gin-contrib/cors
RUN go get github.com/gin-contrib/gzip
RUN go get github.com/gin-gonic/gin
RUN go get github.com/stretchr/testify/assert

# to enable auto reloading while developing
RUN go get github.com/pilu/fresh

COPY main.go /go/src/github.com/junkboy0315/pairwise-pict-online/
COPY main_test.go /go/src/github.com/junkboy0315/pairwise-pict-online/
WORKDIR /go/src/github.com/junkboy0315/pairwise-pict-online

# start server
CMD go run main.go
CMD ["fresh"]

###############################
# for production
###############################

FROM target_for_development AS target_for_compilation
RUN CGO_ENABLED=0 GOOS=linux go build -v -o server

# Use clean image for a lean production container.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM debian:buster-20191014-slim AS target_for_production
COPY --from=target_for_compilation /root/pict/pict /usr/bin/
RUN which pict
COPY --from=target_for_compilation /go/src/github.com/junkboy0315/pairwise-pict-online/server /server
CMD ["/server"]
9 changes: 0 additions & 9 deletions README.md
Expand Up @@ -4,12 +4,3 @@

An online service that easily generates pair-wise test cases.
It's powered by [Microsoft Pict](https://github.com/microsoft/pict) under the hood.

## Deployment

NOTE: This section is a personal memo.

```sh
gcloud config set project pairwise-pict-online
gcloud builds submit --tag asia.gcr.io/pairwise-pict-online/pict-api .
```
30 changes: 30 additions & 0 deletions docker-compose.prod.yml
@@ -0,0 +1,30 @@
version: '3.4'
services:
pict_api_prod:
build:
context: .
target: target_for_production
environment:
NODE_ENV: production
container_name: pict_api_prod
volumes:
- .:/go/src/github.com/junkboy0315/pairwise-pict-online
working_dir: /go/src/github.com/junkboy0315/pairwise-pict-online
ports:
- '8080:8080'
# following frontend will not work because
# browsers doe's not support CORS on localhost.
# CORS should be disabled to test production builds on local environment.
# https://stackoverflow.com/a/10892392/6574720
pict_react_prod:
image: node:12.12.0-alpine
container_name: pict_react_prod
environment:
NODE_ENV: development # run frontend as dev mode to point to the local API
volumes:
- ./front:/root/front
- /root/front/node_modules/
working_dir: /root/front
ports:
- '3000:3000'
command: sh -c 'yarn && yarn start'
13 changes: 6 additions & 7 deletions docker-compose.yml
@@ -1,14 +1,15 @@
# This file is only used in the development enviroment

version: '3.3'
version: '3.4'
services:
pict_api:
build: .
build:
context: .
target: target_for_development
environment:
NODE_ENV: development
container_name: pict_api
volumes:
- .:/go/src/github.com/junkboy0315/pairwise-pict-online
- ./:/go/src/github.com/junkboy0315/pairwise-pict-online/
- /go/src/github.com/junkboy0315/pairwise-pict-online/front # to ignore front/node_modules
working_dir: /go/src/github.com/junkboy0315/pairwise-pict-online
command: fresh # run `main.go` with auto reload
ports:
Expand All @@ -23,8 +24,6 @@ services:
- ./front:/root/front
- /root/front/node_modules/
working_dir: /root/front
depends_on:
- pict_api
ports:
- '3000:3000'
command: sh -c 'yarn && yarn start'
85 changes: 79 additions & 6 deletions main.go
@@ -1,15 +1,88 @@
package main

import (
"fmt"
"net/http"
"io/ioutil"
"log"
"os"
"os/exec"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
type GenerateCasesRequestBody struct {
Factors string `json:"factors"`
}

func SetupRouter() *gin.Engine {
// set gin as release mode
if os.Getenv("NODE_ENV") != "development" {
gin.SetMode(gin.ReleaseMode)
}

router := gin.Default()

// CORS settings
// https://github.com/gin-contrib/cors#using-defaultconfig-as-start-point
config := cors.DefaultConfig()
config.AllowOrigins = []string{
"*",
}
if os.Getenv("NODE_ENV") != "development" {
config.AllowOrigins = []string{
"https://pairwise.yuuniworks.com",
}
}
router.Use(cors.New(config))

// enable gzip
router.Use(gzip.Gzip(gzip.DefaultCompression))

router.GET("/", func(c *gin.Context) {
c.String(200, "ok")
})

router.POST("/generate_cases", func(c *gin.Context) {
// extract request body
var generateCasesRequestBody GenerateCasesRequestBody
c.BindJSON(&generateCasesRequestBody)

// create temporary test-factors file
factors := []byte(generateCasesRequestBody.Factors)
tmpfile, err := ioutil.TempFile("", "temp-test-factors-")
if err != nil {
log.Print(err)
c.Status(500)
return
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write(factors); err != nil {
log.Print(err)
c.Status(500)
return
}
if err := tmpfile.Close(); err != nil {
log.Print(err)
c.Status(500)
return
}

// exec `pict` command
pictCmd := exec.Command("pict", tmpfile.Name())
pictOut, err := pictCmd.CombinedOutput() // get both stdin & stdout
if err != nil {
c.JSON(400, string(pictOut))
return
}

c.JSON(200, string(pictOut))
})

return router
}

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
router := SetupRouter()
router.Run()
}
50 changes: 50 additions & 0 deletions main_test.go
@@ -0,0 +1,50 @@
package main

import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

func performRequest(r http.Handler, method, path string, body io.Reader) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, body)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}

func TestReadiness(t *testing.T) {
router := SetupRouter()

response := performRequest(router, "GET", "/", nil)

assert.Equal(t, http.StatusOK, response.Code)
assert.Equal(t, "ok", response.Body.String())
}

func TestGeneratingCases(t *testing.T) {
requestBodyStruct := gin.H{
"factors": "SomeFactor: 1234567,2,3,4",
}
requestBody, _ := json.Marshal(requestBodyStruct)

router := SetupRouter()

response := performRequest(
router,
"POST",
"/generate_cases",
bytes.NewReader(requestBody))

assert.Equal(t, http.StatusOK, response.Code)
responseBody := response.Body.String()
assert.True(t, strings.Contains(responseBody, "SomeFactor"))
assert.True(t, strings.Contains(responseBody, "1234567"))
}
32 changes: 0 additions & 32 deletions package.json

This file was deleted.

6 changes: 6 additions & 0 deletions scripts/push_to_gcs_as_production_build.sh
@@ -0,0 +1,6 @@
#/bin/sh

cd ~/go/src/github.com/junkboy0315/pairwise-pict-online
docker build --target target_for_production -t pict_api:production .
docker tag pict_api:production asia.gcr.io/pairwise-pict-online/pict_api:production
docker push asia.gcr.io/pairwise-pict-online/pict_api:production
6 changes: 6 additions & 0 deletions scripts/push_to_gcs_as_staging_build.sh
@@ -0,0 +1,6 @@
#/bin/sh

cd ~/go/src/github.com/junkboy0315/pairwise-pict-online
docker build --target target_for_production -t pict_api:staging
docker tag pict_api:staging asia.gcr.io/pairwise-pict-online/pict_api:staging
docker push asia.gcr.io/pairwise-pict-online/pict_api:staging
29 changes: 0 additions & 29 deletions src/generate_cases.js

This file was deleted.

36 changes: 0 additions & 36 deletions src/index.js

This file was deleted.

0 comments on commit 1f3e08d

Please sign in to comment.