Skip to content

Commit 91c983a

Browse files
committed
Added squeleton version for our service, and add fully functional upload
1 parent 5af04b3 commit 91c983a

File tree

5 files changed

+129
-5
lines changed

5 files changed

+129
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,4 @@ Temporary Items
235235
# contains private data
236236
*http-client.private.env.json
237237
.idea/
238+
tempfiles/

markdown-note-taking-app/frontend/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ <h1>Notes</h1>
1313
<main>
1414
<ul id="notes">
1515
<li><a href="create.html">Create note</a></li>
16+
<li><a href="upload.html">Upload note</a></li>
1617
</ul>
1718
</main>
1819
<footer>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Upload note</title>
7+
<link rel="stylesheet" href="styles.css">
8+
</head>
9+
<body>
10+
<header>
11+
<h1>Upload note</h1>
12+
</header>
13+
<main>
14+
<form id="upload-note" action="http://localhost:8080/upload" method="post" enctype="multipart/form-data">
15+
<label for="file">File:</label>
16+
<input type="file" id="file" name="file" required accept=".md">
17+
<button type="submit">Upload note</button>
18+
</form>
19+
</main>
20+
<footer>
21+
<p>&copy; 2025 Notes</p>
22+
</footer>
23+
<script src="app.js"></script>
24+
</body>
25+
</html>

markdown-note-taking-app/main.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,53 @@ import (
44
"fmt"
55
"log"
66
"net/http"
7+
"markdown-note-taking-app/notes"
78
)
89

910

10-
func apiHandler(w http.ResponseWriter, r *http.Request) {
11-
fmt.Fprint(w, "Hello, World!")
11+
func createNote(w http.ResponseWriter, r *http.Request) {
12+
fmt.Fprint(w, "Create note")
13+
}
14+
15+
func checkGrammarAndSpelling(w http.ResponseWriter, r *http.Request) {
16+
fmt.Fprint(w, "Check grammar and spelling")
17+
18+
}
19+
20+
func listNotes(w http.ResponseWriter, r *http.Request) {
21+
fmt.Fprint(w, "List notes")
22+
}
23+
24+
func readNote(w http.ResponseWriter, r *http.Request) {
25+
fmt.Fprint(w, "Read note")
26+
}
27+
28+
func deleteNote(w http.ResponseWriter, r *http.Request) {
29+
fmt.Fprint(w, "Delete note")
30+
}
31+
32+
func updateNote(w http.ResponseWriter, r *http.Request) {
33+
fmt.Fprint(w, "Update note")
1234
}
1335

1436
func startFrontendServer() {
15-
fs := http.FileServer(http.Dir("frontend"))
37+
fs := http.FileServer(http.Dir("frontend"))
1638
http.Handle("/", fs)
1739
log.Fatal(http.ListenAndServe(":80", nil))
1840
}
1941

2042
func startBackendServer() {
21-
http.HandleFunc("/api", apiHandler)
22-
log.Fatal(http.ListenAndServe(":8080", nil))
43+
// todo change to router and use verbs GET, POST, PUT, DELETE
44+
45+
mux := http.NewServeMux()
46+
mux.HandleFunc("/api/createNote", createNote)
47+
mux.HandleFunc("/api/upload", notes.UploadHandler)
48+
mux.HandleFunc("/api/checkGrammarAndSpelling", checkGrammarAndSpelling)
49+
mux.HandleFunc("/api/listNotes", listNotes)
50+
mux.HandleFunc("/api/readNote", readNote)
51+
mux.HandleFunc("/api/deleteNote", deleteNote)
52+
mux.HandleFunc("/api/updateNote", updateNote)
53+
log.Fatal(http.ListenAndServe(":8080", mux))
2354

2455
}
2556

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package notes
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"mime/multipart"
7+
"net/http"
8+
"os"
9+
"path/filepath"
10+
)
11+
12+
var rootPath = "./"
13+
14+
func UploadHandler(w http.ResponseWriter, r *http.Request) {
15+
//1. Param input for multipart file upload
16+
r.ParseMultipartForm(200 << 20) // Maximum of 200MB file allowed
17+
18+
file, handler, err := r.FormFile("file")
19+
if err != nil {
20+
w.WriteHeader(http.StatusBadRequest)
21+
fmt.Fprint(w, "Error parsing form file")
22+
return
23+
}
24+
25+
err = saveFile(file, handler)
26+
if err != nil {
27+
w.WriteHeader(http.StatusInternalServerError)
28+
fmt.Fprint(w, "Error saving file")
29+
return
30+
}
31+
32+
defer file.Close()
33+
34+
w.WriteHeader(http.StatusOK)
35+
fmt.Fprint(w, "File uploaded successfully")
36+
}
37+
38+
func saveFile(file multipart.File, handler *multipart.FileHeader) error {
39+
//3. Create a temporary file to our directory
40+
tempFolderPath := fmt.Sprintf("%s%s", rootPath, "/tempFiles")
41+
tempFileName := fmt.Sprintf("upload-%s-*.%s", fileNameWithoutExtension(handler.Filename), filepath.Ext(handler.Filename))
42+
43+
tempFile, err := os.CreateTemp(tempFolderPath, tempFileName)
44+
if err != nil {
45+
return err
46+
}
47+
defer tempFile.Close()
48+
49+
fileBytes, err := io.ReadAll(file)
50+
if err != nil {
51+
return err
52+
}
53+
_, err = tempFile.Write(fileBytes)
54+
if err != nil {
55+
return err
56+
}
57+
58+
return nil
59+
60+
}
61+
62+
63+
64+
func fileNameWithoutExtension(filename string) string {
65+
return filename[:len(filename)-len(filepath.Ext(filename))]
66+
}

0 commit comments

Comments
 (0)