# Go and Redis HTTP Server

import { Tabs, TabsContent, TabsList, TabsTrigger } from "zudoku/ui/Tabs"

{/* vale off */}
{/* THIS FILE WAS AUTOGENERATED FROM THE PUBLIC EXAMPLE REPOSITORY. DO NOT EDIT THIS FILE DIRECTLY. */}


This guide explains how to create and deploy a Go app with a Redis database.
To run this example, follow these steps:

1. Install the [unikraft CLI](/cli).
   You need a [BuildKit](https://github.com/moby/buildkit) builder. The easiest way to get one is via [Docker](https://docs.docker.com/engine/install/).
   Alternatively, you can also directly set up and use BuildKit, see the [quick start](https://github.com/moby/buildkit#quick-start).

2. Clone the [`examples` repository](https://github.com/unikraft-cloud/examples) and `cd` into the `examples/httpserver-go1.22-redis` directory:

   ```bash
   git clone https://github.com/unikraft-cloud/examples
   cd examples/httpserver-go1.22-redis/
   ```

Make sure to log into Unikraft Cloud and pick a [metro](/platform/metros) close to you.
This guide uses `fra` (Frankfurt, 🇩🇪):

```bash
unikraft login
```

## Redis

First, deploy the Redis instance.
Redis is an internal service (not publicly accessible), reached via the `go122-redis.internal` domain.
The Redis password is set at runtime via the `REDIS_PASSWORD` environment variable (defaults to `unikraft` if not provided).

```bash
unikraft build ./redis --output <my-org>/httpserver-go-122-redis-db:latest
unikraft run --metro fra \
  -m 256M \
  --scale-to-zero policy=idle,cooldown-time=1000,stateful=true \
  --domain go122-redis.internal \
  -e REDIS_PASSWORD=unikraft \
  --image <my-org>/httpserver-go-122-redis-db:latest
```

Make sure to replace `<my-org>` with your username / org-name.

The output shows the Redis instance details:

```ansi
[1mmetro[22m:           fra
[1mname[22m:            httpserver-go-122-redis-db-2xc9u
[1muuid[22m:            8abe24f6-5670-4e9b-8955-8ec10f3bad21
[1mstate[22m:           [38;2;144;161;185mstarting[0m
[1mimage[22m:           <my-org>/httpserver-go-122-redis-db
[1mresources[22m:
[3m  memory[23m:        256MiB
[3m  vcpus[23m:         1
[1mservice[22m:
[3m  name[23m:          late-sound-rhboe98o
[3m  uuid[23m:          01953c15-8a2e-4c1a-ac24-98a9417c5aa2
[3m  domains[23m:
[3m  - fqdn[23m:        go122-redis.internal
[1mnetworks[22m:
[3m- uuid[23m:          e76ae319-d210-4566-ad75-baed32fc3d2b
[3m  private-ip[23m:    10.0.0.85
[3m  mac[23m:           12:b0:0a:00:00:55
[1mtimestamps[22m:
[3m  created[23m:       just now
[1mscale-to-zero[22m:
[3m  enabled[23m:       true
[3m  policy[23m:        idle
[3m  stateful[23m:      true
[3m  cooldown-time[23m: 1s
```

## Go HTTP Server

Next, deploy the Go HTTP server.
It connects to Redis using the `REDIS_ADDR` and `REDIS_PASS` environment variables:

```bash
unikraft build ./httpserver-go --output <my-org>/httpserver-go-122-redis-app:latest
unikraft run --metro fra \
  -m 256M \
  -p 443:8080/tls+http \
  --scale-to-zero policy=on,cooldown-time=1000 \
  --env REDIS_ADDR=go122-redis.internal:6379 \
  --env REDIS_PASS=unikraft \
  --image <my-org>/httpserver-go-122-redis-app:latest
```

The output shows the instance address and other details:

```ansi
[1mmetro[22m:           fra
[1mname[22m:            httpserver-go-122-redis-app-bnnnc
[1muuid[22m:            82093bcf-fc4c-471c-89c8-4d0b7810e280
[1mstate[22m:           [38;2;144;161;185mstarting[0m
[1mimage[22m:           <my-org>/httpserver-go-122-redis-app
[1mruntime[22m:
[3m  env[23m:
    REDIS_ADDR:  go122-redis.internal:6379
    REDIS_PASS:  unikraft
[1mresources[22m:
[3m  memory[23m:        256MiB
[3m  vcpus[23m:         1
[1mservice[22m:
[3m  name[23m:          frosty-cherry-32qs6na2
[3m  uuid[23m:          bcb9a2af-cfce-4731-9844-5a368696ee7d
[3m  domains[23m:
[3m  - fqdn[23m:        frosty-cherry-32qs6na2.fra.unikraft.app
[1mnetworks[22m:
[3m- uuid[23m:          f9030f74-4e85-445b-a57e-36ec76137c88
[3m  private-ip[23m:    10.0.0.173
[3m  mac[23m:           12:b0:0a:00:00:ad
[1mtimestamps[22m:
[3m  created[23m:       just now
[1mscale-to-zero[22m:
[3m  enabled[23m:       true
[3m  policy[23m:        on
[3m  cooldown-time[23m: 1s
```

In this case, the instance names are `httpserver-go-122-redis-db-2xc9u` and `httpserver-go-122-redis-app-bnnnc`.
They're different for each run.

To set a value in Redis via the Go server, use the URL from the `fqdn` field:

```bash
curl -X POST -d "key=my-key" -d "value=my-value" https://<FQDN>
```

```
Success!
```

You can then retrieve the value:

```bash
curl https://<FQDN>/?key=my-key
```

```
Key "my-key" has value "my-value"
```

You can list information about the instances by running:

```bash
unikraft instances list
```

```ansi
[1mMETRO[22m  [1mNAME[22m                               [1mSTATE[22m    [1mIMAGE[22m                                 [1mARGS[22m  [1mMEMORY[22m  [1mVCPUS[22m  [1mFQDN[22m                                     [1mCREATED[22m
fra    httpserver-go-122-redis-app-bnnnc  [38;2;43;127;255mstandby[0m  <my-org>/httpserver-go-122-redis-app        256MiB  1      frosty-cherry-32qs6na2.fra.unikraft.app  4 minutes ago
fra    httpserver-go-122-redis-db-2xc9u   [38;2;43;127;255mstandby[0m  <my-org>/httpserver-go-122-redis-db         256MiB  1      go122-redis.internal                     5 minutes ago
```

## Clean up

When done, remove the instances:

```bash
unikraft instances delete httpserver-go-122-redis-db-2xc9u httpserver-go-122-redis-app-bnnnc
```

## Learn more

Use the `--help` option for detailed information on using Unikraft Cloud:

```bash
unikraft --help
```

Or visit the [CLI Reference](/cli/unikraft).
{/* vale on */}
