feat: initial Gin-Go web app scaffold
This commit is contained in:
56
templates/index.html
Normal file
56
templates/index.html
Normal file
@@ -0,0 +1,56 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{{ .title }}</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ .title }}</h1>
|
||||
<p>{{ .message }}</p>
|
||||
|
||||
<div id="app">
|
||||
<h2>Users</h2>
|
||||
<ul id="user-list"></ul>
|
||||
<form id="user-form" autocomplete="off">
|
||||
<input type="text" id="name" placeholder="Name" required>
|
||||
<input type="email" id="email" placeholder="Email" required>
|
||||
<button type="submit">Add User</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function fetchUsers(){
|
||||
const res = await fetch('/users');
|
||||
const data = await res.json();
|
||||
const ul = document.getElementById('user-list');
|
||||
ul.innerHTML = '';
|
||||
data.forEach(u => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = u.id + ' - ' + u.name + ' (' + u.email + ')';
|
||||
ul.appendChild(li);
|
||||
});
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
fetchUsers();
|
||||
const form = document.getElementById('user-form');
|
||||
form.onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
const name = document.getElementById('name').value;
|
||||
const email = document.getElementById('email').value;
|
||||
const res = await fetch('/users', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type':'application/json'},
|
||||
body: JSON.stringify({name, email})
|
||||
});
|
||||
if(res.ok){
|
||||
form.reset();
|
||||
fetchUsers();
|
||||
} else {
|
||||
alert('Error creating user');
|
||||
}
|
||||
};
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user