Skip to content

Instantly share code, notes, and snippets.

@josephspurrier
Last active March 8, 2020 20:32
Show Gist options
  • Select an option

  • Save josephspurrier/d4f2d97bc9e84f46604ede527a30f7f5 to your computer and use it in GitHub Desktop.

Select an option

Save josephspurrier/d4f2d97bc9e84f46604ede527a30f7f5 to your computer and use it in GitHub Desktop.

Revisions

  1. josephspurrier revised this gist Mar 8, 2020. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    include ${GOPATH}/.env

    # Set local environment variables.
    MYSQL_NAME=mysql56
    MYSQL_NAME=govueapp_db_1

    .PHONY: docker-build
    docker-build:
    @@ -36,6 +36,11 @@ ui-dev:
    # Start the UI.
    cd ${GOPATH}/src/app/ui && npm run dev

    .PHONY: ui-test
    ui-test:
    # Run the Jest UI tests.
    cd ${GOPATH}/src/app/ui && npm test

    .PHONY: api-dep
    api-dep:
    # Restore the dependencies. Get gvt if it's not found in $PATH.
  2. josephspurrier revised this gist Mar 7, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -78,7 +78,7 @@ swagger-gen:
    ## MacOS
    sed -i '' -e 's/example/x\-example/' ${GOPATH}/src/app/api/static/swagger/swagger.json
    ## Linux
    sed -i'' -e 's/example/x\-example/' ${GOPATH}/src/app/api/static/swagger/swagger.json
    #sed -i'' -e 's/example/x\-example/' ${GOPATH}/src/app/api/static/swagger/swagger.json

    # Validate the swagger spec.
    swagger validate ${GOPATH}/src/app/api/static/swagger/swagger.json
  3. josephspurrier revised this gist Mar 7, 2020. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,12 @@ api-test:
    # Run the Go tests.
    cd ${GOPATH}/src/app/api && go test ./...

    .PHONY: clean
    clean:
    # Remove binaries.
    rm -rf ${GOPATH}/src/app/api/cmd/api/api
    rm -rf ${GOPATH}/src/app/api/cmd/dbmigrate/dbmigrate

    .PHONY: gvt-get
    gvt-get:
    # Download gvt.
    @@ -80,12 +86,6 @@ swagger-gen:
    # Serve the spec for the browser.
    swagger serve -F=swagger ${GOPATH}/src/app/api/static/swagger/swagger.json

    .PHONY: clean
    clean:
    # Remove binaries.
    rm -rf ${GOPATH}/src/app/api/cmd/api/api
    rm -rf ${GOPATH}/src/app/api/cmd/dbmigrate/dbmigrate

    .PHONY: db-init
    db-init:
    # Launch database container.
    @@ -116,9 +116,9 @@ db-rm:
    .PHONY: nuxt-upgrade
    nuxt-upgrade:
    # Upgrade nuxt to the latest version.
    $(shell npm bin)/npm upgrade nuxt
    cd ${GOPATH}/src/app/ui && npm upgrade nuxt

    .PHONY: nuxt-version
    nuxt-version:
    # Output the version of nuxt.
    $(shell npm bin)/nuxt --version
    ${GOPATH}/src/app/ui/node_modules/.bin/nuxt --version
  4. josephspurrier created this gist Mar 7, 2020.
    4 changes: 4 additions & 0 deletions .env
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    APP_VERSION=1.0
    MYSQL_CONTAINER=mysql:5.6
    MYSQL_ROOT_PASSWORD=password
    MYSQL_HOST=db
    124 changes: 124 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    # This Makefile is an easy way to run common operations.
    # Execute commands this:
    # * make db-init
    # * make api-dep
    # * make api-dev
    # * make nuxt-version
    #
    # Tip: Each command is run on its own line so you can't CD unless you
    # connect commands together using operators. See examples:
    # A; B # Run A and then B, regardless of success of A
    # A && B # Run B if and only if A succeeded
    # A || B # Run B if and only if A failed
    # A & # Run A in background.
    # Source: https://askubuntu.com/a/539293
    #
    # Tip: Use $(shell app param) syntax when expanding a shell return value.

    # Load the shared environment variables (shared with docker-compose.yml).
    include ${GOPATH}/.env

    # Set local environment variables.
    MYSQL_NAME=mysql56

    .PHONY: docker-build
    docker-build:
    # Build the docker containers.
    bash ${GOPATH}/bash/build-containers.sh

    .PHONY: ui-dep
    ui-dep:
    # Install the dependencies.
    cd ${GOPATH}/src/app/ui && npm install

    .PHONY: ui-dev
    ui-dev:
    # Start the UI.
    cd ${GOPATH}/src/app/ui && npm run dev

    .PHONY: api-dep
    api-dep:
    # Restore the dependencies. Get gvt if it's not found in $PATH.
    which gvt || go get github.com/FiloSottile/gvt
    cd ${GOPATH}/src/app/api && gvt restore

    .PHONY: api-dev
    api-dev:
    # Start the API.
    go run ${GOPATH}/src/app/api/cmd/api/main.go

    .PHONY: api-test
    api-test:
    # Run the Go tests.
    cd ${GOPATH}/src/app/api && go test ./...

    .PHONY: gvt-get
    gvt-get:
    # Download gvt.
    go get github.com/FiloSottile/gvt

    .PHONY: swagger-get
    swagger-get:
    # Download the Swagger generation tool.
    go get github.com/go-swagger/go-swagger/cmd/swagger

    .PHONY: swagger-gen
    swagger-gen:
    # Generate the swagger spec.
    cd ${GOPATH}/src/app/api/cmd/api && \
    swagger generate spec -o ${GOPATH}/src/app/api/static/swagger/swagger.json

    # Replace 'example' with 'x-example' in the swagger spec.
    ## MacOS
    sed -i '' -e 's/example/x\-example/' ${GOPATH}/src/app/api/static/swagger/swagger.json
    ## Linux
    sed -i'' -e 's/example/x\-example/' ${GOPATH}/src/app/api/static/swagger/swagger.json

    # Validate the swagger spec.
    swagger validate ${GOPATH}/src/app/api/static/swagger/swagger.json

    # Serve the spec for the browser.
    swagger serve -F=swagger ${GOPATH}/src/app/api/static/swagger/swagger.json

    .PHONY: clean
    clean:
    # Remove binaries.
    rm -rf ${GOPATH}/src/app/api/cmd/api/api
    rm -rf ${GOPATH}/src/app/api/cmd/dbmigrate/dbmigrate

    .PHONY: db-init
    db-init:
    # Launch database container.
    docker run -d --name=${MYSQL_NAME} -p 3306:3306 -e MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} ${MYSQL_CONTAINER}

    .PHONY: db-start
    db-start:
    # Start the stopped database container.
    docker start ${MYSQL_NAME}

    .PHONY: db-stop
    db-stop:
    # Stop the running database container.
    docker stop ${MYSQL_NAME}

    .PHONY: db-reset
    db-reset:
    # Drop the database, create the database, and perform the migrations.
    docker exec ${MYSQL_NAME} sh -c "exec mysql -h 127.0.0.1 -uroot -p${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE IF EXISTS main;'"
    docker exec ${MYSQL_NAME} sh -c "exec mysql -h 127.0.0.1 -uroot -p${MYSQL_ROOT_PASSWORD} -e 'CREATE DATABASE IF NOT EXISTS main DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;'"
    go run ${GOPATH}/src/app/api/cmd/dbmigrate/main.go

    .PHONY: db-rm
    db-rm:
    # Stop and remove the database container.
    docker rm -f ${MYSQL_NAME}

    .PHONY: nuxt-upgrade
    nuxt-upgrade:
    # Upgrade nuxt to the latest version.
    $(shell npm bin)/npm upgrade nuxt

    .PHONY: nuxt-version
    nuxt-version:
    # Output the version of nuxt.
    $(shell npm bin)/nuxt --version
    35 changes: 35 additions & 0 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # Docker Reference:
    # https://github.com/docker/docker.github.io/blob/master/compose/compose-file/compose-versioning.md#version-3
    # Requires: Docker Engine 17.09.0 and higher
    version: '3.4'

    # Microservice template.
    x-template: &service-template
    restart: always
    env_file:
    - .env
    networks:
    - cnet

    # Networks.
    networks:
    cnet:
    driver: bridge

    # Services.
    services:
    db:
    <<: *service-template
    image: "${MYSQL_CONTAINER}"
    ports:
    - "3306:3306"
    ui:
    <<: *service-template
    image: "govueapp-ui:${APP_VERSION}"
    ports:
    - "80:80"
    api:
    <<: *service-template
    image: "govueapp-api:${APP_VERSION}"
    ports:
    - "8081:8081"