Use better router, Get/Update note

This commit is contained in:
MrLetsplay 2023-08-18 22:34:42 +02:00
parent 858174b9d2
commit 84b877017c
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 104 additions and 18 deletions

2
go.mod
View File

@ -3,3 +3,5 @@ module git.cringe-studios.com/NoteServer
go 1.20 go 1.20
require github.com/mattn/go-sqlite3 v1.14.17 require github.com/mattn/go-sqlite3 v1.14.17
require github.com/julienschmidt/httprouter v1.3.0

2
go.sum
View File

@ -1,2 +1,4 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

118
main.go
View File

@ -6,7 +6,9 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"strconv"
"github.com/julienschmidt/httprouter"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -19,6 +21,7 @@ type Note struct {
func httpError(err error, statusCode int, message string, w http.ResponseWriter) bool { func httpError(err error, statusCode int, message string, w http.ResponseWriter) bool {
if err != nil { if err != nil {
log.Println(err)
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
io.WriteString(w, message) io.WriteString(w, message)
return true return true
@ -27,7 +30,7 @@ func httpError(err error, statusCode int, message string, w http.ResponseWriter)
return false return false
} }
func getNotes(w http.ResponseWriter, req *http.Request) { func getNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
res, err := db.Query(`SELECT Id, Content FROM Notes`) res, err := db.Query(`SELECT Id, Content FROM Notes`)
if httpError(err, http.StatusInternalServerError, "Failed to load notes", w) { if httpError(err, http.StatusInternalServerError, "Failed to load notes", w) {
return return
@ -52,10 +55,11 @@ func getNotes(w http.ResponseWriter, req *http.Request) {
return return
} }
w.Header().Set("Content-Type", "application/json")
w.Write(json) w.Write(json)
} }
func postNotes(w http.ResponseWriter, req *http.Request) { func postNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
data, err := io.ReadAll(req.Body) data, err := io.ReadAll(req.Body)
if httpError(err, http.StatusBadRequest, "Failed to read data", w) { if httpError(err, http.StatusBadRequest, "Failed to read data", w) {
return return
@ -91,28 +95,102 @@ func postNotes(w http.ResponseWriter, req *http.Request) {
return return
} }
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
w.Write(json) w.Write(json)
} }
func putNote(w http.ResponseWriter, req *http.Request) { func getNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
if req.Method != http.MethodPut { id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
w.WriteHeader(http.StatusBadRequest) if httpError(err, http.StatusBadRequest, "Invalid id", w) {
io.WriteString(w, "Invalid method")
return return
} }
stmt, err := db.Prepare(`SELECT Content FROM Notes WHERE Id = ?`)
if httpError(err, http.StatusInternalServerError, "Failed to get note", w) {
return
} }
func handleNotes(w http.ResponseWriter, req *http.Request) { defer stmt.Close()
switch req.Method {
case http.MethodGet: res := stmt.QueryRow(id)
getNotes(w, req)
case http.MethodPost: var note Note = Note{Id: id}
postNotes(w, req)
default: err = res.Scan(&note.Content)
w.WriteHeader(http.StatusBadRequest) if err == sql.ErrNoRows {
io.WriteString(w, "Invalid method") 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) {
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}
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, &note)
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(`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
}
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)
} }
func main() { func main() {
@ -134,7 +212,11 @@ func main() {
log.Println("Listening") log.Println("Listening")
http.HandleFunc("/api/notes", handleNotes) router := httprouter.New()
http.HandleFunc("/api/note/", putNote) router.GET("/api/notes", getNotes)
http.ListenAndServe(":8080", nil) // TODO: configure host, port, TLS 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
} }