-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored code base, created router for discord (#90)
- Loading branch information
Showing
6 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|