Pull out index.html
This commit is contained in:
+2
-2
@@ -5,7 +5,7 @@ WORKDIR /build
|
|||||||
COPY go.mod ./
|
COPY go.mod ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
COPY main.go ./
|
COPY index.html.tmpl main.go ./
|
||||||
|
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o beam .
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o beam .
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ RUN adduser -D -u 1000 beam
|
|||||||
USER beam
|
USER beam
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY --from=builder /build/beam .
|
COPY --from=builder /build/index.html.tmpl /build/beam .
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
|
|||||||
+197
@@ -0,0 +1,197 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Beam - File Upload Service</title>
|
||||||
|
<style>
|
||||||
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||||
|
body {
|
||||||
|
font-family: monospace;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 40px auto;
|
||||||
|
padding: 20px;
|
||||||
|
line-height: 1.6;
|
||||||
|
}
|
||||||
|
h1 { margin-bottom: 30px; font-size: 2em; }
|
||||||
|
h2 { margin: 30px 0 15px 0; font-size: 1.3em; }
|
||||||
|
.section {
|
||||||
|
background: #f5f5f5;
|
||||||
|
padding: 20px;
|
||||||
|
margin: 20px 0;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
code {
|
||||||
|
background: #333;
|
||||||
|
color: #0f0;
|
||||||
|
padding: 15px;
|
||||||
|
display: block;
|
||||||
|
margin: 10px 0;
|
||||||
|
border-radius: 4px;
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
.upload-area {
|
||||||
|
border: 3px dashed #ccc;
|
||||||
|
padding: 40px;
|
||||||
|
text-align: center;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s;
|
||||||
|
}
|
||||||
|
.upload-area.dragover {
|
||||||
|
border-color: #333;
|
||||||
|
background: #f0f0f0;
|
||||||
|
}
|
||||||
|
.upload-area:hover {
|
||||||
|
border-color: #666;
|
||||||
|
}
|
||||||
|
input[type="file"] { display: none; }
|
||||||
|
button {
|
||||||
|
background: #333;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
padding: 10px 20px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 1em;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
button:hover { background: #555; }
|
||||||
|
.progress-container {
|
||||||
|
width: 100%%;
|
||||||
|
background: #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin: 10px 0;
|
||||||
|
height: 20px;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
.progress-bar {
|
||||||
|
height: 100%%;
|
||||||
|
background: #333;
|
||||||
|
border-radius: 4px;
|
||||||
|
width: 0;
|
||||||
|
transition: width 0.3s;
|
||||||
|
}
|
||||||
|
.results {
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
.result-item {
|
||||||
|
background: white;
|
||||||
|
padding: 10px;
|
||||||
|
margin: 5px 0;
|
||||||
|
border-radius: 4px;
|
||||||
|
word-break: break-all;
|
||||||
|
}
|
||||||
|
.result-item a {
|
||||||
|
color: #0066cc;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>🚀 Beam - File Upload Service</h1>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h2>Upload with curl</h2>
|
||||||
|
<code>curl --upload-file myfile.txt {{ .Proto }}://{{ .Host }}/myfile.txt</code>
|
||||||
|
<p style="margin-top: 10px;">The server will respond with a download URL.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h2>Download a file</h2>
|
||||||
|
<code>curl {{ .Proto }}://{{ .Host }}/x123abcx/myfile.txt -o myfile.txt</code>
|
||||||
|
<p style="margin-top: 10px;">Or simply open the URL in your browser.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h2>Web Upload</h2>
|
||||||
|
<div class="upload-area" id="uploadArea">
|
||||||
|
<p>📁 Click to select files or drag and drop here</p>
|
||||||
|
<input type="file" id="fileInput" multiple>
|
||||||
|
<button onclick="document.getElementById('fileInput').click()">Select Files</button>
|
||||||
|
</div>
|
||||||
|
<div class="progress-container" id="progressContainer">
|
||||||
|
<div class="progress-bar" id="progressBar"></div>
|
||||||
|
</div>
|
||||||
|
<div class="results" id="results"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const uploadArea = document.getElementById('uploadArea');
|
||||||
|
const fileInput = document.getElementById('fileInput');
|
||||||
|
const progressContainer = document.getElementById('progressContainer');
|
||||||
|
const progressBar = document.getElementById('progressBar');
|
||||||
|
const results = document.getElementById('results');
|
||||||
|
|
||||||
|
uploadArea.addEventListener('click', () => fileInput.click());
|
||||||
|
|
||||||
|
uploadArea.addEventListener('dragover', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
uploadArea.classList.add('dragover');
|
||||||
|
});
|
||||||
|
|
||||||
|
uploadArea.addEventListener('dragleave', () => {
|
||||||
|
uploadArea.classList.remove('dragover');
|
||||||
|
});
|
||||||
|
|
||||||
|
uploadArea.addEventListener('drop', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
uploadArea.classList.remove('dragover');
|
||||||
|
const files = e.dataTransfer.files;
|
||||||
|
uploadFiles(files);
|
||||||
|
});
|
||||||
|
|
||||||
|
fileInput.addEventListener('change', (e) => {
|
||||||
|
uploadFiles(e.target.files);
|
||||||
|
});
|
||||||
|
|
||||||
|
function uploadFiles(files) {
|
||||||
|
if (files.length === 0) return;
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
for (let file of files) {
|
||||||
|
formData.append('files', file);
|
||||||
|
}
|
||||||
|
|
||||||
|
progressContainer.style.display = 'block';
|
||||||
|
progressBar.style.width = '0%%';
|
||||||
|
results.innerHTML = '';
|
||||||
|
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
|
||||||
|
xhr.upload.addEventListener('progress', (e) => {
|
||||||
|
if (e.lengthComputable) {
|
||||||
|
const percent = (e.loaded / e.total) * 100;
|
||||||
|
progressBar.style.width = percent + '%%';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
xhr.addEventListener('load', () => {
|
||||||
|
if (xhr.status === 200) {
|
||||||
|
const response = JSON.parse(xhr.responseText);
|
||||||
|
displayResults(response.urls);
|
||||||
|
} else {
|
||||||
|
results.innerHTML = '<div class="result-item">Upload failed: ' + xhr.statusText + '</div>';
|
||||||
|
}
|
||||||
|
progressBar.style.width = '100%%';
|
||||||
|
});
|
||||||
|
|
||||||
|
xhr.addEventListener('error', () => {
|
||||||
|
results.innerHTML = '<div class="result-item">Upload failed: Network error</div>';
|
||||||
|
});
|
||||||
|
|
||||||
|
xhr.open('POST', '/upload');
|
||||||
|
xhr.send(formData);
|
||||||
|
}
|
||||||
|
|
||||||
|
function displayResults(urls) {
|
||||||
|
results.innerHTML = '<h3>Download URLs:</h3>';
|
||||||
|
urls.forEach(url => {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.className = 'result-item';
|
||||||
|
div.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>';
|
||||||
|
results.appendChild(div);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
@@ -11,13 +11,14 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"html/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
var uploadDir string
|
var uploadDir string
|
||||||
|
|
||||||
const idLength = 8
|
const idLength = 8
|
||||||
const idCharset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
const idCharset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
var indexHTML *template.Template
|
||||||
func main() {
|
func main() {
|
||||||
uploadDir = os.Getenv("UPLOAD_DIR")
|
uploadDir = os.Getenv("UPLOAD_DIR")
|
||||||
if uploadDir == "" {
|
if uploadDir == "" {
|
||||||
@@ -32,7 +33,7 @@ func main() {
|
|||||||
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
||||||
log.Fatalf("Failed to create upload directory: %v", err)
|
log.Fatalf("Failed to create upload directory: %v", err)
|
||||||
}
|
}
|
||||||
|
indexHTML = template.Must(template.ParseFiles("index.html.tmpl"))
|
||||||
http.HandleFunc("/", handleRequest)
|
http.HandleFunc("/", handleRequest)
|
||||||
|
|
||||||
log.Printf("Starting server on port %s, upload directory: %s", port, uploadDir)
|
log.Printf("Starting server on port %s, upload directory: %s", port, uploadDir)
|
||||||
@@ -49,6 +50,9 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
logRequest(r, 200, time.Since(start))
|
logRequest(r, 200, time.Since(start))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if r.Method == "GET" && r.URL.Path == "/healthz" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if r.Method == "POST" && r.URL.Path == "/upload" {
|
if r.Method == "POST" && r.URL.Path == "/upload" {
|
||||||
handleMultipartUpload(w, r, start)
|
handleMultipartUpload(w, r, start)
|
||||||
@@ -327,204 +331,8 @@ func logRequest(r *http.Request, status int, duration time.Duration) {
|
|||||||
func serveFrontpage(w http.ResponseWriter, r *http.Request) {
|
func serveFrontpage(w http.ResponseWriter, r *http.Request) {
|
||||||
proto := getProtocol(r)
|
proto := getProtocol(r)
|
||||||
host := getHost(r)
|
host := getHost(r)
|
||||||
html := fmt.Sprintf(`<!DOCTYPE html>
|
pageData := map[string]string {"Proto": proto, "Host":host}
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Beam - File Upload Service</title>
|
|
||||||
<style>
|
|
||||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
||||||
body {
|
|
||||||
font-family: monospace;
|
|
||||||
max-width: 800px;
|
|
||||||
margin: 40px auto;
|
|
||||||
padding: 20px;
|
|
||||||
line-height: 1.6;
|
|
||||||
}
|
|
||||||
h1 { margin-bottom: 30px; font-size: 2em; }
|
|
||||||
h2 { margin: 30px 0 15px 0; font-size: 1.3em; }
|
|
||||||
.section {
|
|
||||||
background: #f5f5f5;
|
|
||||||
padding: 20px;
|
|
||||||
margin: 20px 0;
|
|
||||||
border-radius: 4px;
|
|
||||||
}
|
|
||||||
code {
|
|
||||||
background: #333;
|
|
||||||
color: #0f0;
|
|
||||||
padding: 15px;
|
|
||||||
display: block;
|
|
||||||
margin: 10px 0;
|
|
||||||
border-radius: 4px;
|
|
||||||
overflow-x: auto;
|
|
||||||
}
|
|
||||||
.upload-area {
|
|
||||||
border: 3px dashed #ccc;
|
|
||||||
padding: 40px;
|
|
||||||
text-align: center;
|
|
||||||
cursor: pointer;
|
|
||||||
transition: all 0.3s;
|
|
||||||
}
|
|
||||||
.upload-area.dragover {
|
|
||||||
border-color: #333;
|
|
||||||
background: #f0f0f0;
|
|
||||||
}
|
|
||||||
.upload-area:hover {
|
|
||||||
border-color: #666;
|
|
||||||
}
|
|
||||||
input[type="file"] { display: none; }
|
|
||||||
button {
|
|
||||||
background: #333;
|
|
||||||
color: white;
|
|
||||||
border: none;
|
|
||||||
padding: 10px 20px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-family: monospace;
|
|
||||||
font-size: 1em;
|
|
||||||
border-radius: 4px;
|
|
||||||
margin-top: 10px;
|
|
||||||
}
|
|
||||||
button:hover { background: #555; }
|
|
||||||
.progress-container {
|
|
||||||
width: 100%%;
|
|
||||||
background: #ddd;
|
|
||||||
border-radius: 4px;
|
|
||||||
margin: 10px 0;
|
|
||||||
height: 20px;
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
.progress-bar {
|
|
||||||
height: 100%%;
|
|
||||||
background: #333;
|
|
||||||
border-radius: 4px;
|
|
||||||
width: 0;
|
|
||||||
transition: width 0.3s;
|
|
||||||
}
|
|
||||||
.results {
|
|
||||||
margin-top: 20px;
|
|
||||||
}
|
|
||||||
.result-item {
|
|
||||||
background: white;
|
|
||||||
padding: 10px;
|
|
||||||
margin: 5px 0;
|
|
||||||
border-radius: 4px;
|
|
||||||
word-break: break-all;
|
|
||||||
}
|
|
||||||
.result-item a {
|
|
||||||
color: #0066cc;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>🚀 Beam - File Upload Service</h1>
|
|
||||||
|
|
||||||
<div class="section">
|
|
||||||
<h2>Upload with curl (Recommended)</h2>
|
|
||||||
<code>curl --upload-file myfile.txt %s://%s/myfile.txt</code>
|
|
||||||
<p style="margin-top: 10px;">The server will respond with a download URL.</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="section">
|
|
||||||
<h2>Download a file</h2>
|
|
||||||
<code>curl %s://%s/xxxxxxxx/myfile.txt -o myfile.txt</code>
|
|
||||||
<p style="margin-top: 10px;">Or simply open the URL in your browser.</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="section">
|
|
||||||
<h2>Web Upload</h2>
|
|
||||||
<div class="upload-area" id="uploadArea">
|
|
||||||
<p>📁 Click to select files or drag and drop here</p>
|
|
||||||
<input type="file" id="fileInput" multiple>
|
|
||||||
<button onclick="document.getElementById('fileInput').click()">Select Files</button>
|
|
||||||
</div>
|
|
||||||
<div class="progress-container" id="progressContainer">
|
|
||||||
<div class="progress-bar" id="progressBar"></div>
|
|
||||||
</div>
|
|
||||||
<div class="results" id="results"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
const uploadArea = document.getElementById('uploadArea');
|
|
||||||
const fileInput = document.getElementById('fileInput');
|
|
||||||
const progressContainer = document.getElementById('progressContainer');
|
|
||||||
const progressBar = document.getElementById('progressBar');
|
|
||||||
const results = document.getElementById('results');
|
|
||||||
|
|
||||||
uploadArea.addEventListener('click', () => fileInput.click());
|
|
||||||
|
|
||||||
uploadArea.addEventListener('dragover', (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
uploadArea.classList.add('dragover');
|
|
||||||
});
|
|
||||||
|
|
||||||
uploadArea.addEventListener('dragleave', () => {
|
|
||||||
uploadArea.classList.remove('dragover');
|
|
||||||
});
|
|
||||||
|
|
||||||
uploadArea.addEventListener('drop', (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
uploadArea.classList.remove('dragover');
|
|
||||||
const files = e.dataTransfer.files;
|
|
||||||
uploadFiles(files);
|
|
||||||
});
|
|
||||||
|
|
||||||
fileInput.addEventListener('change', (e) => {
|
|
||||||
uploadFiles(e.target.files);
|
|
||||||
});
|
|
||||||
|
|
||||||
function uploadFiles(files) {
|
|
||||||
if (files.length === 0) return;
|
|
||||||
|
|
||||||
const formData = new FormData();
|
|
||||||
for (let file of files) {
|
|
||||||
formData.append('files', file);
|
|
||||||
}
|
|
||||||
|
|
||||||
progressContainer.style.display = 'block';
|
|
||||||
progressBar.style.width = '0%%';
|
|
||||||
results.innerHTML = '';
|
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.upload.addEventListener('progress', (e) => {
|
|
||||||
if (e.lengthComputable) {
|
|
||||||
const percent = (e.loaded / e.total) * 100;
|
|
||||||
progressBar.style.width = percent + '%%';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
xhr.addEventListener('load', () => {
|
|
||||||
if (xhr.status === 200) {
|
|
||||||
const response = JSON.parse(xhr.responseText);
|
|
||||||
displayResults(response.urls);
|
|
||||||
} else {
|
|
||||||
results.innerHTML = '<div class="result-item">Upload failed: ' + xhr.statusText + '</div>';
|
|
||||||
}
|
|
||||||
progressBar.style.width = '100%%';
|
|
||||||
});
|
|
||||||
|
|
||||||
xhr.addEventListener('error', () => {
|
|
||||||
results.innerHTML = '<div class="result-item">Upload failed: Network error</div>';
|
|
||||||
});
|
|
||||||
|
|
||||||
xhr.open('POST', '/upload');
|
|
||||||
xhr.send(formData);
|
|
||||||
}
|
|
||||||
|
|
||||||
function displayResults(urls) {
|
|
||||||
results.innerHTML = '<h3>Download URLs:</h3>';
|
|
||||||
urls.forEach(url => {
|
|
||||||
const div = document.createElement('div');
|
|
||||||
div.className = 'result-item';
|
|
||||||
div.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>';
|
|
||||||
results.appendChild(div);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>`, proto, host, proto, host)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.Write([]byte(html))
|
indexHTML.Execute(w, pageData)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user