mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Add server examples
This commit is contained in:
parent
7e9dc9038d
commit
8ca3672204
27
examples/server/notes_public/index.html
Normal file
27
examples/server/notes_public/index.html
Normal file
@ -0,0 +1,27 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Notes</title>
|
||||
<link href="styles.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div class="notes">
|
||||
<h2>Notes</h2>
|
||||
<div class="new-note">
|
||||
<input placeholder="Your note" />
|
||||
<button onclick="addNote()">add</button>
|
||||
</div>
|
||||
<ul id="notes"></ul>
|
||||
</div>
|
||||
|
||||
<templates>
|
||||
<ul name="note">
|
||||
<li class="note">
|
||||
<h4>Note 1</h4>
|
||||
<p>Content</p>
|
||||
</li>
|
||||
</ul>
|
||||
</templates>
|
||||
</body>
|
||||
<script src="notes.js"></script>
|
||||
</html>
|
33
examples/server/notes_public/notes.js
Normal file
33
examples/server/notes_public/notes.js
Normal file
@ -0,0 +1,33 @@
|
||||
const elNotes = document.querySelector('#notes');
|
||||
const elNoteTemplate = document.querySelector('templates ul[name="note"] li');
|
||||
|
||||
function renderNote(note) {
|
||||
const el = elNoteTemplate.cloneNode(true);
|
||||
el.querySelector('h4').innerText = 'Note #' + note.id;
|
||||
el.querySelector('p').innerText = note.content;
|
||||
return el;
|
||||
}
|
||||
|
||||
async function addNote() {
|
||||
const inpEl = document.querySelector('.new-note input');
|
||||
const content = inpEl.value;
|
||||
const resp = await fetch('/notes/', {
|
||||
method: "POST",
|
||||
body: content
|
||||
});
|
||||
const note = await resp.json();
|
||||
inpEl.value = '';
|
||||
console.log(note);
|
||||
elNotes.prepend(renderNote(note));
|
||||
}
|
||||
|
||||
async function getNotes() {
|
||||
const resp = await fetch("/notes");
|
||||
const notes = await resp.json();
|
||||
elNotes.innerHTML = '';
|
||||
for (const note of notes) {
|
||||
elNotes.prepend(renderNote(note));
|
||||
}
|
||||
}
|
||||
|
||||
getNotes();
|
35
examples/server/notes_public/styles.css
Normal file
35
examples/server/notes_public/styles.css
Normal file
@ -0,0 +1,35 @@
|
||||
templates {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#notes {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
}
|
||||
.note {
|
||||
margin: 0;
|
||||
padding: 0rem 0.3rem;
|
||||
}
|
||||
.note h4 {
|
||||
margin: 0;
|
||||
}
|
||||
.note p {
|
||||
color: #333;
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.new-note {
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
.new-note input {
|
||||
width: 15rem;
|
||||
padding: 0.3rem;
|
||||
}
|
||||
.new-note button {
|
||||
padding: 0.3rem 1rem;
|
||||
background-color: #4caf50;
|
||||
color: #fff;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
font-size: 1rem;
|
||||
}
|
37
examples/server/server_spa.own
Normal file
37
examples/server/server_spa.own
Normal file
@ -0,0 +1,37 @@
|
||||
use std, jdbc, server
|
||||
|
||||
// curl -X POST http://localhost:8084/notes/ -d "New note 2"
|
||||
|
||||
conn = getConnection("jdbc:sqlite::memory:")
|
||||
st = conn.createStatement()
|
||||
st.executeUpdate("CREATE TABLE IF NOT EXISTS notes(id integer primary key, content string)")
|
||||
stAddNote = conn.prepareStatement("INSERT INTO notes(content) VALUES(?)", RETURN_GENERATED_KEYS)
|
||||
stGetNote = conn.prepareStatement("SELECT id, content FROM notes WHERE id = ?")
|
||||
|
||||
createNote("This is your first note.")
|
||||
|
||||
def getNotes() {
|
||||
notes = []
|
||||
rs = st.executeQuery("SELECT id, content FROM notes")
|
||||
while (rs.next()) {
|
||||
notes += {"id": rs.getInt(1), "content": rs.getString(2)}
|
||||
}
|
||||
rs.close()
|
||||
return notes
|
||||
}
|
||||
|
||||
def createNote(content) {
|
||||
stAddNote.setString(1, content)
|
||||
stAddNote.executeUpdate()
|
||||
rs = stAddNote.getGeneratedKeys()
|
||||
rs.next()
|
||||
return {"id": rs.getLong(1) ?? -1, "content": content}
|
||||
}
|
||||
|
||||
newServer({"dev": true, "externalDirs": ["notes_public"]})
|
||||
.get("/notes", def(ctx) = ctx.json( getNotes() ))
|
||||
.post("/notes", def(ctx) {
|
||||
ctx.status(201)
|
||||
ctx.json( createNote(ctx.body()) )
|
||||
})
|
||||
.start(8084)
|
20
examples/server/server_spa_simple.own
Normal file
20
examples/server/server_spa_simple.own
Normal file
@ -0,0 +1,20 @@
|
||||
use std, jdbc, server
|
||||
|
||||
// curl -X POST http://localhost:8084/notes/ -d "New note 2"
|
||||
|
||||
notes = []
|
||||
createNote("This is your first note.")
|
||||
|
||||
def createNote(content) {
|
||||
note = {"id": notes.length + 1, "content": content};
|
||||
notes += note
|
||||
return note
|
||||
}
|
||||
|
||||
newServer({"externalDirs": ["notes_public"]})
|
||||
.get("/notes", def(ctx) = ctx.json(notes))
|
||||
.post("/notes", def(ctx) {
|
||||
ctx.status(201)
|
||||
ctx.json( createNote(ctx.body()) )
|
||||
})
|
||||
.start(8084)
|
Loading…
Reference in New Issue
Block a user