Coder Social home page Coder Social logo

jmls / nx-go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nx-go/nx-go

0.0 0.0 0.0 817 KB

๐Ÿˆ Nx plugin to use Go in a Nx Workspace

Home Page: http://npm.im/@nx-go/nx-go

License: MIT License

Shell 0.32% JavaScript 2.43% TypeScript 93.81% Makefile 1.14% Dockerfile 2.31%

nx-go's Introduction

nx-go

Nx plugin to use Go in a Nx workspace.

nx-go logo

Using nx-go in your company? Consider sponsoring me and get priority support in the issues.

Getting started

First, make sure you have a Nx Workspace.

Create a new one using the following command:

pnpm dlx create-nx-workspace go-playground --preset=empty --cli=nx --nx-cloud true
## Or using yarn
# yarn create nx-workspace go-playground --preset=empty --cli=nx --nx-cloud true
## Or using npm
# npm exec create-nx-workspace go-playground --preset=empty --cli=nx --nx-cloud true
cd go-playground

Next, install the nx-go plugin:

pnpm add -D @nx-go/nx-go
## Or using yarn
# yarn add -D @nx-go/nx-go
## Or using npm
# npm install -D @nx-go/nx-go

Create a new application:

nx g @nx-go/nx-go:app api

Enable nx-go plugin for dependency graphing

nx-go includes a plugin that will build the dependency graph of go modules within your nx workspace. This is not enabled by default and must be enabled explicitly.

To enable the nx-go plugin for the dependency graph run the following command:

nx generate @nx-go/nx-go:setup-nx-go-plugin

Usage

You can now run the Nx workspace commands:

Building the application

This command builds the application using the go build command, and stores the output in the dist/<app-name>/ directory.

nx build api

Linting the application

Lint the application using the go fmt command.

nx lint api

Setting up a custom linter

You can define a custom linter that will execute instead of the default go fmt.

The lint executor provides two options to configure the desired linter:

interface LintOptions {
  linter: string
  args: string
}

linter: is the command to execute (example: revive)

args: these are additional arguments to be supplied to the linter (example: -config revive.toml)

The examples above will result in the following command that will be executed:

revive -config revive.toml ./...

Serving the application

Serves the application using the go run command.

nx serve api

Watch mode

To run the application in watch mode you can use gow, after installing it on your machine.

Open the file apps/<app-name>/project.json and in the targets.serve.options object and set the cmd parameter to gow and the cwd parameter to ., like so:

{
  targets: {
    serve: {
      executor: '@nx-go/nx-go:serve',
      options: {
        cmd: 'gow', // This is the cmd that will be used
        cwd: '.', // Set working dir to project root so it picks up changes in `libs/*`
        main: 'apps/api/main.go',
      },
    },
  },
}

Testing the application

Test the application using the go test command.

nx test api

Docker

In order to build Docker containers from the Go api inside the Nx Workspace, there are 2 base images provided:

Using the base images:

# Use nxgo/cli as the base image to do the build
FROM nxgo/cli as builder

# Create app directory
WORKDIR /workspace

# Copy package.json and the lock file
COPY package.json yarn.lock /workspace/

# Install app dependencies
RUN yarn

# Copy source files
COPY . .

# Build apps
RUN yarn build api

# This is the stage where the final production image is built
FROM golang:1.17-alpine as final

# Copy over artifacts from builder image
COPY --from=builder /workspace/dist/apps/api /workspace/api

# Set environment variables
ENV PORT=3000
ENV HOST=0.0.0.0

# Expose default port
EXPOSE 3000

# Start server
CMD [ "/workspace/api" ]

MIT License

Created by Bram Borggreve.

nx-go's People

Contributors

262nos avatar aashis-kishore avatar beeman avatar counterposition avatar phillipcouto avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.