Skip to content

Commit

Permalink
refactored code base, created router for discord (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanielv authored Nov 15, 2024
1 parent d4a51c6 commit 21c853a
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server/cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
38 changes: 38 additions & 0 deletions server/internal/api/api.go
Original file line number Diff line number Diff line change
@@ -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)


}
36 changes: 36 additions & 0 deletions server/internal/punishments/discord/webhook/routes.go
Original file line number Diff line number Diff line change
@@ -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")

}
39 changes: 39 additions & 0 deletions server/internal/punishments/discord/webhook/webhook.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions server/internal/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package types
22 changes: 22 additions & 0 deletions server/internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 21c853a

Please sign in to comment.