2023-08-17 22:32:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-08-18 22:34:42 +02:00
|
|
|
"strconv"
|
2023-08-17 22:32:43 +02:00
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2023-08-17 22:32:43 +02:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var db *sql.DB
|
|
|
|
|
|
|
|
type Note struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func httpError(err error, statusCode int, message string, w http.ResponseWriter) bool {
|
|
|
|
if err != nil {
|
2023-08-18 22:34:42 +02:00
|
|
|
log.Println(err)
|
2023-08-17 22:32:43 +02:00
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
io.WriteString(w, message)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
func getNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
|
2023-08-17 22:32:43 +02:00
|
|
|
res, err := db.Query(`SELECT Id, Content FROM Notes`)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to load notes", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Close()
|
|
|
|
|
|
|
|
var notes []Note = []Note{}
|
|
|
|
|
|
|
|
for res.Next() {
|
|
|
|
if httpError(res.Err(), http.StatusInternalServerError, "Failed to load notes", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var note Note
|
|
|
|
res.Scan(¬e.Id, ¬e.Content)
|
|
|
|
notes = append(notes, note)
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(notes)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to create JSON", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-08-17 22:32:43 +02:00
|
|
|
w.Write(json)
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
func postNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
|
2023-08-17 22:32:43 +02:00
|
|
|
data, err := io.ReadAll(req.Body)
|
|
|
|
if httpError(err, http.StatusBadRequest, "Failed to read data", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var note Note
|
|
|
|
err = json.Unmarshal(data, ¬e)
|
|
|
|
if httpError(err, http.StatusBadRequest, "Invalid request", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(note.Content) == 0 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
io.WriteString(w, "Missing note content")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := db.Prepare(`INSERT INTO Notes(Content) VALUES(?) RETURNING Id`)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to store note", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
res := stmt.QueryRow(note.Content)
|
|
|
|
err = res.Scan(¬e.Id)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to store note", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(note)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to create JSON", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-08-17 22:32:43 +02:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
w.Write(json)
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
func getNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
|
|
|
|
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
|
|
|
|
if httpError(err, http.StatusBadRequest, "Invalid id", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := db.Prepare(`SELECT Content FROM Notes WHERE Id = ?`)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to get note", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
res := stmt.QueryRow(id)
|
|
|
|
|
|
|
|
var note Note = Note{Id: id}
|
|
|
|
|
|
|
|
err = res.Scan(¬e.Content)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
io.WriteString(w, "Note doesn't exist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to get note", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(note)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to create JSON", w) {
|
2023-08-17 22:32:43 +02:00
|
|
|
return
|
|
|
|
}
|
2023-08-18 22:34:42 +02:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(json)
|
2023-08-17 22:32:43 +02:00
|
|
|
}
|
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
func putNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
|
|
|
|
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
|
|
|
|
if httpError(err, http.StatusBadRequest, "Invalid id", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := io.ReadAll(req.Body)
|
|
|
|
if httpError(err, http.StatusBadRequest, "Failed to read data", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var note Note
|
|
|
|
err = json.Unmarshal(data, ¬e)
|
|
|
|
if httpError(err, http.StatusBadRequest, "Invalid request", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(note.Content) == 0 {
|
2023-08-17 22:32:43 +02:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2023-08-18 22:34:42 +02:00
|
|
|
io.WriteString(w, "Missing note content")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := db.Prepare(`UPDATE Notes SET Content = ? WHERE Id = ?`)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to update note", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
res, err := stmt.Exec(note.Content, id)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to update note", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err := res.RowsAffected()
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to update note", w) {
|
|
|
|
return
|
2023-08-17 22:32:43 +02:00
|
|
|
}
|
2023-08-18 22:34:42 +02:00
|
|
|
|
|
|
|
if updated == 0 {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
io.WriteString(w, "Note doesn't exist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
note.Id = id
|
|
|
|
json, err := json.Marshal(note)
|
|
|
|
if httpError(err, http.StatusInternalServerError, "Failed to create JSON", w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(json)
|
2023-08-17 22:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.Println("Starting NoteServer")
|
|
|
|
|
|
|
|
log.Println("Loading database")
|
|
|
|
|
|
|
|
var err error
|
|
|
|
db, err = sql.Open("sqlite3", "./notes.sqlite")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to load db:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Creating missing tables")
|
|
|
|
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS Notes(Id INTEGER PRIMARY KEY AUTOINCREMENT, Content TEXT)`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to create table:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Listening")
|
|
|
|
|
2023-08-18 22:34:42 +02:00
|
|
|
router := httprouter.New()
|
|
|
|
router.GET("/api/notes", getNotes)
|
|
|
|
router.POST("/api/notes", postNotes)
|
|
|
|
router.GET("/api/note/:id", getNote)
|
|
|
|
router.PUT("/api/note/:id", putNote)
|
|
|
|
|
|
|
|
http.ListenAndServe(":8080", router) // TODO: configure host, port, TLS
|
2023-08-17 22:32:43 +02:00
|
|
|
}
|