Deploying a static file server.

  • Let's start with a basic go static file server, copy paste below to server.go

    package main
    import (
    func main() {
    fs := http.FileServer(http.Dir("static"))
    http.Handle("/", fs)
    log.Println("Listening...on 8080")
    http.ListenAndServe(":8080", nil)
  • Now build server.go

    $ GOOS=linux go build server.go
  • Prepare the HTML content.

    $ mkdir static
    $ cd static

    Create hello.html

    <!doctype html>
    <meta charset="utf-8">
    <title>A static page</title>
    <h1>Hello from a static page</h1>
  • Create a config file config.json

    "Dirs" : ["static"],
    "Files":["/lib/x86_64-linux-gnu/libnss_dns.so.2", "/etc/ssl/certs/ca-certificates.crt"]

    If you are on linux you can use the above example to enable dns/tls, otherwise you can use this for local Mac examples. NOTE: This is more than likely to change in the very near future.


    "Dirs" : ["static"]

    The directory structure should look like below after these steps.

    ├── config.json
    ├── server
    └── static
    └── hello.html
  • Package and deploy

    $ ops run -p 8080 -c config.json server

    Notice that we are using KVM user-mode networking and have to forward host port 8080 to VM.

  • Curl it

    curl http://localhost:8080/hello.html

To learn more about various config options visit OPS GitHub repository. More examples can be found from the ops-examples repository.