mirror of
https://github.com/Xevion/banner.git
synced 2025-12-07 20:06:25 -06:00
Continue improving comments & use of structured logs
This commit is contained in:
22
api.go
22
api.go
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -38,11 +37,10 @@ func GetTerms(search string, offset int, max int) ([]Term, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// print the response body
|
// print the response body
|
||||||
body, _ := io.ReadAll(res.Body)
|
// _body, _ := io.ReadAll(res.Body)
|
||||||
log.Printf("Response Body: %s", body)
|
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +67,7 @@ func GetPartOfTerms(search string, term int, offset int, max int) ([]TermParts,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +95,7 @@ func GetInstructor(search string, term int, offset int, max int) []Instructor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +119,7 @@ func GetClassDetails(term int, crn int) *ClassDetails {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +149,7 @@ func Search(subject string, keyword string, term string, startDate time.Time, en
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,7 +177,7 @@ func GetSubjects(search string, term int, offset int, max int) []Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,7 +205,7 @@ func GetCampuses(search string, term int, offset int, max int) []Campus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,7 +233,7 @@ func GetInstructionalMethods(search string, term int, offset int, max int) ([]In
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +256,7 @@ func GetCourseMeetingTime(term int, crn int) (*MeetingTimeResponse, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContainsContentType(res.Header.Get("Content-Type"), "application/json") {
|
if !ContainsContentType(res, "application/json") {
|
||||||
log.Fatal().Msgf("Response was not JSON: %s", res.Header.Get("Content-Type"))
|
log.Fatal().Msgf("Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,9 +42,9 @@ func AddUserAgent(req *http.Request) {
|
|||||||
req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
|
req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContainsContentType(header string, search string) bool {
|
func ContainsContentType(response *http.Response, search string) bool {
|
||||||
// Split on commas, check if any of the types match
|
// Split on commas, check if any of the types match
|
||||||
for _, content_type := range strings.Split(header, ";") {
|
for _, content_type := range strings.Split(response.Header.Get("Content-Type"), ";") {
|
||||||
if content_type == search {
|
if content_type == search {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -67,8 +67,9 @@ func Nonce() string {
|
|||||||
return strconv.Itoa(int(time.Now().UnixMilli()))
|
return strconv.Itoa(int(time.Now().UnixMilli()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// doRequest performs & logs the request, logging and returning the response
|
||||||
func doRequest(req *http.Request) (*http.Response, error) {
|
func doRequest(req *http.Request) (*http.Response, error) {
|
||||||
log.Debug().Str("method", req.Method).Msg("Request")
|
log.Debug().Str("method", strings.TrimRight(req.Method, " ")).Str("url", req.URL.String()).Str("query", req.URL.RawQuery).Str("content-type", req.Header.Get("Content-Type")).Msg("Request")
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Str("method", req.Method).Msg("Request Failed")
|
log.Err(err).Str("method", req.Method).Msg("Request Failed")
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -63,11 +63,11 @@ func init() {
|
|||||||
envVarSplit := strings.Split(envVar, "=")
|
envVarSplit := strings.Split(envVar, "=")
|
||||||
environmentDict.Str(envVarSplit[0], envVarSplit[1])
|
environmentDict.Str(envVarSplit[0], envVarSplit[1])
|
||||||
})
|
})
|
||||||
log.Info().Dict("env", environmentDict).Msg("env")
|
|
||||||
|
|
||||||
if isDevelopment {
|
if isDevelopment {
|
||||||
log.Logger = zerolog.New(logOut{}).With().Timestamp().Logger()
|
log.Logger = zerolog.New(logOut{}).With().Timestamp().Logger()
|
||||||
}
|
}
|
||||||
|
log.Info().Dict("env", environmentDict).Msg("env")
|
||||||
|
|
||||||
discordgo.Logger = func(msgL, caller int, format string, a ...interface{}) {
|
discordgo.Logger = func(msgL, caller int, format string, a ...interface{}) {
|
||||||
pc, file, line, _ := runtime.Caller(caller)
|
pc, file, line, _ := runtime.Caller(caller)
|
||||||
@@ -146,7 +146,7 @@ func main() {
|
|||||||
lo.ForEach(commandDefinitions, func(cmd *discordgo.ApplicationCommand, _ int) {
|
lo.ForEach(commandDefinitions, func(cmd *discordgo.ApplicationCommand, _ int) {
|
||||||
arr.Str(cmd.Name)
|
arr.Str(cmd.Name)
|
||||||
})
|
})
|
||||||
log.Info().Array("commands", zerolog.Arr()).Msg("Registering commands")
|
log.Info().Array("commands", arr).Msg("Registering commands")
|
||||||
|
|
||||||
// Register commands
|
// Register commands
|
||||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
||||||
|
|||||||
12
session.go
12
session.go
@@ -21,12 +21,12 @@ func setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate that cookies were set
|
// Validate that cookies were set
|
||||||
baseURL_parsed, err := url.Parse(baseURL)
|
baseUrlParsed, err := url.Parse(baseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Msgf("Failed to parse baseURL: %s", baseURL)
|
log.Fatal().Str("baseURL", baseURL).Err(err).Msg("Failed to parse baseURL")
|
||||||
}
|
}
|
||||||
|
|
||||||
current_cookies := client.Jar.Cookies(baseURL_parsed)
|
current_cookies := client.Jar.Cookies(baseUrlParsed)
|
||||||
required_cookies := map[string]bool{
|
required_cookies := map[string]bool{
|
||||||
"JSESSIONID": false,
|
"JSESSIONID": false,
|
||||||
"SSB_COOKIE": false,
|
"SSB_COOKIE": false,
|
||||||
@@ -41,12 +41,12 @@ func setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if all required cookies were set
|
// Check if all required cookies were set
|
||||||
for cookie_name, cookie_set := range required_cookies {
|
for cookieName, cookie_set := range required_cookies {
|
||||||
if !cookie_set {
|
if !cookie_set {
|
||||||
log.Error().Msgf("Required cookie %s was not set", cookie_name)
|
log.Warn().Str("cookieName", cookieName).Msg("Required cookie not set")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Info().Msg("All cookies acquired. Session setup complete.")
|
log.Debug().Msg("All required cookies set, session setup complete")
|
||||||
|
|
||||||
// TODO: Validate that the session allows access to termSelection
|
// TODO: Validate that the session allows access to termSelection
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user