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 ( import (
"errors" "errors"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"time" "time"
"github.com/gorilla/feeds" "github.com/goccy/go-json"
) )
var ( var (
@ -36,31 +37,20 @@ func handleAnnouncements(w http.ResponseWriter, r *http.Request) {
} else { } else {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
now := time.Now() now := time.Now()
feed := &feeds.Feed{ data := map[string]interface{}{
Title: r.FormValue("title"), "title": r.FormValue("title"),
Link: &feeds.Link{Href: r.FormValue("link")}, "link": r.FormValue("link"),
Description: r.FormValue("severity"), "severity": r.FormValue("severity"),
Created: now, "created": now,
} }
json, err := feed.ToJSON() jsonData, err := json.Marshal(data)
if err != nil { if err != nil {
log.Fatal(err) log.Printf("could not marshal json: %s\n", err)
return
} }
f, err := os.Create("./static/announcements.json") ioutil.WriteFile("./static/announcements.json", jsonData, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err2 := f.WriteString(json)
if err2 != nil {
log.Fatal(err2)
}
w.Write([]byte("Announcement posted!")) w.Write([]byte("Announcement posted!"))
} }

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/ProjectSegfault/segfautils
go 1.18 go 1.18
require ( require (
github.com/goccy/go-json v0.9.10 // indirect
github.com/gorilla/feeds v1.1.1 // indirect github.com/gorilla/feeds v1.1.1 // indirect
github.com/kataras/hcaptcha v0.0.0-20200711031247-2927d4faf32f // 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 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA= github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
github.com/kataras/hcaptcha v0.0.0-20200711031247-2927d4faf32f h1:ktcW6v946XnYiNU2taSVx79C5eDDQ8MxWepJ8S1Mz5A= github.com/kataras/hcaptcha v0.0.0-20200711031247-2927d4faf32f h1:ktcW6v946XnYiNU2taSVx79C5eDDQ8MxWepJ8S1Mz5A=