Book Image Shortcode for Hugo
Lets link to bookshop
- tags
- hugo
Deno is a new V8 based TypeScript and JavaScript programming language that works like a command line web browser. It was written by the creaters of NodeJS and seeks to fix some of the issues with Node around it’s security model and pacakge management. Lets look at building a simple webservice using redis.
If you are in OSX you can use homebrew
or install from a shell installer:
|
|
I’m using asdf so it’s
|
|
|
|
Which yields:
|
|
Running it again it’s cached in your local directory, which defaults to $HOME/.cache/deno
. You can look in this directory to see what’s in there.
We can also install this “tool” locally to make it an executable.
deno install welcome https://deno.land/std/examples/welcome.ts
This pulls down all of the dependancies and installs an executable in ~/.deno/bin
, which you may need to add to your path.
|
|
If you delete the ~/.cache/deno
directory, the next time you run welcome
it will download all of the code again. This is fun because you can easily distribute and install scripts as needed and it takes care of pulling down all the dependacies that are needed.
Lets see how to connect to redis. We will use the deno-redis package, by simply adding it as an import
statement at the top of the file. Then, when we run deno run redis.ts
it will download automatically without needing to use a seperate node_modules
directory!
We also will pull REDIS_URL
from the environment to let us control where things are deployed later. There’s a weird quirk with the way that deno
parses URL
that reflects a weird way that browsers parse URLs, so we’re tweaking it a little bit to make sure that we can pull out the password and hostname if they are set.
The other thing we are doing is export
ing the redis
object. We will use this later to build off of in another script.
|
|
The first time you run deno run redis.ts
it will download all of the needed components, and then it will give you the following error:
|
|
By default deno runs everything in a sandbox environment that has no access to the local environment. You need to specify what permissions this code – that gets pulled down from the internet on demand – has. We need to give it --allow-env
to see the environment (where you may have secured passwords stached) and, also in this case, --allow-net
because we want our script to be able to talk to redis.
|
|
…and we need to startup a redis server. If you have docker installed, the easiest way to do this (for a temporary test server) is:
|
|
Which means start a container based on the redis
image, --rm
remove it when done, -it
give it an interactive terminal, and -p 6379:6379
expose the local container port 6379
as 6379
on the local network.
Once done, when we run our script we get:
|
|
This is the example that you see everywhere, except that
redis
from our previous scriptredis.incr
to add a counter for each “page”
|
|
Lets run this now with deno run --allow-env --allow-net web.ts
. With you browser go to http://localhost:8080/ and refresh to watch the counter go up and up. Exiciting!
deps.ts
As scripts get bigger we want to be able to centralize which versions of what we are using. Instead of having a package.json
file where everything is listed, we can simply organize our project in a way that keeps it cleaner. The standard idiom is to have a deps.js
file that simply imports and exports things from one place. Lets create that now and update our source files to go through that.
|
|
Then in web.ts
change the imports to
|
|
and in redis.ts
change the imports to
|
|
Now everything is centralized in one place. Running
|
|
Will make sure that everything in is the local cache, which we will take advantage of when building our Docker image.
When we package up our application for deployment, we don’t want it to be pulling random source code from the internet when it first starts up. We’ll make that part of our build process. Here is a simple Dockerfile that
hayd/alpine-deno:1.0.4
deps.ts
file into the imagedeno cache deps.ts
which pulls the require dependancies into the local cache
|
|
Now from here you are ready to deploy this on whatever hosting provider you need. It needs a redis instance that it will find by looking at REDIS_URL
in the environment, which is the standard that heroku
or dokku
(and probably others) use.
Lets test it out locally first. You’ll need to find your ip address which on linux you can do with hostname -I
. Change the REDIS_URL
to point to your local instance.
|
|
Now when you go to http://localhost:8080/ you should see deno running in your local container.
From here it will be straightforward to do a git push heroku master
or whatever to deploy on your provider of choice.
Deno is like a web browser for the command line. One thing that webbrowsers do amazingly well is the distrubtion of applications – just go to a URL and you have it on your machine. deno install
brings some of that magic into the command line. The ecosystem is still very young, but with ES modules being the extension mechanism that can pull code from wherever it lives on the internet you can imagine a much more distributed application mechanism that doesn’t rely on any particular centralized service.
Previously
Next