mirror of
https://github.com/Xevion/banner.git
synced 2025-12-09 10:06:34 -06:00
Simplify onRequest/Response into doRequest, print environment, dev-only consolewriter
This commit is contained in:
7
api.go
7
api.go
@@ -32,10 +32,7 @@ func GetTerms(search string, offset int, max int) ([]Term, error) {
|
||||
"_": Nonce(),
|
||||
})
|
||||
|
||||
onRequest(req)
|
||||
res, err := client.Do(req)
|
||||
onResponse(res)
|
||||
|
||||
res, err := doRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -49,7 +46,7 @@ func GetTerms(search string, offset int, max int) ([]Term, error) {
|
||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
||||
}
|
||||
|
||||
return make([]Term, 0), nil
|
||||
return make([]Term, 0, 0), nil
|
||||
}
|
||||
|
||||
type TermParts struct {
|
||||
|
||||
13
helpers.go
13
helpers.go
@@ -67,12 +67,15 @@ func Nonce() string {
|
||||
return strconv.Itoa(int(time.Now().UnixMilli()))
|
||||
}
|
||||
|
||||
func onRequest(req *http.Request) {
|
||||
log.Printf("GET %s", req.URL.String())
|
||||
func doRequest(req *http.Request) (*http.Response, error) {
|
||||
log.Debug().Str("method", req.Method).Msg("Request")
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Err(err).Str("method", req.Method).Msg("Request Failed")
|
||||
} else {
|
||||
log.Debug().Str("status", res.Status).Int64("content-length", res.ContentLength).Strs("content-type", res.Header["Content-Type"]).Msg("Response")
|
||||
}
|
||||
|
||||
func onResponse(res *http.Response) {
|
||||
log.Printf("%s %d %s", res.Status, res.ContentLength, res.Header["Content-Type"])
|
||||
return res, err
|
||||
}
|
||||
|
||||
func Plural(n int) string {
|
||||
|
||||
29
main.go
29
main.go
@@ -52,7 +52,23 @@ func (l logOut) WriteLevel(level zerolog.Level, p []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
env := os.Getenv("ENV")
|
||||
if env == "" {
|
||||
env = "development"
|
||||
}
|
||||
var isDevelopment bool = env == "development"
|
||||
|
||||
environmentDict := zerolog.Dict()
|
||||
lo.ForEach(os.Environ(), func(envVar string, _ int) {
|
||||
envVarSplit := strings.Split(envVar, "=")
|
||||
environmentDict.Str(envVarSplit[0], envVarSplit[1])
|
||||
})
|
||||
log.Info().Dict("env", environmentDict).Msg("env")
|
||||
|
||||
if isDevelopment {
|
||||
log.Logger = zerolog.New(logOut{}).With().Timestamp().Logger()
|
||||
}
|
||||
|
||||
discordgo.Logger = func(msgL, caller int, format string, a ...interface{}) {
|
||||
pc, file, line, _ := runtime.Caller(caller)
|
||||
|
||||
@@ -90,16 +106,18 @@ func main() {
|
||||
}
|
||||
baseURL = os.Getenv("BANNER_BASE_URL")
|
||||
|
||||
//
|
||||
// Create cookie jar
|
||||
var err error
|
||||
cookies, err = cookiejar.New(nil)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Cannot create cookie jar")
|
||||
}
|
||||
|
||||
// Create client, setup session (acquire cookies)
|
||||
client = http.Client{Jar: cookies}
|
||||
setup()
|
||||
|
||||
// Create discord session
|
||||
session, err = discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Invalid bot parameters")
|
||||
@@ -130,13 +148,14 @@ func main() {
|
||||
})
|
||||
log.Info().Array("commands", zerolog.Arr()).Msg("Registering commands")
|
||||
|
||||
// Register commands
|
||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
||||
for i, v := range commandDefinitions {
|
||||
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v)
|
||||
for i, cmdDefinition := range commandDefinitions {
|
||||
cmdInstance, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), cmdDefinition)
|
||||
if err != nil {
|
||||
log.Panic().Msgf("Cannot create '%v' command: %v", v.Name, err)
|
||||
log.Panic().Err(err).Str("name", cmdDefinition.Name).Msgf("Cannot register command")
|
||||
}
|
||||
registeredCommands[i] = cmd
|
||||
registeredCommands[i] = cmdInstance
|
||||
}
|
||||
|
||||
// Cloes session, ensure
|
||||
|
||||
@@ -17,9 +17,7 @@ func setup() {
|
||||
|
||||
for _, path := range request_queue {
|
||||
req := BuildRequest("GET", path, nil)
|
||||
onRequest(req)
|
||||
res, _ := client.Do(req)
|
||||
onResponse(res)
|
||||
doRequest(req)
|
||||
}
|
||||
|
||||
// Validate that cookies were set
|
||||
|
||||
Reference in New Issue
Block a user