Skip to content

shared-config: new example #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions shared-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Meeting Notes Example

### Tutorial article: https://dev.to/encore/setting-up-a-free-production-ready-web-app-backend-in-go-with-database-in-less-than-100-lines-4ge4

This is an example application (frontend + backend) for a Markdown Meeting Notes app.

Live demo: [https://encoredev.github.io/meeting-notes/](https://encoredev.github.io/meeting-notes/)

![Frontend](./images/demo.gif)

The backend uses an SQL database to store meeting notes and has three API endpoints:
* `GET /note/:id` - Retrieve a note by ID.
* `POST /note` - Create a new note (or update an existing one).
* `GET /images/:query` - Search for images by using the [Pexels API](https://www.pexels.com/api/).

## Developing locally

When you have [installed Encore](https://encore.dev/docs/install), you can create a new Encore application and clone this example by running this command:

```bash
encore app create my-app --example=meeting-notes
```

## Running

To run the application locally, make sure you have [Docker](https://docker.com) installed and running. This is required to run Encore applications with SQL databases.

```bash
# Run the backend
encore run

# In a different terminal window, run the frontend
cd frontend
npm install
npm run dev
```

### Encore developer dashboard

While `encore run` is running, open [http://localhost:9400/](http://localhost:9400/) to view Encore's local developer dashboard.
Here you can see the request you just made and a view a trace of the response.

## Deployment

### Backend

Deploy your backend to a staging environment in Encore's free development cloud.

```bash
git push encore
```

You can view your backend deploys, metrics and traces at [https://app.encore.dev](https://app.encore.dev).

### Frontend

#### Using GitHub pages

1. Create a repo on GitHub
2. In the `vite.config.js` file, set the `base` property to the name of your repo:
```ts
base: "/example-meeting-notes/",
```
3. Push your code to GitHub and wait for the GitHub actions workflow to finish.
4. Go to *Settings* → *Pages* for your repo on GitHub and set *Branch* to `gh-pages`.

Your site should now be available at `https://<your-github-username>.github.io/<your-repo-name>/`.

Pushing new code to GitHub will automatically update your site (see the GitHub actions workflow in the `.github` folder).

[Read more about GitHub pages here](https://docs.github.com/en/pages/getting-started-with-github-pages/creating-a-github-pages-site).
1 change: 1 addition & 0 deletions shared-config/cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module: "encore.app"
4 changes: 4 additions & 0 deletions shared-config/encore.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "",
"lang": "go"
}
5 changes: 5 additions & 0 deletions shared-config/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module encore.app

go 1.18

require encore.dev v1.44.6
2 changes: 2 additions & 0 deletions shared-config/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
encore.dev v1.44.6 h1:rpwwZxtoQdSC+Oh88GXI7mC1XALgy3YP0vZuRZRxJDQ=
encore.dev v1.44.6/go.mod h1:XdWK6bKKAVzutmOKpC5qzalDQJLNfRCF/YCgA7OUZ3E=
3 changes: 3 additions & 0 deletions shared-config/myservice/config.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "encore.app/sharedconfig:sharedconfig"

Shared: sharedconfig
25 changes: 25 additions & 0 deletions shared-config/myservice/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"context"

"encore.app/sharedconfig"
"encore.dev/config"
)

type Config struct {
Shared sharedconfig.Config
}

var cfg = config.Load[*Config]()

type GetResponse struct {
EnableFoo bool
}

//encore:api public method=GET path=/config
func Get(_ context.Context) (*GetResponse, error) {
return &GetResponse{
EnableFoo: cfg.Shared.EnableFoo,
}, nil
}
33 changes: 33 additions & 0 deletions shared-config/myservice/encore.gen.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Code generated by encore. DO NOT EDIT.
//
// The contents of this file are generated from the structs used in
// conjunction with Encore's `config.Load[T]()` function. This file
// automatically be regenerated if the data types within the struct
// are changed.
//
// For more information about this file, see:
// https://encore.dev/docs/develop/config
package config

// #Meta contains metadata about the running Encore application.
// The values in this struct will be injected by Encore upon deployment and can be
// referenced from other config values for example when configuring a callback URL:
// CallbackURL: "\(#Meta.APIBaseURL)/webhooks.Handle`"
#Meta: {
APIBaseURL: string @tag(APIBaseURL) // The base URL which can be used to call the API of this running application.
Environment: {
Name: string @tag(EnvName) // The name of this environment
Type: "production" | "development" | "ephemeral" | "test" @tag(EnvType) // The type of environment that the application is running in
Cloud: "aws" | "azure" | "gcp" | "encore" | "local" @tag(CloudType) // The cloud provider that the application is running in
}
}

// #Config is the top level configuration for the application and is generated
// from the Go types you've passed into `config.Load[T]()`. Encore uses a definition
// of this struct which is closed, such that the CUE tooling can any typos of field names.
// this definition is then immediately inlined, so any fields within it are expected
// as fields at the package level.
#Config: {
Shared: EnableFoo: bool
}
#Config
17 changes: 17 additions & 0 deletions shared-config/myservice/encore.gen.go

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

3 changes: 3 additions & 0 deletions shared-config/sharedconfig/sharedconfig.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package sharedconfig

EnableFoo: true
5 changes: 5 additions & 0 deletions shared-config/sharedconfig/sharedconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sharedconfig

type Config struct {
EnableFoo bool
}