github.com/vmware/transport-go@v1.3.4/plank/README.md (about)

     1  # Plank
     2  
     3  ## What is Plank?
     4  Plank is just enough of a platform to build whatever you want on top. It is a small yet powerful Golang server that can serve
     5  static contents, single page applications, create and expose microservices over REST endpoints or WebSocket via a built-in
     6  STOMP broker, or even interact directly with other message brokers such as RabbitMQ. All this is done in a consistent
     7  and easy to follow manner powered by Transport Event Bus.
     8  
     9  Writing a service for a Plank server is in a way similar to writing a Spring Boot `Component` or `Service`, because a lot of tedious
    10  plumbing work is already done for you such as creating an instance of a service and wiring it up with HTTP endpoints using routers etc.
    11  Just by following the API you can easily stand up a service, apply any kinds of middleware your application logic calls for, and do all these
    12  dynamically while in runtime, meaning you can conditionally apply a filter for certain REST endpoints, stand up a new service on demand, or
    13  even spawn yet another whole new instance of Plank at a different endpoint.
    14  
    15  All features are cleanly exposed as public API and modules and, combined with the power of Golang's concurrency model using channels,
    16  the Transport Event Bus allows creating a clean application architecture, with straightforward and easy to follow logic.
    17  
    18  Detailed tutorials and examples are currently in progress and will be made public on the [Transport GitHub page](https://vmware.github.io/transport/).
    19  Some topics that will initially be covered are:
    20  
    21  - Writing a simple service and interacting with it over REST and WebSocket
    22  - Service lifecycle hooks
    23  - Middleware management (for REST bridged services)
    24  - Concept of local bus channel and galactic bus channel
    25  - Communicating between Plank instances using the built-in STOMP broker
    26  - Securing your REST and WebSocket endpoints using Auth Provider Manager
    27  
    28  ## Hello world
    29  ### How to build Plank
    30  First things first, you'll need Golang SDK of at least version 1.16 because it uses a built-in
    31  package that was added as of 1.16. Once you made sure your Golang version is at least 1.16,
    32  follow the commands below:
    33  
    34  ```bash
    35  # get all dependencies
    36  go get ./...
    37  
    38  # To build against your operating system
    39  go run build.go
    40  
    41  # or, to build against a specific operating system
    42  BUILD_OS=darwin|linux|windows go run build.go
    43  ```
    44  
    45  Once successfully built, `plank` binary will be ready under `build/`.
    46  
    47  ### Generate a self signed certificate
    48  Plank can run in non-HTTPS mode but it's generally a good idea to always do development in a similar environment where you'll be serving your
    49  audience in public internet (or even intranet). Plank repository comes with a handy utility script that can generate a pair of server certificate
    50  and its matching private key at `scripts/create-selfsigned-cert.sh`. Simply run it in a POSIX shell like below. (requires `openssl` library
    51  to be available):
    52  
    53  ```bash
    54  # generate a pair of x509 certificate and private key files
    55  ./scripts/generate-selfsigned-cert.sh
    56  
    57  # cert/fullchain.pem is your certificate and cert/server.key its matching private key
    58  ls -la cert/
    59  ```
    60  
    61  ### The real Hello World part
    62  Now we're ready to start the application. To kick off the server using the demo app you have built above, type the following and you'll see something like this:
    63  
    64  ```bash
    65  ./build/plank start-server --config-file config.json
    66  
    67   P L A N K
    68  Host                    localhost
    69  Port                    30080
    70  Fabric endpoint         /ws
    71  SPA endpoint            /
    72  SPA static assets       /assets
    73  Health endpoint         /health
    74  Prometheus endpoint     /prometheus
    75  ...
    76  time="2021-08-17T13:28:15-07:00" level=info msg="Service '*services.StockTickerService' initialized successfully" fileName=initialize.go goroutine=44 package=server
    77  time="2021-08-17T13:28:15-07:00" level=info msg="Service channel 'stock-ticker-service' is now bridged to a REST endpoint /rest/stock-ticker/{symbol} (GET)\n" fileName=server.go goroutine=44 package=server
    78  time="2021-08-17T13:28:15-07:00" level=info msg="Starting Fabric broker at localhost:30080/ws" fileName=server.go goroutine=1 package=server
    79  time="2021-08-17T13:28:15-07:00" level=info msg="Starting HTTP server at localhost:30080 with TLS" fileName=server.go goroutine=3 package=server
    80  ```
    81  
    82  Now, open your browser and navigate to https://localhost:30080/rest/stock-ticker/VMW (or
    83  type `curl -k https://localhost:30080/rest/stock-ticker/VMW` in Terminal if you prefer CLI),
    84  and accept the self-signed certificate warning. You will be served a page that shows the latest stock price
    85  for VMware, Inc. Try and swap out `VMW` with another symbol of your choice to further test it out!
    86  
    87  If you navigate to the root at https://localhost:30080, you'll be greeted with a 404!
    88  This is an expected behavior, as the demo app does not serve anything at root `/`, but we will
    89  consider changing the default 404 screen to something that is informational or more appealing at least.
    90  
    91  > NOTE: The sample service is using a loosely gated third party API which imposes
    92  > a substantial limit on how many calls you can make per minute and per day in return for making
    93  > the service free to all.
    94  
    95  > NOTE: Plank, when started, can boot up with a nice image of a wooden plank
    96  > as a splash screen, but it's disabled by default because of a few reasons like unnecessarily
    97  > adding to the binary size and generally being a bloat to some consumers. If you want
    98  > to see it though, build your app with `--tags boot_img` custom tag.
    99  
   100  ## All supported flags and usages
   101  
   102  |Long flag|Short flag|Default value|Required|Description|
   103  |----|----|----|----|----|
   104  |--hostname|-n|localhost|false|Hostname where Plank is to be served. Also reads from `$PLANK_SERVER_HOSTNAME` environment variable|
   105  |--port|-p|30080|false|Port where Plank is to be served. Also reads from `$PLANK_SERVER_PORT` environment variable|
   106  |--rootdir|-r|<current directory>|false|Root directory for the server. Also reads from `$PLANK_SERVER_ROOTDIR` environment variable|
   107  |--static|-s|-|false|Path to a location where static files will be served. Can be used multiple times|
   108  |--no-fabric-broker|-|false|false|Do not start Fabric broker|
   109  |--fabric-endpoint|-|/fabric|false|Fabric broker endpoint (ignored if --no-fabric-broker is present)|
   110  |--topic-prefix|-|/topic|false|Topic prefix for Fabric broker (ignored if --no-fabric-broker is present)|
   111  |--queue-prefix|-|/queue|false|Queue prefix for Fabric broker (ignored if --no-fabric-broker is present)|
   112  |--request-prefix|-|/pub|false|Application request prefix for Fabric broker (ignored if --no-fabric-broker is present)|
   113  |--request-queue-prefix|-|/pub/queue|false|Application request queue prefix for Fabric broker (ignored if --no-fabric-broker is present)|
   114  |--shutdown-timeout|-|5|false|Graceful server shutdown timeout in minutes|
   115  |--output-log|-l|stdout|false|File to output platform logs to|
   116  |--access-log|-l|stdout|false|File to output HTTP server access logs to|
   117  |--error-log|-l|stderr|false|File to output HTTP server error logs to|
   118  |--debug|-d|false|false|Debug mode|
   119  |--no-banner|-b|false|false|Do not print Plank banner at startup|
   120  |--prometheus|-|false|false|Enable Prometheus at /prometheus for metrics|
   121  |--rest-bridge-timeout|-|1|false|Time in minutes before a REST endpoint for a service request to timeout|
   122  
   123  Examples are as follows:
   124  ```bash
   125  # Start a server with all options set to default values
   126  ./plank start-server
   127  
   128  # Start a server with a custom hostname and at port 8080 without Fabric (WebSocket) broker
   129  ./plank start-server --no-fabric-broker --hostname my-app.io --port 8080
   130  
   131  # Start a server with a 10 minute graceful shutdown timeout
   132  # NOTE: this is useful when you run a service that takes a significant amount of time or might even hang while shutting down 
   133  ./plank start-server --shutdown-timeout 10
   134  
   135  # Start a server with platform server logs printing to stdout and access/error logs to their respective files
   136  ./plank start-server --access-log server-access-$(date +%m%d%y).log --error-log server-error-$(date +%m%d%y).log
   137  
   138  # Start a server with debug outputs enabled
   139  ./plank start-server -d
   140  
   141  # Start a server without splash banner
   142  ./plank start-server --no-banner
   143  
   144  # Start a server with Prometheus enabled at /prometheus
   145  ./plank start-server --prometheus
   146  
   147  # Start a server with static path served at `/static` for folder `static`
   148  ./plank start-server --static static 
   149  
   150  # Start a server with static paths served at `/static` for folder `static` and
   151  # at `/public` for folder `public-contents`
   152  ./plank start-server --static static --static public-contents:/public
   153  
   154  # Start a server with a JSON configuration file
   155  ./plank start-server --config-file config.json
   156  ```
   157  
   158  ## Advanced topics (WIP. Coming soon)
   159  ### OAuth2 Client
   160  Plank supports seamless out of the box OAuth 2.0 client that support a few OAuth flows. such as authorization
   161  code grant for web applications and client credentials grant for server-to-server applications.
   162  See below for a detailed guide for each flow.
   163  
   164  #### Authorization Code flow
   165  You'll choose this authentication flow when the Plank server acts as an intermediary that exchanges
   166  the authorization code returned from the authorization server for an access token. During this process you will be
   167  redirected to the identity provider's page like Google where you are asked to confirm the type of 3rd party application and
   168  its scope of actions it will perform on your behalf and will be taken back to the application after successful authorization.
   169  
   170  #### Client Credentials flow
   171  You'll choose this authentication flow when the Plank server needs to directly communicate with another backend service.
   172  This will not require user's consent like you would be redirected to Google's page where you confirm the type of application
   173  requesting your consent for the scope of actions it will perform on your behalf. not requiring any interactions from the user. You will need to
   174  create an OAuth 2.0 Client with `client_credentials` grant before following the steps below to implement the
   175  authentication flow.