Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc8cce3

Browse files
committed
chore: work on template to test cors
1 parent 5ba7b82 commit bc8cce3

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

examples/cors_demo/README.md

Whitespace-only changes.

examples/cors_demo/apps.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "coder_app" "code-server" {
2+
agent_id = coder_agent.main.id
3+
slug = "code-server"
4+
display_name = "VS Code"
5+
icon = "${data.coder_workspace.me.access_url}/icon/code.svg"
6+
url = "http://localhost:13337"
7+
share = "owner"
8+
subdomain = false
9+
open_in = "window"
10+
healthcheck {
11+
url = "http://localhost:13337/healthz"
12+
interval = 5
13+
threshold = 6
14+
}
15+
}

examples/cors_demo/main.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"html/template"
7+
"log"
8+
"net/http"
9+
)
10+
11+
var enableCORS bool
12+
var port int
13+
14+
func main() {
15+
flag.IntVar(&port, "port", 7600, "Port to run the server on")
16+
flag.BoolVar(&enableCORS, "cors", false, "Enable CORS headers")
17+
flag.Parse()
18+
19+
http.HandleFunc("/", serveHTML)
20+
http.HandleFunc("/test", testHandler)
21+
22+
fmt.Printf("Server started at :%d\n", port)
23+
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
24+
}
25+
26+
func serveHTML(w http.ResponseWriter, r *http.Request) {
27+
if enableCORS {
28+
w.Header().Set("Access-Control-Allow-Origin", "*")
29+
}
30+
tmpl := `
31+
<!DOCTYPE html>
32+
<html>
33+
<head>
34+
<title>Fetch Test</title>
35+
</head>
36+
<body>
37+
<h1>Fetch Tester</h1>
38+
<input type="text" id="url" placeholder="Enter URL" size="50" value="https://enable-cors.org/" />
39+
<select id="method">
40+
<option>GET</option>
41+
<option>POST</option>
42+
<option>PUT</option>
43+
<option>DELETE</option>
44+
</select>
45+
<button onclick="sendRequest()">Send Request</button>
46+
<ul id="log"></ul>
47+
48+
<script>
49+
function sendRequest() {
50+
const url = document.getElementById('url').value;
51+
const method = document.getElementById('method').value;
52+
fetch(url, { method: method })
53+
.then(response => {
54+
const log = document.getElementById('log');
55+
const li = document.createElement('li');
56+
li.textContent = method + " " + url + ": " + response.status;
57+
log.appendChild(li);
58+
})
59+
.catch(error => {
60+
const log = document.getElementById('log');
61+
const li = document.createElement('li');
62+
li.textContent = '(check network log for details) Error: ' + error;
63+
log.appendChild(li);
64+
});
65+
}
66+
</script>
67+
</body>
68+
</html>
69+
`
70+
t := template.Must(template.New("page").Parse(tmpl))
71+
t.Execute(w, nil)
72+
}
73+
74+
func testHandler(w http.ResponseWriter, r *http.Request) {
75+
if enableCORS {
76+
w.Header().Set("Access-Control-Allow-Origin", "*")
77+
}
78+
fmt.Fprintf(w, "You made a %s request to /test", r.Method)
79+
}

examples/cors_demo/main.tf

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
}
6+
docker = {
7+
source = "kreuzwerker/docker"
8+
}
9+
}
10+
}
11+
12+
locals {
13+
username = data.coder_workspace_owner.me.name
14+
}
15+
16+
variable "docker_socket" {
17+
default = ""
18+
description = "(Optional) Docker socket URI"
19+
type = string
20+
}
21+
22+
provider "docker" {
23+
# Defaulting to null if the variable is an empty string lets us have an optional variable without having to set our own default
24+
host = var.docker_socket != "" ? var.docker_socket : null
25+
}
26+
27+
data "coder_provisioner" "me" {}
28+
data "coder_workspace" "me" {}
29+
data "coder_workspace_owner" "me" {}
30+
31+
resource "coder_agent" "main" {
32+
arch = data.coder_provisioner.me.arch
33+
os = "linux"
34+
startup_script = <<-EOT
35+
set -e
36+
37+
# Prepare user home with default files on first start.
38+
if [ ! -f ~/.init_done ]; then
39+
cp -rT /etc/skel ~
40+
touch ~/.init_done
41+
fi
42+
43+
# Add any commands that should be executed at workspace startup (e.g install requirements, start a program, etc) here
44+
EOT
45+
46+
# These environment variables allow you to make Git commits right away after creating a
47+
# workspace. Note that they take precedence over configuration defined in ~/.gitconfig!
48+
# You can remove this block if you'd prefer to configure Git manually or using
49+
# dotfiles. (see docs/dotfiles.md)
50+
env = {
51+
GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
52+
GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}"
53+
GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
54+
GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
55+
}
56+
57+
# The following metadata blocks are optional. They are used to display
58+
# information about your workspace in the dashboard. You can remove them
59+
# if you don't want to display any information.
60+
# For basic resources, you can use the `coder stat` command.
61+
# If you need more control, you can write your own script.
62+
metadata {
63+
display_name = "CPU Usage"
64+
key = "0_cpu_usage"
65+
script = "coder stat cpu"
66+
interval = 10
67+
timeout = 1
68+
}
69+
70+
metadata {
71+
display_name = "RAM Usage"
72+
key = "1_ram_usage"
73+
script = "coder stat mem"
74+
interval = 10
75+
timeout = 1
76+
}
77+
78+
metadata {
79+
display_name = "Home Disk"
80+
key = "3_home_disk"
81+
script = "coder stat disk --path $${HOME}"
82+
interval = 60
83+
timeout = 1
84+
}
85+
86+
metadata {
87+
display_name = "CPU Usage (Host)"
88+
key = "4_cpu_usage_host"
89+
script = "coder stat cpu --host"
90+
interval = 10
91+
timeout = 1
92+
}
93+
94+
metadata {
95+
display_name = "Memory Usage (Host)"
96+
key = "5_mem_usage_host"
97+
script = "coder stat mem --host"
98+
interval = 10
99+
timeout = 1
100+
}
101+
102+
metadata {
103+
display_name = "Load Average (Host)"
104+
key = "6_load_host"
105+
# get load avg scaled by number of cores
106+
script = <<EOT
107+
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
108+
EOT
109+
interval = 60
110+
timeout = 1
111+
}
112+
113+
metadata {
114+
display_name = "Swap Usage (Host)"
115+
key = "7_swap_host"
116+
script = <<EOT
117+
free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }'
118+
EOT
119+
interval = 10
120+
timeout = 1
121+
}
122+
}
123+
124+
# See https://registry.coder.com/modules/code-server
125+
module "code-server" {
126+
count = data.coder_workspace.me.start_count
127+
source = "registry.coder.com/modules/code-server/coder"
128+
129+
# This ensures that the latest version of the module gets downloaded, you can also pin the module version to prevent breaking changes in production.
130+
version = ">= 1.0.0"
131+
132+
agent_id = coder_agent.main.id
133+
order = 1
134+
}
135+
136+
resource "docker_volume" "home_volume" {
137+
name = "coder-${data.coder_workspace.me.id}-home"
138+
# Protect the volume from being deleted due to changes in attributes.
139+
lifecycle {
140+
ignore_changes = all
141+
}
142+
# Add labels in Docker to keep track of orphan resources.
143+
labels {
144+
label = "coder.owner"
145+
value = data.coder_workspace_owner.me.name
146+
}
147+
labels {
148+
label = "coder.owner_id"
149+
value = data.coder_workspace_owner.me.id
150+
}
151+
labels {
152+
label = "coder.workspace_id"
153+
value = data.coder_workspace.me.id
154+
}
155+
# This field becomes outdated if the workspace is renamed but can
156+
# be useful for debugging or cleaning out dangling volumes.
157+
labels {
158+
label = "coder.workspace_name_at_creation"
159+
value = data.coder_workspace.me.name
160+
}
161+
}
162+
163+
resource "docker_container" "workspace" {
164+
count = data.coder_workspace.me.start_count
165+
image = "codercom/enterprise-base:ubuntu"
166+
# Uses lower() to avoid Docker restriction on container names.
167+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
168+
# Hostname makes the shell more user friendly: coder@my-workspace:~$
169+
hostname = data.coder_workspace.me.name
170+
# Use the docker gateway if the access URL is 127.0.0.1
171+
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
172+
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
173+
host {
174+
host = "host.docker.internal"
175+
ip = "host-gateway"
176+
}
177+
volumes {
178+
container_path = "/home/coder"
179+
volume_name = docker_volume.home_volume.name
180+
read_only = false
181+
}
182+
183+
# Add labels in Docker to keep track of orphan resources.
184+
labels {
185+
label = "coder.owner"
186+
value = data.coder_workspace_owner.me.name
187+
}
188+
labels {
189+
label = "coder.owner_id"
190+
value = data.coder_workspace_owner.me.id
191+
}
192+
labels {
193+
label = "coder.workspace_id"
194+
value = data.coder_workspace.me.id
195+
}
196+
labels {
197+
label = "coder.workspace_name"
198+
value = data.coder_workspace.me.name
199+
}
200+
}

0 commit comments

Comments
 (0)