Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11840d7
commit 1f3e08d
Showing
14 changed files
with
210 additions
and
4,612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.