• Pipy Documentation
  • Introduction
    • Overview
    • Concepts
  • Getting Started
    • Build and Install
    • Quick Start
    • Getting help
  • Release
    • Release History
      • 0.10.0-1
      • 0.22.0-31
      • 0.30.0
      • 0.50.0
  • Tutorial
    • Part 1: Hello
    • Part 2: Echo
    • Part 3: Proxy
    • Part 4: Routing
    • Part 5: Loading Balancing
    • Part 6: Configuration
    • Part 7: Plugins
  • Operating
    • Admin GUI
    • CLI
    • Pipy Repo
      • Introduction
      • Quick Start
      • Demo
      • REST API
    • Statistics
  • Reference
    • API
      • Configuration
        • acceptHTTPTunnel()
        • acceptSOCKS()
        • acceptTLS()
        • branch()
        • compressHTTP()
        • compressMessage()
        • connect()
        • connectHTTPTunnel()
        • connectSOCKS()
        • connectTLS()
        • decodeDubbo()
        • decodeHTTPRequest()
        • decodeHTTPResponse()
        • decodeMQTT()
        • decodeMultipart()
        • decodeWebSocket()
        • decompressHTTP()
        • decompressMessage()
        • deframe()
        • demux()
        • demuxHTTP()
        • demuxQueue()
        • depositMessage()
        • detectProtocol()
        • dummy()
        • dump()
        • encodeDubbo()
        • encodeHTTPRequest()
        • encodeHTTPResponse()
        • encodeMQTT()
        • encodeWebSocket()
        • exec()
        • export()
        • fork()
        • handleData()
        • handleMessage()
        • handleMessageBody()
        • handleMessageEnd()
        • handleMessageStart()
        • handleStreamEnd()
        • handleStreamStart()
        • handleTLSClientHello()
        • import()
        • input()
        • link()
        • listen()
        • merge()
        • mux()
        • muxHTTP()
        • muxQueue()
        • onEnd()
        • onStart()
        • output()
        • pack()
        • pipeline()
        • print()
        • read()
        • replaceData()
        • replaceMessage()
        • replaceMessageBody()
        • replaceMessageEnd()
        • replaceMessageStart()
        • replaceStreamEnd()
        • replaceStreamStart()
        • serveHTTP()
        • split()
        • task()
        • tee()
        • throttleConcurrency()
        • throttleDataRate()
        • throttleMessageRate()
        • to()
        • use()
        • wait()
      • Data
        • Data()
        • push()
        • shift()
        • shiftTo()
        • shiftWhile()
        • size
      • Hessian
        • decode()
        • encode()
      • JSON
        • decode()
        • encode()
        • parse()
        • parse()
      • Message
        • body
        • head
        • tail
      • MessageEnd
        • tail
      • MessageStart
        • head
      • Netmask
        • base
        • bitmask
        • broadcast
        • contains()
        • first
        • hostmask
        • last
        • mask
        • next()
        • size
      • StreamEnd
        • error
      • URL
        • auth
        • hash
        • host
        • hostname
        • href
        • origin
        • password
        • path
        • pathname
        • port
        • protocol
        • query
        • search
        • searchParams
        • username
      • URLSearchParams
        • get()
        • getAll()
        • set()
        • toObject()
      • XML
        • Node
          • attributes
          • children
          • name
        • decode()
        • encode()
        • parse()
        • stringify()
      • algo
        • Cache
          • clear()
          • get()
          • remove()
          • set()
        • HashingLoadBalancer
          • add()
          • deselect()
          • next()
          • select()
        • LeastWorkLoadBalancer
          • deselect()
          • next()
          • select()
          • set()
        • Percentile
          • calculate()
          • observe()
          • reset()
        • ResourcePool
          • allocate()
          • free()
        • RoundRobinLoadBalancer
          • deselect()
          • next()
          • select()
          • set()
        • URLRouter
          • add()
          • find()
      • console
        • log()
      • crypto
        • Certificate
        • CertificateChain
        • Cipher
        • Decipher
        • Hash
        • Hmac
        • JWK
        • JWT
        • PrivateKey
        • PublicKey
        • Sign
        • Verify
      • http
        • File
          • from()
          • toMessage()
      • os
        • Stats
          • atime
          • blksize
          • blocks
          • ctime
          • dev
          • gid
          • ino
          • isBlockDevice()
          • isCharacterDevice()
          • isDirectory()
          • isFIFO()
          • isFile()
          • isSocket()
          • isSymbolicLink()
          • mode
          • mtime
          • nlink
          • rdev
          • size
          • uid
        • env
        • readFile()
        • stat()
        • writeFile()
      • pipy()
        • exit()
        • load()
      • repeat()
      • stats
        • Counter
          • decrease()
          • increase()
          • withLabels()
          • zero()
        • Gauge
          • decrease()
          • increase()
          • set()
          • withLabels()
          • zero()
        • Histogram
          • observe()
          • withLabels()
          • zero()
    • PipyJS
      • Language
      • Builtin Objects

    Part 1: Hello

    Ahoy, world!

    As programmers, we're all sorta obsessed with Hello World, aren't we? So why don't we dive in now and do just that!

    Hello, world!

    Type in your terminal the following command:

    pipy -e "console.log('Hello, world!')"

    The '-e' option tells Pipy to evaluate the expression coming after. The expression we're evaluating here is:

    console.log('Hello, world!')

    It says in PipyJS, invoke function console.log() with a string parameter "Hello, world!".

    PipyJS is the dialect of ECMAScript that Pipy speaks. Refer to PipyJS Reference for more information.

    Pipy will now start but quit almost immediately, leaving the following output:

    [INF] Hello, world!
    undefined
    [ERR] [pjs] Script did not result in a Configuration
    [INF] [shutdown] Shutting down...
    [INF] [shutdown] Stopped.
    Done.

    Let's take a look at the output:

    • On the first line, we see "Hello, world!". This is what the function call to console.log() ended up with: a log output to the console.

    • On the second line, we see "undefined". This is the result from evaluating our expression. Since function console.log() has no return values, the evaluated result was undefined.

    • On the third line, we see an error message saying "Script did not result in a Configuration". Here, Configuration is the main API provided by Pipy for pipeline setups, which we'll see being used a lot later on. For now, the only thing you need to know is, Pipy requires a Configuration to continue as a long-lived server. However, since the expression evaluation gave us undefined, which is not a Configuration, so there's nothing else Pipy could do but quit. That's what we see in the output after: Pipy shut down everything and stopped running.

    As you can see from above, Pipy is fundamentally an "expression evaluator". If you say "pipy -e 1+1", it tells you "2", nothing fancy indeed. Surely that isn't what we created Pipy for. To make Pipy do something more interesting, we should write an expression that evaluates to a Configuration.

    Hello, world in HTTP!

    Now let's use Pipy the way it usually is: run an HTTP server.

    pipy -e "pipy().listen(8080).serveHTTP(new Message('Hi, there!'))"

    This time, we're evaluating a slightly more complex expression. The same expression can be rewritten in multiple lines for better clarity:

    pipy()
    .listen(8080)
    .serveHTTP(new Message('Hi, there!'))

    Code dissection

    Take a bit of time to examine the code. Here's what's going on:

    1. At the very first line, the built-in function pipy() is called. It returns a Configuration object. That's what Pipy expects for running a server.

    2. At line 2, by calling method listen() on the Configuration object that we just got from pipy(), we add a port pipeline layout to the configuration that listens on port 8080. Method listen() itself returns the same Configuration object, so we can continually call other methods on it to add more pipeline layouts and filters. That's exactly what we are about to do next.

    3. At line 3, we add a serveHTTP() filter to the pipeline layout that we just added in line 2. The filter takes a Message object as its construction parameter. At runtime, it'll be outputting that same message in response to any input messages.

    To recap, the expression we've written above defined one pipeline layout containing only one filter. To picture it all would be like this:

    image/svg+xmlserveHTTPfilter inputrequestresponsePipelinefilter outputpipeline inputpipeline outputPort8080Client(e.g. curl)

    Test in action

    Input the above command in your terminal, you'll see output like this:

    [INF] [config]
    [INF] [config] Module
    [INF] [config] =======
    [INF] [config]
    [INF] [config] [Listen on 8080 at 0.0.0.0]
    [INF] [config] ----->|
    [INF] [config] |
    [INF] [config] serveHTTP -->|
    [INF] [config] |
    [INF] [config] <-----------------|
    [INF] [config]
    [INF] [listener] Listening on TCP port 8080 at 0.0.0.0

    This time, Pipy didn't quit. Instead, it started listening on port 8080 for HTTP requests.

    Now open up a second terminal window and send a request with curl:

    curl localhost:8080

    You should see the result:

    Hi, there!

    Summary

    In this first part of our tutorial, you've learned how a Pipy program is organized by creating and running a simple one-liner HTTP server.

    Takeaways

    • A Pipy program is made of definitions of pipeline layouts and filters. It always starts off with a call to pipy(), which gives us a Configuration object, which we call various methods on to add pipeline layouts and filters.

    • A port pipeline reads from a network port and writes back to that port whatever its last filter outputs.

    • A serveHTTP() filter takes in HTTP messages as requests and outputs HTTP messages as responses.

    What's next?

    Now you've made a simple Hello World server, but it's barely useful because it always says the same thing. Next, we'll look at how we can respond to different requests with different responses.


    © 2022, Flomesh Team.