Skip to content

Commit

Permalink
Remove pnpm; you get to install tailwind yourself!
Browse files Browse the repository at this point in the history
  • Loading branch information
foodelevator committed Aug 19, 2024
1 parent 8534e7c commit 2c3a194
Show file tree
Hide file tree
Showing 12 changed files with 1,093 additions and 918 deletions.
23 changes: 0 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ARG GO_VERSION=1.22
ARG NODE_VERSION=22.2
ARG ALPINE_VERSION=3.20

FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build-server
Expand All @@ -20,25 +19,6 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
CGO_ENABLED=0 go build -o /bin/server ./cmd/web

FROM node:${NODE_VERSION}-alpine${ALPINE_VERSION} AS npm-packages

WORKDIR /src

RUN corepack enable

COPY package.json pnpm-lock.yaml ./

RUN pnpm i

FROM npm-packages AS build-tailwind

COPY tailwind.config.js style.css ./
COPY pkg pkg
COPY services services
COPY islands islands

RUN pnpm tailwind

FROM alpine:${ALPINE_VERSION}

ARG UID=10001
Expand All @@ -47,8 +27,5 @@ RUN adduser --disabled-password --gecos "" --home /nonexistent --shell "/sbin/no
USER user

COPY --from=build-server /bin/server /bin/
COPY --from=build-tailwind /src/dist/style.css /dist/style.css

ENV DIST_DIR=/dist

ENTRYPOINT ["/bin/server"]
56 changes: 24 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
## Repository structure

This repository contains two main parts: a Go web server and some solid
components written in typescript. The web server in Go does most of the job, it
has a few API:s returning JSON but mostly server-rendered routes that the
browser interacts with directly (so it's not just a backend). Some parts of the
page however require a bit more interactivity; these are made with
[solid](https://www.solidjs.com/) (which is like react but less cringe™). This
is sometimes called _interactive islands_.

### Solid

These are build with [rollup](https://rollupjs.org/) for bundling,
[babel](https://babeljs.io/) for building towers or whatever (the solid
compiler is a babel plugin), typescript (to not loose sanity) and solid
(through babel-preset-solid). They are configured with `rollup.config.mjs` and
`tsconfig.json`.

For each file ending with `.island.tsx` in the `islands/` directory a
corresponding file ending in `.island.js` will be placed in `dist/` which the
Go server will serve at `/dist`. These can be loaded using a script tag with
`type="module"`.

Also note that `pnpm` is used, not `npm`. You can install `pnpm` by enabling
[corepack](https://nodejs.org/api/corepack.html).

## Routes

```sh
Expand All @@ -36,17 +10,30 @@ grep -r 'http.Handle(' --no-filename services/ | sed 's/\s\+//'

Download a go compiler, at least version 1.22

Download the correct version of [templ](https://templ.guide/) using:
If you will modify any `.sql` files, download [sqlc](https://sqlc.dev/). It's
probably best to get the latest version, using:
```sh
go install github.com/a-h/templ/cmd/templ@$(grep -oPm1 'github.com/a-h/templ \K[^ ]*' go.sum)
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
```

Download [sqlc](https://sqlc.dev/). It's probably best to get the latest version, using:
But to see which version was last used, look at the top of any generated file, e.g. `pkg/database/models.go`.

If you will modify `style.css` or any `.templ` files you need to download [templ](https://templ.guide/) and [tailwind](https://tailwindcss.com/).

Download the correct version of templ using:
```sh
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
go install github.com/a-h/templ/cmd/templ@$(grep -oPm1 'github.com/a-h/templ \K[^ ]*' go.sum)
```

But to see which version was last used, look at the top of any generated file, e.g. `pkg/database/models.go`.
(It should be the same version as is imported by the then generated go code)

Download tailwind, using your favourite or second-favourite package manager or:
```sh
TAILWIND_VERSION=$(head -n2 services/static/public/style.dist.css | grep -oP 'v\K\S+') # or use latest, it probably won't hurt.
curl -Lo tailwindcss https://github.com/tailwindlabs/tailwindcss/releases/download/v$TAILWIND_VERSION/tailwindcss-linux-x64
chmod +x tailwindcss
```
and then move it to a directory in your `$PATH` or set `$TAILWIND_PATH` to it's location.

### Database

Expand All @@ -67,12 +54,17 @@ Copy `example.env` to `.env` and change anything if needed.
The best way (objectively, of course) to load them is by installing
[direnv](https://direnv.net/).

If you don't want to do that, you'll have to figure it out on your own. The
application will not load the file `.env`.

### Running with automatic recompiling & rerunning

```sh
go run ./cmd/dev
```

This also updates generated files and they're tracked by the git repository so you better.

### Mocking an OIDC provider

Only needed if you want to test the "Login with KTH" button.
Expand Down Expand Up @@ -157,4 +149,4 @@ since the user was (indirectly) redirected from KTH. Therefore they're set to
## Database schema

The schema is defined by the migrations in `./pkg/database/migrations/`. A new
one can be created using `go run ./cmd/manage goose create $NAME sql`.
one can be created using `go run ./cmd/manage goose create some_fancy_name sql`.
8 changes: 6 additions & 2 deletions cmd/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

tailwind := "tailwindcss"
if t, ok := os.LookupEnv("TAILWIND_PATH"); ok {
tailwind = t
}
for _, cmd := range [][]string{
{"pnpm", "tailwind", "--watch"},
{tailwind, "-i", "style.css", "-o", "./services/static/public/style.dist.css"},
} {
go run(ctx, cmd)
}
Expand Down Expand Up @@ -105,7 +109,7 @@ func main() {
switch ending {
case "templ", "sql":
run(ctx, []string{"go", "generate", "./..."})
case "go":
case "go", "css":
restartMain <- struct{}{}
}
case err, ok := <-watcher.Errors:
Expand Down
1 change: 0 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ DEV=true
PORT=7000
DATABASE_URL=postgresql://logout:logout@localhost:5432/logout
# LDAP_PROXY_URL=
DIST_DIR=./dist
# PLS_URL=https://pls.datasektionen.se
14 changes: 0 additions & 14 deletions package.json

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Cfg struct {
DatabaseURL *url.URL
Dev bool
LDAPProxyURL *url.URL
DistDir string
OIDCProviderIssuerURL *url.URL
OIDCProviderKey string
PlsURL *url.URL
Expand All @@ -35,7 +34,6 @@ func init() {
DatabaseURL: getURL("DATABASE_URL", false),
Dev: os.Getenv("DEV") == "true",
LDAPProxyURL: getURL("LDAP_PROXY_URL", true),
DistDir: os.Getenv("DIST_DIR"),
OIDCProviderIssuerURL: getURL("OIDC_PROVIDER_ISSUER_URL", false),
OIDCProviderKey: os.Getenv("OIDC_PROVIDER_KEY"),
PlsURL: getURL("PLS_URL", true),
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/layout.templ
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ templ Page() {
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Logout</title>
<link rel="stylesheet" href="/dist/style.css"/>
<link rel="stylesheet" href="/public/style.dist.css"/>
<script src="https://unpkg.com/htmx.org@2.0.1" integrity="sha384-QWGpdj554B4ETpJJC9z+ZHJcA/i59TyjxEPXiiUgN2WmTyV5OEZWCD6gQhgkdpB/" crossorigin="anonymous"></script>
<script src="https://unpkg.com/htmx-ext-sse@2.2.2/sse.js" integrity="sha384-fw+eTlCc7suMV/1w/7fr2/PmwElUIt5i82bi+qTiLXvjRXZ2/FkiTNA/w0MhXnGI" crossorigin="anonymous"></script>
<script src="/public/hx-clone.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/layout_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2c3a194

Please sign in to comment.