# Cog webserver end-to-end

We used some pieces of a webserver cog on the last page just to exemplify how JSON works. As a reminder, this was the summary:

> For an example of providing a JSON response from a cog to a web client, we'll pull some lines from `sire/demo_runcog_site.sire`, an example app that allows a web client to start and stop toy timer cogs as well as check the current status of any cogs in the app.

We're going to explain this entire file now, because it has everything you need to build a useful full-stack web app.

{% hint style="warning" %}
Coming soon: This page will thoroughly cover every line of the cog under examination.

Until this page is finished, we'll leave a few terse hints here for the intrepid learner to get started. It's a great final challenge.
{% endhint %}

{% hint style="info" %}
Define the shape of our kernel and threads
{% endhint %}

```sire
# record KernelState
| KERNEL_STATE
* cogStatus  : Tab Nat CogStatus
* servThread : ThreadId
* files      : (HMap Str (ContentType, Pin Bar))

> ThreadId > KernelState
= (newState servThread)
| KERNEL_STATE
* emptyTab
* servThread
* hmEmpty largeConfig
```

{% hint style="info" %}
Set up a fileserver binding
{% endhint %}

```sire
= (fileServer (PIN st) [method path headers (PIN body)])
# switch method
* _ | NONE
* GET
  | **fmapMaybe | hmLookup (barNat path) (**getFiles st)
  & [type (PIN content)]
  @ head | [(b#{content-type}, type)]
  [200 b#gotcha head content]
```

{% hint style="info" %}
Set up a JSON request handler binding
{% endhint %}

```sire
> Ref KernelState > HttpReq > Cog ()
= (handleReq vSt request return)
;| trk [%req request]
@ [rid method path headers pBody@(PIN body)] request
# switch method
* POST
  # switch path
  * b#{/spin}
  ; <etc> ;
```

{% hint style="info" %}
Start a cog with with the kernel we defined and some threads for the various HTTP handlers
{% endhint %}

\`\`\`sire > Ref KernelState > Cog Void = (runHttpServer vSt return) : ??(rhs\_heard req) < syscall HTTP\_HEAR : \_ < handleReq vSt req | runHttpServer vSt return

> Cog () = (launchKernel return) : servThread < fork (syscall (\*\*HTTP\_SERV emptyFileServer)) : vSt < newRef (PIN | newState servThread) : httpThread1 < fork (runHttpServer vSt) | return ()

main=(runCog launchKernel)

```
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://opfn.gitbook.io/pallas/quickstart-tutorial/cog-web-end-to-end.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
