From f384640d03f3736501eecc037e1dc4f1557807b3 Mon Sep 17 00:00:00 2001 From: Odyssey346 Date: Thu, 4 Aug 2022 20:53:05 +0200 Subject: [PATCH] Switch to an actual JSON library. Signed-off-by: Odyssey346 --- api/announcements.go | 32 +++++++++++--------------------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/api/announcements.go b/api/announcements.go index 0ad209a..6338928 100644 --- a/api/announcements.go +++ b/api/announcements.go @@ -3,12 +3,13 @@ package api import ( "errors" "io" + "io/ioutil" "log" "net/http" "os" "time" - "github.com/gorilla/feeds" + "github.com/goccy/go-json" ) var ( @@ -36,31 +37,20 @@ func handleAnnouncements(w http.ResponseWriter, r *http.Request) { } else { w.WriteHeader(http.StatusOK) now := time.Now() - feed := &feeds.Feed{ - Title: r.FormValue("title"), - Link: &feeds.Link{Href: r.FormValue("link")}, - Description: r.FormValue("severity"), - Created: now, + data := map[string]interface{}{ + "title": r.FormValue("title"), + "link": r.FormValue("link"), + "severity": r.FormValue("severity"), + "created": now, } - json, err := feed.ToJSON() + jsonData, err := json.Marshal(data) if err != nil { - log.Fatal(err) + log.Printf("could not marshal json: %s\n", err) + return } - f, err := os.Create("./static/announcements.json") - - if err != nil { - log.Fatal(err) - } - - defer f.Close() - - _, err2 := f.WriteString(json) - - if err2 != nil { - log.Fatal(err2) - } + ioutil.WriteFile("./static/announcements.json", jsonData, os.ModePerm) w.Write([]byte("Announcement posted!")) } diff --git a/go.mod b/go.mod index 2a36d81..6aa4557 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/ProjectSegfault/segfautils go 1.18 require ( + github.com/goccy/go-json v0.9.10 // indirect github.com/gorilla/feeds v1.1.1 // indirect github.com/kataras/hcaptcha v0.0.0-20200711031247-2927d4faf32f // indirect ) diff --git a/go.sum b/go.sum index 1eb8b8a..0407f62 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc= +github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY= github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA= github.com/kataras/hcaptcha v0.0.0-20200711031247-2927d4faf32f h1:ktcW6v946XnYiNU2taSVx79C5eDDQ8MxWepJ8S1Mz5A=