Skip to content

Commit

Permalink
chore: Update server configuration flags for Discord Bot Token and Gu…
Browse files Browse the repository at this point in the history
…ild ID
  • Loading branch information
f-fsantos committed Sep 1, 2024
1 parent dff7579 commit 3ccc000
Showing 1 changed file with 44 additions and 38 deletions.
82 changes: 44 additions & 38 deletions rogueserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package main

import (
"encoding/gob"
"flag"
"log"
"net"
"net/http"
"os"
"strconv"

"github.com/bwmarrin/discordgo"
"github.com/pagefaultgames/rogueserver/api"
Expand All @@ -32,58 +32,56 @@ import (
)

func main() {
// flag stuff
debug := flag.Bool("debug", false, "use debug mode")
// env stuff
debug, _ := strconv.ParseBool(os.Getenv("debug"))

proto := flag.String("proto", "tcp", "protocol for api to use (tcp, unix)")
addr := flag.String("addr", "0.0.0.0:8001", "network address for api to listen on")
tlscert := flag.String("tlscert", "", "tls certificate path")
tlskey := flag.String("tlskey", "", "tls key path")
proto := getEnv("proto", "tcp")
addr := getEnv("addr", "0.0.0.0:8001")
tlscert := getEnv("tlscert", "")
tlskey := getEnv("tlskey", "")

dbuser := flag.String("dbuser", "pokerogue", "database username")
dbpass := flag.String("dbpass", "pokerogue", "database password")
dbproto := flag.String("dbproto", "tcp", "protocol for database connection")
dbaddr := flag.String("dbaddr", "localhost", "database address")
dbname := flag.String("dbname", "pokeroguedb", "database name")
dbuser := getEnv("dbuser", "pokerogue")
dbpass := getEnv("dbpass", "pokerogue")
dbproto := getEnv("dbproto", "tcp")
dbaddr := getEnv("dbaddr", "localhost")
dbname := getEnv("dbname", "pokeroguedb")

discordclientid := flag.String("discordclientid", "dcid", "Discord Oauth2 Client ID")
discordsecretid := flag.String("discordsecretid", "dsid", "Discord Oauth2 Secret ID")
discordclientid := getEnv("discordclientid", "")
discordsecretid := getEnv("discordsecretid", "")

googleclientid := flag.String("googleclientid", "gcid", "Google Oauth2 Client ID")
googlesecretid := flag.String("googlesecretid", "gsid", "Google Oauth2 Secret ID")
googleclientid := getEnv("googleclientid", "")
googlesecretid := getEnv("googlesecretid", "")

callbackurl := flag.String("callbackurl", "http://localhost:8001/", "Callback URL for Oauth2 Client")
callbackurl := getEnv("callbackurl", "http://localhost:8001/")

gameurl := flag.String("gameurl", "https://pokerogue.net", "URL for game server")
gameurl := getEnv("gameurl", "https://pokerogue.net")

discordbottoken := flag.String("discordbottoken", "", "Discord Bot Token")
discordguildid := flag.String("discordguildid", "", "Discord Guild ID")
discordbottoken := getEnv("discordbottoken", "")
discordguildid := getEnv("discordguildid", "")

flag.Parse()
account.GameURL = gameurl

account.GameURL = *gameurl
account.DiscordClientID = discordclientid
account.DiscordClientSecret = discordsecretid
account.DiscordCallbackURL = callbackurl + "/auth/discord/callback"

account.DiscordClientID = *discordclientid
account.DiscordClientSecret = *discordsecretid
account.DiscordCallbackURL = *callbackurl + "/auth/discord/callback"

account.GoogleClientID = *googleclientid
account.GoogleClientSecret = *googlesecretid
account.GoogleCallbackURL = *callbackurl + "/auth/google/callback"
account.DiscordSession, _ = discordgo.New("Bot " + *discordbottoken)
account.DiscordGuildID = *discordguildid
account.GoogleClientID = googleclientid
account.GoogleClientSecret = googlesecretid
account.GoogleCallbackURL = callbackurl + "/auth/google/callback"
account.DiscordSession, _ = discordgo.New("Bot " + discordbottoken)
account.DiscordGuildID = discordguildid
// register gob types
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})

// get database connection
err := db.Init(*dbuser, *dbpass, *dbproto, *dbaddr, *dbname)
err := db.Init(dbuser, dbpass, dbproto, dbaddr, dbname)
if err != nil {
log.Fatalf("failed to initialize database: %s", err)
}

// create listener
listener, err := createListener(*proto, *addr)
listener, err := createListener(proto, addr)
if err != nil {
log.Fatalf("failed to create net listener: %s", err)
}
Expand All @@ -97,14 +95,14 @@ func main() {

// start web server
handler := prodHandler(mux, gameurl)
if *debug {
if debug {
handler = debugHandler(mux)
}

if *tlscert == "" {
if tlscert == "" {
err = http.Serve(listener, handler)
} else {
err = http.ServeTLS(listener, handler, *tlscert, *tlskey)
err = http.ServeTLS(listener, handler, tlscert, tlskey)
}
if err != nil {
log.Fatalf("failed to create http server or server errored: %s", err)
Expand All @@ -131,11 +129,11 @@ func createListener(proto, addr string) (net.Listener, error) {
return listener, nil
}

func prodHandler(router *http.ServeMux, clienturl *string) http.Handler {
func prodHandler(router *http.ServeMux, clienturl string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
w.Header().Set("Access-Control-Allow-Origin", *clienturl)
w.Header().Set("Access-Control-Allow-Origin", clienturl)

if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
Expand All @@ -160,3 +158,11 @@ func debugHandler(router *http.ServeMux) http.Handler {
router.ServeHTTP(w, r)
})
}

func getEnv(key string, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}

return defaultValue
}

0 comments on commit 3ccc000

Please sign in to comment.