SDK quickstart
Serve a page only on your Network from one program, and call it by name from another.
You will run two small programs. hello-server answers HTTP on your Network only and greets each caller by the Device
name it calls from. The client calls it by the name of a Service, hello. Both join with a Join key and leave the Network when they stop.
1. Make a Join key
In the console, under Join keys, create a reusable key named after what it is for, such as sdk quickstart.
Copy it while it is on screen, and give it to both programs through the environment:
export TILTAS_JOIN_KEY=qlj_your_join_key2. Install
go get tiltas.openpanel.sh/go3. Serve on the Network
Save the server and start it as the Device hello-server:
// Command hello-server answers HTTP requests that come over a Tiltas
// Network, and only those: it opens no port on the computer it runs on.
//
// TILTAS_JOIN_KEY=qlj_… TILTAS_NAME=hello-server go run ./hello-server
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"tiltas.openpanel.sh/go"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
srv := &tiltas.Server{} // the name comes from TILTAS_NAME, the key from TILTAS_JOIN_KEY
defer srv.Close() // an ephemeral Device leaves the Network here
upCtx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
if err := srv.Up(upCtx); err != nil {
return err
}
ln, err := srv.Listen("tcp", ":80")
if err != nil {
return err
}
hs := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
who, err := srv.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
http.Error(w, "unknown caller", http.StatusForbidden)
return
}
fmt.Fprintf(w, "hello %s\n", who.Name)
})}
go func() {
<-ctx.Done()
hs.Shutdown(context.Background())
}()
log.Printf("serving on the Network only, as Device %s", srv.DeviceID())
if err := hs.Serve(ln); !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}TILTAS_NAME=hello-server go run ./hello-serverIt prints serving on the Network only, as Device dev_…, and hello-server appears under Devices in the console, as
a Server. Nothing listens on the computer itself: port 80 exists only on the Network.
4. Let the client in
New Servers reach nothing and nobody reaches them. In the console, open Services → New Service:
- Name
hello, kind Web, on the Devicehello-server, targetlocalhost:80; - Who can use it: the Group your client will be in, or the client's Device once it exists.
A Join key with Groups puts every Device that joins with it into those Groups, which suits a client that starts many times. See Services and Access rules.
5. Call it by name
Run the client with a Join key of the Group you allowed, as client:
// Command hello-client calls a Service by name over a Tiltas Network and
// prints the answer.
//
// TILTAS_JOIN_KEY=qlj_… go run ./hello-client http://hello/
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"tiltas.openpanel.sh/go"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
url := "http://hello/"
if len(os.Args) > 1 {
url = os.Args[1]
}
// Stopped or timed out, it still runs Close: the Device leaves the Network.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
srv := &tiltas.Server{}
defer srv.Close()
if err := srv.Up(ctx); err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return err
}
res, err := srv.HTTPClient().Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("%s: %s", url, res.Status)
}
_, err = io.Copy(os.Stdout, res.Body)
return err
}TILTAS_NAME=client go run ./hello-client http://hello/It prints:
hello clientThe client joined, called hello by its Service name, and left. The server learned who called from the Network
itself, not from anything the client sent. Any client language works with any server language.
Next
- Call the server by its Device name,
http://hello-server/, after an Access rule from the client to that Device; or reach a database:Dial(Go) ordial(Python, Node.js) any TCP port by name. - Keep a Device across restarts with the persistent option; see your language's page.
- Read the limitations before you ship.