-
Notifications
You must be signed in to change notification settings - Fork 33
WIP Draft - Example that deploys a golang backend, postgres db and a react frontend where they all talk with each other #83
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
Draft
psankar
wants to merge
4
commits into
unikraft-cloud:main
Choose a base branch
from
psankar:go-postgres-react
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # go-postgres-react | ||
|
|
||
| This is for creating a Contacts application with the following components: | ||
|
|
||
| - [x] a Golang backend | ||
| - [x] a Postgresql database for saving data | ||
| -- the corresponding Volume for persistent storage | ||
| - [] a react frontend app for serving the UI | ||
|
|
||
| ## Create a volume and deploy postgresql | ||
|
|
||
| ```console | ||
| $ cd github.com/kraftcloud/examples/postgres | ||
|
|
||
| github.com/kraftcloud/examples/postgres$ kraft cloud volume create --name postgres --size 200 | ||
|
|
||
| github.com/kraftcloud/examples/postgres$ kraft cloud deploy --metro fra0 -M 1024 -e POSTGRES_PASSWORD=unikraft -e PGDATA=/volume/postgres -v postgres:/volume -p 5432:5432/tls . | ||
| (Search for `service group` in the output) | ||
|
|
||
| # Port-forward postgresql to localhost | ||
| github.com/kraftcloud/examples/postgres$ kraft cloud tunnel <SERVICE_GROUP_FROM_ABOVE> 5432:5432 | ||
|
|
||
|
|
||
| $ psql -U postgres -h localhost | ||
| Password for user postgres: unikraft | ||
| psql (16.2) | ||
| Type "help" for help. | ||
|
|
||
| postgres=# CREATE TABLE contacts (id SERIAL PRIMARY KEY, name TEXT, email TEXT); | ||
| CREATE TABLE | ||
| postgres=# | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ## Deploy the backend: | ||
|
|
||
| ```console | ||
| github.com/kraftcloud/examples/go-postgres-react/go$ kraft cloud deploy --metro fra0 -p 443:8080 . | ||
| (Search for `domain` in the output) | ||
|
|
||
| # Ensure that the backend API works fine by talking to the db | ||
|
|
||
| # Empty response first | ||
| $ curl -vv https://DOMAIN_FROM_ABOVE.fra0.kraft.host/contacts | ||
| null | ||
|
|
||
| # Create contact next | ||
| $ curl -v https://DOMAIN_FROM_ABOVE.fra0.kraft.host/contacts -d '{ "name": "example1", "email": "one@example.com" }' | ||
|
|
||
| # Ensure the new contact is saved and retrieved from db via the backend | ||
| $ curl https://DOMAIN_FROM_ABOVE.fra0.kraft.host/contacts | ||
| [{"name":"example1","email":"one@example.com"}] | ||
|
|
||
| # Above backend API confirms data is saved, but let us check in DB also | ||
| $ psql -U postgres -h localhost | ||
| Password for user postgres: unikraft | ||
| psql (16.2) | ||
| Type "help" for help. | ||
|
|
||
| postgres=# select * from contacts; | ||
| id | name | email | ||
| ----+----------+----------------- | ||
| 1 | example1 | one@example.com | ||
| (1 row) | ||
|
|
||
| postgres=# | ||
|
|
||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| FROM --platform=linux/x86_64 golang:1.22.3-bookworm AS build | ||
|
|
||
| WORKDIR /src | ||
|
|
||
| COPY . /src/ | ||
|
|
||
| RUN set -xe; \ | ||
| go build \ | ||
| -buildmode=pie \ | ||
| -ldflags "-linkmode external -extldflags -static-pie" \ | ||
| -tags netgo \ | ||
| -o /server server.go \ | ||
| ; | ||
|
|
||
| FROM scratch | ||
|
|
||
| COPY --from=build /server /server | ||
| COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/ | ||
| COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| spec: v0.6 | ||
|
|
||
| runtime: base:latest | ||
|
|
||
| rootfs: ./Dockerfile | ||
|
|
||
| cmd: ["/server"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Simple Go 1.21 HTTP Server | ||
|
|
||
| This is a simple HTTP server written in the [Go](https://go.dev/) programming language. | ||
|
|
||
| To run this example on KraftCloud, first [install the `kraft` CLI tool](https://unikraft.org/docs/cli). | ||
| Then clone this examples repository and `cd` into this directory, and invoke: | ||
|
|
||
| ```console | ||
| kraft cloud deploy --metro fra0 -p 443:8080 . | ||
| ``` | ||
|
|
||
| The command will build and deploy the `server.go` source code file. | ||
|
|
||
| After deploying, you can query the service using the provided URL. | ||
|
|
||
| ## Learn more | ||
|
|
||
| - [Go's Documentation](https://go.dev/doc/) | ||
| - [KraftCloud's Documentation](https://docs.kraft.cloud) | ||
| - [Building `Dockerfile` Images with `Buildkit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module github.com/kraftcloud/examples/go-postgres-react/go | ||
|
|
||
| go 1.22.3 | ||
|
|
||
| require github.com/lib/pq v1.10.9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= | ||
| github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||
| package main | ||||||
|
|
||||||
| import ( | ||||||
| "database/sql" | ||||||
| "encoding/json" | ||||||
| "fmt" | ||||||
| "net/http" | ||||||
|
|
||||||
| _ "github.com/lib/pq" | ||||||
| ) | ||||||
|
|
||||||
| type Contact struct { | ||||||
| Name string `json:"name"` | ||||||
| Email string `json:"email"` | ||||||
| } | ||||||
|
|
||||||
| var db *sql.DB | ||||||
|
|
||||||
| func main() { | ||||||
| var err error | ||||||
| db, err = sql.Open( | ||||||
| "postgres", | ||||||
| "user=postgres dbname=postgres password=unikraft host=postgres-wahr0.internal sslmode=disable", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ) | ||||||
| if err != nil { | ||||||
| panic(err) | ||||||
| } | ||||||
|
|
||||||
| http.HandleFunc("/contacts", contactsHandler) | ||||||
|
|
||||||
| fmt.Println("Listening on :8080...") | ||||||
| http.ListenAndServe(":8080", nil) | ||||||
| } | ||||||
|
|
||||||
| func contactsHandler(w http.ResponseWriter, r *http.Request) { | ||||||
| switch r.Method { | ||||||
| case http.MethodGet: | ||||||
| rows, err := db.Query("SELECT name, email FROM contacts") | ||||||
| if err != nil { | ||||||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||
| return | ||||||
| } | ||||||
| defer rows.Close() | ||||||
|
|
||||||
| var contacts []Contact | ||||||
| for rows.Next() { | ||||||
| var c Contact | ||||||
| if err := rows.Scan(&c.Name, &c.Email); err != nil { | ||||||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||
| return | ||||||
| } | ||||||
| contacts = append(contacts, c) | ||||||
| } | ||||||
|
|
||||||
| json.NewEncoder(w).Encode(contacts) | ||||||
| case http.MethodPost: | ||||||
| var c Contact | ||||||
| json.NewDecoder(r.Body).Decode(&c) | ||||||
|
|
||||||
| _, err := db.Exec( | ||||||
| "INSERT INTO contacts(name, email) VALUES($1, $2)", | ||||||
| c.Name, | ||||||
| c.Email, | ||||||
| ) | ||||||
| if err != nil { | ||||||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| json.NewEncoder(w).Encode(c) | ||||||
| default: | ||||||
| http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) | ||||||
| } | ||||||
| } | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.