Switch to an actual JSON library.

Signed-off-by: Odyssey346 <odyssey346@disroot.org>
This commit is contained in:
Odyssey346 2022-08-04 20:53:05 +02:00
parent 2149d8c57c
commit f384640d03
3 changed files with 14 additions and 21 deletions

View File

@ -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!"))
}

1
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=