From 26dc8a3851382ca8e868f9552bd19196100dc6ba Mon Sep 17 00:00:00 2001 From: James O'Halloran Date: Tue, 15 Aug 2023 10:43:37 -0300 Subject: [PATCH] Split out existing site setup --- content/docs/self-hosted/existing-site.md | 146 +++++++++++++++++ content/docs/self-hosted/overview.md | 148 +----------------- .../self-hosted/starters/nextjs-vercel.md | 9 +- content/toc-doc.json | 13 +- 4 files changed, 162 insertions(+), 154 deletions(-) create mode 100644 content/docs/self-hosted/existing-site.md diff --git a/content/docs/self-hosted/existing-site.md b/content/docs/self-hosted/existing-site.md new file mode 100644 index 000000000..a927ea582 --- /dev/null +++ b/content/docs/self-hosted/existing-site.md @@ -0,0 +1,146 @@ +--- +title: Setting up Self-Hosted Backend on an existing project +id: /docs/self-hosted/existing-site +last_edited: '2023-07-07T04:00:00.000Z' +next: /docs/self-hosted/core-concepts/overview +--- + +If you want to self-host the Tina backend, and don't want to use our [pre-configured starter](/docs/self-hosted/starters/nextjs-vercel/), you can follow the steps below. + +We offer a CLI init to quickly setup the backend on NextJS sites, or you can take the manual setup approach if you're using another framework. + +## Using the CLI init command (NextJS Only) + +This will add the files needed to self host TinaCMS to your project. + +TODO + +## Manually configuring the Self-hosted backend + +### 1. Setup TinaCMS on your site + +To setup the TinaCMS admin on your site, follow the [getting started guide](/docs/setup-overview/). (You can skip this step if you already have TinaCMS setup on your site). + +### 2. Choose a Git provider, database adapter, and authentication provider + +You will need to choose a [Git provider](/docs/self-hosted/git-provider/overview/), [database adapter](/docs/self-hosted/database-adapter/overview/), and [authentication provider](/docs/self-hosted/authentication-provider/overview/). You can use any of the providers we have created or you can create your own. In the example below we will use GitHub, VercelKV, and Next Auth. + +### 3. Install the dependencies + +```bash +yarn add tinacms @tinacms/datalayer +``` + +```bash +yarn add --dev @tinacms/cli +``` + +Install any dependencies for your chosen git provider, database adapter, and authentication provider (This may very to depending on what you have chosen) + +```bash +yarn add tinacms-gitprovider-github tinacms-next-auth upstash-redis-level @upstash/redis +``` + +### 4. Create a database file + +Create a file called `database.{js,ts}` in the the `/tina` folder of your project. This file will be used to create the database. + +`tina/database.{ts,js}` + +```ts +import { createDatabase, createLocalDatabase } from '@tinacms/datalayer' + +// Change this to your chosen git provider +import { GitHubProvider } from 'tinacms-gitprovider-github' + +// Change this to your chosen database adapter +import { Redis } from '@upstash/redis' +import { RedisLevel } from 'upstash-redis-level' + +// Manage this flag in your CI/CD pipeline and make sure it is set to false in production +const isLocal = process.env.TINA_PUBLIC_IS_LOCAL === 'true' + +const branch = + process.env.GITHUB_BRANCH || process.env.VERCEL_GIT_COMMIT_REF || 'main' + +if (!branch) { + throw new Error( + 'No branch found. Make sure that you have set the GITHUB_BRANCH or process.env.VERCEL_GIT_COMMIT_REF environment variable.' + ) +} + +export default isLocal + ? // If we are running locally, use a local database that stores data in memory and writes to the locac filesystem on save + createLocalDatabase() + : // If we are not running locally, use a database that stores data in redis and Saves data to github + createDatabase({ + // May very depending on your git provider + gitProvider: new GitHubProvider({ + repo: process.env.GITHUB_REPO || process.env.VERCEL_GIT_REPO_SLUG, + owner: process.env.GITHUB_OWNER || process.env.VERCEL_GIT_REPO_OWNER, + token: process.env.GITHUB_PERSONAL_ACCESS_TOKEN, + branch, + }), + // May very depending on your database adapter + databaseAdapter: new RedisLevel>({ + redis: new Redis({ + url: + (process.env.KV_REST_API_URL as string) || 'http://localhost:8079', + token: (process.env.KV_REST_API_TOKEN as string) || 'example_token', + }), + debug: process.env.DEBUG === 'true' || false, + namespace: branch, + }), + }) +``` + +### 5. Host the GraphQL API + +You will need a [backend endpoint](/docs/self-hosted/graphql-endpoint/overview) that hosts the GraphQL API. + +In this example we will show how to host the GraphQL API on Vercel. You can use any hosting provider you want (May need to addjust the code to suite your chosen framwork) + +```js +// pages/api/graphql.js +import { NextApiHandler } from 'next' +import databaseClient from '../../tina/__generated__/databaseClient' + +const nextApiHandler: NextApiHandler = async (req, res) => { + // Your custom authentication function that returns true if the user is authenticated. + const isAuthenticated = await customAuthFunction({ + token: req.headers.authorization, + }) + if (!isAuthenticated) { + return res.status(401).json({ message: 'Unauthorized' }) + } + const { query, variables } = req.body + const result = await databaseClient.request({ query, variables }) + return res.json(result) +} + +export default nextApiHandler +``` + +> For more info see [GraphQL endpont docs](/docs/self-hosted/graphql-endpoint/overview) + +### 6. Update the TinaCMS config + +Update the TinaCMS config to use the GraphQL API you created in the previous step. + +```js +// tina/config.{js,ts} + +export default defineConfig({ + // Make sure to set this to the url of your GraphQL API + contentApiUrlOverride: '/api/gql', + admin: { + auth: { + // Add your authentication provider's functions here. Please refer to the docs for your chosen authentication provider. + }, + //... + }, + //... +}) +``` + +Now you should be able to run your site and use TinaCMS to edit your content. Pleas see our [core concept docs](/docs/self-hosted/core-concepts/overview/) for more info on how to self host TinaCMS. diff --git a/content/docs/self-hosted/overview.md b/content/docs/self-hosted/overview.md index bd6c3ebe2..0021a818f 100644 --- a/content/docs/self-hosted/overview.md +++ b/content/docs/self-hosted/overview.md @@ -8,154 +8,18 @@ next: /docs/self-hosted/starters/nextjs-vercel By default, TinaCMS uses Tina Cloud as its backend. Tina Cloud is a powerful, out-of-the-box solution that handles reading/writing to your GitHub repository, caching content in a queryable data layer, and authentication / authorization. -For users who want to be independent of Tina Cloud, **we also offer a self-hosted alternative** where you can host your own Tina Data Layer and provide your own user authentication and git integration. We love the control and portability you get when storing content in Markdown and JSON files and this allows users to extend that flexibility to the rest of the CMS. - +For users who want to be independent of Tina Cloud, **we also offer a self-hosted alternative** where you can host your own Tina Data Layer and provide your own user authentication and Gitintegration. ## How does it work? -You create a Tina Data Layer by creating backend functions that host the GraphQL API and you create a Data Layer that indexes content from the filesystem into a databse. This database is used to retrive the data. When an editor updates the data a [git provider](/docs/self-hosted/git-provider/overview/) is used to commit the changes to the filesystem. +You create a Tina Data Layer by creating backend functions that host the GraphQL API and you create a Data Layer that indexes content from the filesystem into a databse. This database is used to retrive the data. When an editor updates the data a [Gitprovider](/docs/self-hosted/git-provider/overview/) is used to commit the changes to the filesystem. ## Getting Started -### Using a Example (Recommended) - -Self hosting TinaCMS can be a bit of a challenge. We have created a [example](/docs/self-hosted/starters/overview/) that you can use to get started. This starter is a Next.js app that uses Vercel for hosting and GitHub for git integration. It also includes Next Auth authentication. - - -### Using the CLI init command - -This will add the files needed to self host TinaCMS to your project. It is recommended to use this command in a next.js project. - -TODO: - -### Manually adding to an existing project - -If you are not using next.js or you want to manually add the files needed to self host TinaCMS to your project, you can follow the steps below. - -#### 1. Setup TinaCMS on your site - -Follow the [getting started guide](/docs/setup-overview/) to setup TinaCMS on your site. (You can skip this step if you already have TinaCMS setup on your site) - -#### 2. Choose a git provider, database adapter, and authentication provider - -You will need to choose a [git provider](/docs/self-hosted/git-provider/overview/), [database adapter](/docs/self-hosted/database-adapter/overview/), and [authentication provider](/docs/self-hosted/authentication-provider/overview/). You can use any of the providers we have created or you can create your own. In the example below we will use GitHub, VercelKV, and Next Auth. - -#### 3. Install the dependencies - -```bash -yarn add tinacms @tinacms/datalayer -``` - -```bash -yarn add --dev @tinacms/cli -``` - -Install any dependencies for your chosen git provider, database adapter, and authentication provider (This may very to depending on what you have choses) - -```bash -yarn add tinacms-gitprovider-github tinacms-next-auth upstash-redis-level @upstash/redis -``` - -#### 4. Create a database file - -Create a file called `database.{js,ts}` in the the tina folder of your project. This file will be used to create the database. - -`tina/database.{ts,js}` - -```ts -import { createDatabase, createLocalDatabase } from "@tinacms/datalayer"; - -// Change this to your chosen git provider -import { GitHubProvider } from "tinacms-gitprovider-github"; - -// Change this to your chosen database adapter -import { Redis } from "@upstash/redis"; -import { RedisLevel } from "upstash-redis-level"; - -// Manage this flag in your CI/CD pipeline and make sure it is set to false in production -const isLocal = process.env.TINA_PUBLIC_IS_LOCAL === "true"; - -const branch = process.env.GITHUB_BRANCH || - process.env.VERCEL_GIT_COMMIT_REF || - "main" - -if (!branch) { - throw new Error( - "No branch found. Make sure that you have set the GITHUB_BRANCH or process.env.VERCEL_GIT_COMMIT_REF environment variable." - ); -} - -export default isLocal -// If we are running locally, use a local database that stores data in memory and writes to the locac filesystem on save - ? createLocalDatabase() - // If we are not running locally, use a database that stores data in redis and Saves data to github - : createDatabase({ - // May very depending on your git provider - gitProvider: new GitHubProvider({ - repo: process.env.GITHUB_REPO || process.env.VERCEL_GIT_REPO_SLUG, - owner: process.env.GITHUB_OWNER || process.env.VERCEL_GIT_REPO_OWNER, - token: process.env.GITHUB_PERSONAL_ACCESS_TOKEN, - branch, - }), - // May very depending on your database adapter - databaseAdapter: new RedisLevel>({ - redis: new Redis({ - url: - (process.env.KV_REST_API_URL as string) || "http://localhost:8079", - token: (process.env.KV_REST_API_TOKEN as string) || "example_token", - }), - debug: process.env.DEBUG === "true" || false, - namespace: branch, - }), - }); -``` - - -#### 5. Host the GraphQL API - -You will need a [backend endpoint](/docs/self-hosted/graphql-endpoint/overview) that hosts the GraphQL API. - -In this example we will show how to host the GraphQL API on Vercel. You can use any hosting provider you want (May need to addjust the code to suite your chosen framwork) - -```js -// pages/api/graphql.js -import { NextApiHandler } from "next"; -import databaseClient from "../../tina/__generated__/databaseClient"; - -const nextApiHandler: NextApiHandler = async (req, res) => { - // Your custom authentication function that returns true if the user is authenticated. - const isAuthenticated = await customAuthFunction({token: req.headers.authorization}); - if (!isAuthenticated) { - return res.status(401).json({ message: "Unauthorized" }); - } - const { query, variables } = req.body; - const result = await databaseClient.request({ query, variables }); - return res.json(result); -}; - -export default nextApiHandler -``` - -> For more info see [GraphQL endpont docs](/docs/self-hosted/graphql-endpoint/overview) - -#### 6. Update the TinaCMS config - -Update the TinaCMS config to use the GraphQL API you created in the previous step. +### Using a Starter -```js -// tina/config.{js,ts} +Self hosting TinaCMS can be a bit of a challenge. We have created a [example](/docs/self-hosted/starters/overview/) that you can use to get started. This starter is a Next.js app that uses Vercel for hosting and GitHub for Gitintegration. It also includes Next Auth authentication. -export default defineConfig({ - // Make sure to set this to the url of your GraphQL API - contentApiUrlOverride: '/api/gql', - admin: { - auth: { - // Add your authentication provider's functions here. Please refer to the docs for your chosen authentication provider. - } - //... - } - //... -}) -``` +### Implementing on an Existing Site -Now you should be able to run your site and use TinaCMS to edit your content. Pleas see our [core concept docs](/docs/self-hosted/core-concepts/overview/) for more info on how to self host TinaCMS. +If you don't want to use our preconfigured starter, we have a guide for setting up the Self-hosted backend on your existing project. Check it out [here](/docs/self-hosted/existing-site/). diff --git a/content/docs/self-hosted/starters/nextjs-vercel.md b/content/docs/self-hosted/starters/nextjs-vercel.md index 686f71b36..a39c66629 100644 --- a/content/docs/self-hosted/starters/nextjs-vercel.md +++ b/content/docs/self-hosted/starters/nextjs-vercel.md @@ -2,6 +2,7 @@ title: Using the Next.js Vercel Example id: /docs/self-hosted/starters/nextjs-vercel last_edited: '2023-07-07T04:00:00.000Z' +next: /docs/self-hosted/existing-site --- ## Introduction @@ -12,15 +13,14 @@ This doc will guide you through setting up our pre-configured self-hosted exampl - [Vercel](/docs/self-hosted/database-adapters/vercel-kv) KV for the [databasae adapter](/docs/self-hosted/database-adapters/overview) - [NextAuth](/docs/self-hosted/authentication-provider/next-auth) for its [authentication provider](/docs/self-hosted/authentication-providers/overview) - ## Deploy The Starter Template - First you can, deploy the [the self-hosted starter](https://github.com/tinacms/tina-self-hosted-demo) using our preconfigured Vercel template: -[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Ftinacms%2Ftina-self-hosted-demo\&env=GITHUB_PERSONAL_ACCESS_TOKEN,GITHUB_BRANCH,NEXTAUTH_SECRET,KV_REST_API_JAMES_REST_API_URL,KV_REST_API_JAMES_REST_API_TOKEN,NEXTAUTH_CREDENTIALS_KEY\&envDescription=See%20the%20self-hosted%20demo%20README%20for%20more%20information\&envLink=https%3A%2F%2Fgithub.com%2Ftinacms%2Ftina-self-hosted-demo%2Fblob%2Fmain%2FREADME.md\&project-name=tina-self-hosted-demo\&repository-name=tina-self-hosted-demo\&stores=%5B%7B%22type%22%3A%22kv%22%7D%5D&) +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Ftinacms%2Ftina-self-hosted-demo&env=GITHUB_PERSONAL_ACCESS_TOKEN,GITHUB_BRANCH,NEXTAUTH_SECRET,KV_REST_API_JAMES_REST_API_URL,KV_REST_API_JAMES_REST_API_TOKEN,NEXTAUTH_CREDENTIALS_KEY&envDescription=See%20the%20self-hosted%20demo%20README%20for%20more%20information&envLink=https%3A%2F%2Fgithub.com%2Ftinacms%2Ftina-self-hosted-demo%2Fblob%2Fmain%2FREADME.md&project-name=tina-self-hosted-demo&repository-name=tina-self-hosted-demo&stores=%5B%7B%22type%22%3A%22kv%22%7D%5D&) This will + - Create a copy of the [self-hosted starter](https://github.com/tinacms/tina-self-hosted-demo) to your github account - Create a new Vercel project with the starter code - Create a new Vercel KV store for your project @@ -37,7 +37,6 @@ You will be prompted to enter values for the following environment variables: GitHub personal access token generated in your [GitHub developer settings](https://github.com/settings/personal-access-tokens/new). Make sure to assign it `repo` access to your new repository with Read/Write access to Contents. - #### `NEXTAUTH_SECRET` Random string used by NextAuth.js for JWT encryption @@ -107,4 +106,4 @@ When you navigate to /admin on your Vercel deployment, or if you run `yarn build yarn setup:users ``` -If the KV\_REST\_API\_URL & KV\_REST\_API\_TOKEN variables are not set, you will be prompted for them the first time the script is executed. Once you have created a user with a password, they will be able to login to your production site, make changes, and have those updates persisted to your live site. +If the KV_REST_API_URL & KV_REST_API_TOKEN variables are not set, you will be prompted for them the first time the script is executed. Once you have created a user with a password, they will be able to login to your production site, make changes, and have those updates persisted to your live site. diff --git a/content/toc-doc.json b/content/toc-doc.json index e9822e158..cfc456a82 100644 --- a/content/toc-doc.json +++ b/content/toc-doc.json @@ -230,13 +230,12 @@ "slug": "/docs/self-hosted/overview/" }, { - "title": "Using a Example (Recommended)", - "items": [ - { - "title": "NextJS + Next Auth + Vercel KV", - "slug": "/docs/self-hosted/starters/nextjs-vercel/" - } - ] + "title": "Self-Hosted Starter", + "slug": "/docs/self-hosted/starters/nextjs-vercel/" + }, + { + "title": "Implementing on an Existing Site", + "slug": "/docs/self-hosted/existing-site/" }, { "title": "Core Concepts",