diff --git a/server/cmd/app/main.go b/server/cmd/app/main.go new file mode 100644 index 0000000..7c1e996 --- /dev/null +++ b/server/cmd/app/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "log" + + "ufosc/alarm-clock/internal/api" +) + +func main() { + app := api.NewAPIServer("localhost:8080") + + if err := app.Run(); err != nil { + log.Fatal(err) + } + +} diff --git a/server/internal/api/api.go b/server/internal/api/api.go new file mode 100644 index 0000000..100c2fd --- /dev/null +++ b/server/internal/api/api.go @@ -0,0 +1,38 @@ +package api + +import ( + "log" + "net/http" + "ufosc/alarm-clock/internal/punishments/discord/webhook" + + "github.com/gorilla/mux" +) + +type APIServer struct { + addr string +} + +func NewAPIServer(addr string) *APIServer { + return &APIServer{ + addr: addr, + } +} + +func (s * APIServer) Run() error { + router := mux.NewRouter() + subrouter := router.PathPrefix("/api/v1").Subrouter() + + subrouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(200) + w.Write([]byte("Hello World")) + }) + + webhook.RegisterRoutes(subrouter) + + log.Println("Listening on", s.addr) + + return http.ListenAndServe(s.addr, router) + + +} \ No newline at end of file diff --git a/server/internal/punishments/discord/webhook/routes.go b/server/internal/punishments/discord/webhook/routes.go new file mode 100644 index 0000000..055a655 --- /dev/null +++ b/server/internal/punishments/discord/webhook/routes.go @@ -0,0 +1,36 @@ +package webhook + +import ( + "fmt" + "log" + "net/http" + "os" + + "github.com/gorilla/mux" + "github.com/joho/godotenv" + + "ufosc/alarm-clock/internal/utils" +) + +func RegisterRoutes(router *mux.Router) { + // router.HandleFunc("/discord/webhook", handleDiscordWebHook).Methods(http.MethodGet) + router.HandleFunc("/discord/webhook", handleDiscordWebHook).Methods(http.MethodGet) + +} + +func handleDiscordWebHook(w http.ResponseWriter, r *http.Request) { + // TODO: Create Webhook + + fmt.Println("Webhook activated") + err := godotenv.Load("../.env") + + if err != nil { + log.Fatal(err) + } + + token := os.Getenv("DISCORD_WEBHOOK") + + SendMessageExample(token) + utils.WriteJSON(w, http.StatusOK, "Discord Webhook sent") + +} \ No newline at end of file diff --git a/server/internal/punishments/discord/webhook/webhook.go b/server/internal/punishments/discord/webhook/webhook.go new file mode 100644 index 0000000..60df4ef --- /dev/null +++ b/server/internal/punishments/discord/webhook/webhook.go @@ -0,0 +1,39 @@ +package webhook + +import ( + "log" + + "github.com/gtuk/discordwebhook" +) + +func SendMessageExample(discord_webhook string) { + var username = "Wake Up" + var content = "Simple Message" + + var title = "Testing Embed" + var description = "Embed Description." + var color = "383483" // Must be a numerical value not nex + var url2 = "https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExbXluOXlraTJzNzU1eHhqdnNta2xld3l2anA0bG0zemM1NTBiajIxYyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/qpCvOBBmBkble/giphy.webp" + + image := discordwebhook.Image{ + Url: &url2, + } + + embeds := discordwebhook.Embed{ + Title: &title, + Description: &description, + Color: &color, + Image: &image, + } + + message := discordwebhook.Message{ + Username: &username, + Content: &content, + Embeds: &[]discordwebhook.Embed{embeds}, + } + + err := discordwebhook.SendMessage(discord_webhook, message) + if err != nil { + log.Fatal(err) + } +} diff --git a/server/internal/types/types.go b/server/internal/types/types.go new file mode 100644 index 0000000..7193a74 --- /dev/null +++ b/server/internal/types/types.go @@ -0,0 +1 @@ +package types \ No newline at end of file diff --git a/server/internal/utils/utils.go b/server/internal/utils/utils.go new file mode 100644 index 0000000..ed063f2 --- /dev/null +++ b/server/internal/utils/utils.go @@ -0,0 +1,22 @@ +package utils + +import ( + "encoding/json" + "fmt" + "net/http" +) + +func WriteJSON(w http.ResponseWriter, status int, v any) error { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(status) + return json.NewEncoder(w).Encode(v) +} + +func ParseJSON(r *http.Request, v any) error { + if r.Body == nil { + return fmt.Errorf("missing request body") + } + + return json.NewDecoder(r.Body).Decode(v) +} +