Skip to content

Commit 8b0cf01

Browse files
committed
Update readme
0 parents  commit 8b0cf01

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed

.github/workflows/deploy.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Golang-Http-Form-Railpack
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-golang-http-form-template%26template_owner%3DDefangSamples)
4+
5+
This Go application demonstrates a simple form submission using the standard net/http library. Users can input their first name into a form, and upon submission, they will be greeted personally by the application.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Configuration
14+
15+
For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).
16+
17+
If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.
18+
19+
```bash
20+
defang config set API_KEY
21+
```
22+
23+
## Deployment
24+
25+
> [!NOTE]
26+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
27+
28+
### Defang Playground
29+
30+
Deploy your application to the Defang Playground by opening up your terminal and typing:
31+
32+
```bash
33+
defang compose up
34+
```
35+
36+
### BYOC (AWS)
37+
38+
If you want to deploy to your own cloud account, you can use Defang BYOC:
39+
40+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
41+
2. Run in a terminal that has access to your AWS environment variables:
42+
```bash
43+
defang --provider=aws compose up
44+
```
45+
46+
---
47+
48+
Title: Go HTTP Form
49+
50+
Short Description: A simple Go application that demonstrates form submission using the net/http library.
51+
52+
Tags: Go, HTTP, Railpack
53+
54+
Languages: golang

app/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module defang.io/sample
2+
3+
go 1.20

app/go.sum

Whitespace-only changes.

app/main.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"html/template"
6+
"log"
7+
"net/http"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
)
12+
13+
var indexTmpl = template.Must(template.New("index").Parse(`
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<title>Simple form post</title>
18+
<style>
19+
body {
20+
font-family: Arial, sans-serif;
21+
margin: 20px;
22+
}
23+
24+
label {
25+
font-weight: bold;
26+
}
27+
28+
input[type="text"] {
29+
padding: 5px;
30+
margin-bottom: 10px;
31+
width: 200px;
32+
}
33+
34+
input[type="submit"] {
35+
padding: 10px 20px;
36+
background-color: #4CAF50;
37+
color: white;
38+
border: none;
39+
cursor: pointer;
40+
}
41+
</style>
42+
</head>
43+
<body>
44+
<h1>Simple form post</h1>
45+
<form action="/submit" method="post">
46+
<label for="first_name">First name:</label><br>
47+
<input type="text" id="first_name" name="first_name"><br>
48+
<input type="submit" value="Submit">
49+
</form>
50+
</body>
51+
</html>
52+
`))
53+
54+
var submitTmpl = template.Must(template.New("submit").Parse(`
55+
<html>
56+
<head>
57+
<title>Simple form post</title>
58+
</head>
59+
<body>
60+
<h1>Hello {{.FirstName}}!</h1>
61+
</body>
62+
</html>
63+
`))
64+
65+
func main() {
66+
http.HandleFunc("/", indexHandler)
67+
http.HandleFunc("/submit", submitHandler)
68+
69+
// Register signal handler for graceful shutdown
70+
sigs := make(chan os.Signal, 1)
71+
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
72+
defer signal.Stop(sigs)
73+
74+
server := &http.Server{Addr: ":8080"}
75+
go func() {
76+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
77+
log.Fatalf("HTTP server Serve: %v\n", err)
78+
}
79+
}()
80+
81+
sig := <-sigs
82+
log.Printf("Received signal %v, shutting down...\n", sig)
83+
log.Fatal(server.Shutdown(context.Background()))
84+
}
85+
86+
func indexHandler(w http.ResponseWriter, r *http.Request) {
87+
w.Header().Set("Content-Type", "text/html")
88+
indexTmpl.Execute(w, nil)
89+
}
90+
91+
func submitHandler(w http.ResponseWriter, r *http.Request) {
92+
if r.Method != http.MethodPost {
93+
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
94+
return
95+
}
96+
97+
w.Header().Set("Content-Type", "text/html")
98+
firstName := r.FormValue("first_name")
99+
submitTmpl.Execute(w, struct{ FirstName string }{firstName})
100+
}

compose.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
app:
3+
restart: unless-stopped
4+
build:
5+
context: ./app
6+
ports:
7+
- mode: ingress
8+
target: 8080
9+
deploy:
10+
resources:
11+
reservations:
12+
memory: 50M
13+
healthcheck:
14+
test: ["CMD", "curl", "-f", "http://localhost:8080/"]

0 commit comments

Comments
 (0)