2022-08-04 01:07:12 +05:30
package api
import (
"errors"
"io"
2022-08-05 00:23:05 +05:30
"io/ioutil"
2022-08-04 01:07:12 +05:30
"log"
"net/http"
"os"
"time"
2022-08-15 00:36:00 +05:30
"github.com/ProjectSegfault/segfautils/config"
2022-08-05 00:23:05 +05:30
"github.com/goccy/go-json"
2022-08-04 01:07:12 +05:30
)
var (
2022-08-15 00:36:00 +05:30
authToken = config . AuthToken ( )
2022-09-18 03:03:44 +05:30
resAnn = config . OptAnn ( )
2022-08-04 01:07:12 +05:30
)
2022-09-18 22:14:37 +05:30
func AnnCheck ( ) {
2022-09-18 23:46:57 +05:30
if resAnn == "false" {
2022-09-18 22:14:37 +05:30
log . Println ( "[Segfautils] ℹ Announcements are disabled" )
2022-09-18 03:42:06 +05:30
http . HandleFunc ( "/announcements" , func ( w http . ResponseWriter , r * http . Request ) {
2022-09-18 21:48:14 +05:30
http . Error ( w , "Announcements are disabled." , http . StatusServiceUnavailable )
2022-09-18 02:25:50 +05:30
} )
2022-09-24 20:56:47 +05:30
http . HandleFunc ( "/api/set/announcements" , func ( w http . ResponseWriter , r * http . Request ) {
2022-09-18 21:48:14 +05:30
http . Error ( w , "{\"enabled\": \"false\"}" , http . StatusServiceUnavailable )
2022-09-18 03:42:06 +05:30
} )
2022-09-18 23:46:57 +05:30
} else {
AnnPage ( )
2022-09-24 20:56:47 +05:30
http . HandleFunc ( "/api/set/announcements" , func ( w http . ResponseWriter , r * http . Request ) {
http . Error ( w , "{\"enabled\": \"true\"}" , http . StatusServiceUnavailable )
} )
2022-09-18 23:46:57 +05:30
Announcements ( )
2022-09-18 02:20:55 +05:30
}
}
2022-09-18 03:42:06 +05:30
func AnnPage ( ) {
http . HandleFunc ( "/announcements" , func ( w http . ResponseWriter , r * http . Request ) {
http . ServeFile ( w , r , "static/announcements.html" )
} )
}
2022-08-04 01:07:12 +05:30
func Announcements ( ) {
http . HandleFunc ( "/api/announcements" , getAnnouncements )
http . HandleFunc ( "/api/announcements/post" , handleAnnouncements )
http . HandleFunc ( "/api/announcements/delete" , handleAnnouncementDeleteRequest )
}
func handleAnnouncements ( w http . ResponseWriter , r * http . Request ) {
if r . Method != "POST" {
http . Error ( w , "Method not allowed" , http . StatusMethodNotAllowed )
return
}
if r . FormValue ( "token" ) != authToken {
http . Error ( w , "You need to provide the authorization token given to you by your system administrator in order to post an announcement." , http . StatusUnauthorized )
return
} else {
2022-09-13 19:17:10 +05:30
if r . FormValue ( "title" ) == "" || r . FormValue ( "severity" ) == "" {
http . Error ( w , "Your request is not proper. Please add a title and severity." , http . StatusBadRequest )
2022-08-04 01:07:12 +05:30
return
} else {
w . WriteHeader ( http . StatusOK )
2022-08-05 18:47:56 +05:30
now := time . Now ( ) . Unix ( )
2022-08-05 00:23:05 +05:30
data := map [ string ] interface { } {
"title" : r . FormValue ( "title" ) ,
"link" : r . FormValue ( "link" ) ,
"severity" : r . FormValue ( "severity" ) ,
"created" : now ,
2022-08-04 01:07:12 +05:30
}
2022-08-05 00:23:05 +05:30
jsonData , err := json . Marshal ( data )
2022-08-04 01:07:12 +05:30
if err != nil {
2022-08-05 00:23:05 +05:30
log . Printf ( "could not marshal json: %s\n" , err )
return
2022-08-04 01:07:12 +05:30
}
2022-08-05 18:47:56 +05:30
ioutil . WriteFile ( "./data/announcements.json" , jsonData , os . ModePerm )
2022-08-04 01:07:12 +05:30
w . Write ( [ ] byte ( "Announcement posted!" ) )
}
return
}
}
func handleAnnouncementDeleteRequest ( w http . ResponseWriter , r * http . Request ) {
if r . Method != "POST" {
http . Error ( w , "Method not allowed" , http . StatusMethodNotAllowed )
return
}
if r . FormValue ( "token" ) != authToken {
http . Error ( w , "You need to provide the authorization token given to you by your system administrator in order to delete an announcement." , http . StatusUnauthorized )
return
} else {
2022-08-05 18:47:56 +05:30
if _ , err := os . Stat ( "./data/announcements.json" ) ; errors . Is ( err , os . ErrNotExist ) {
2022-08-04 01:07:12 +05:30
http . Error ( w , "If you're gonna delete the annoucement, there has to be an announcement in the first place." , http . StatusNotFound )
return
} else {
2022-08-05 18:47:56 +05:30
err := os . Remove ( "./data/announcements.json" )
2022-08-04 01:07:12 +05:30
if err != nil {
log . Fatal ( err )
}
2022-08-04 01:13:20 +05:30
w . WriteHeader ( http . StatusOK )
2022-08-04 04:04:10 +05:30
w . Write ( [ ] byte ( "Announcement deleted!" ) )
2022-08-04 01:07:12 +05:30
return
}
}
}
func getAnnouncements ( w http . ResponseWriter , r * http . Request ) {
if r . Method != "GET" {
http . Error ( w , "Method not allowed" , http . StatusMethodNotAllowed )
return
}
2022-08-05 18:47:56 +05:30
if _ , err := os . Stat ( "./data/announcements.json" ) ; errors . Is ( err , os . ErrNotExist ) {
2022-08-04 01:07:12 +05:30
http . Error ( w , "There are no announcements." , http . StatusNotFound )
return
} else {
2022-08-05 18:47:56 +05:30
f , err := os . Open ( "./data/announcements.json" )
2022-08-04 01:07:12 +05:30
if err != nil {
log . Fatal ( err )
}
defer f . Close ( )
io . Copy ( w , f )
}
}