rogueserver/rogueserver.go

169 lines
4.3 KiB
Go
Raw Normal View History

2024-04-29 17:26:46 -04:00
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-04-29 15:32:58 -04:00
2023-12-05 13:28:08 -05:00
package main
import (
"encoding/gob"
2023-12-05 13:28:08 -05:00
"log"
"net"
2023-12-05 13:28:08 -05:00
"net/http"
"os"
"strconv"
2023-12-05 13:28:08 -05:00
"github.com/bwmarrin/discordgo"
2024-04-29 15:22:27 -04:00
"github.com/pagefaultgames/rogueserver/api"
2024-07-27 20:38:32 -04:00
"github.com/pagefaultgames/rogueserver/api/account"
2024-04-29 15:22:27 -04:00
"github.com/pagefaultgames/rogueserver/db"
2023-12-05 13:28:08 -05:00
)
func main() {
// env stuff
debug, _ := strconv.ParseBool(os.Getenv("debug"))
2024-04-25 14:41:48 -04:00
proto := getEnv("proto", "tcp")
addr := getEnv("addr", "0.0.0.0:8001")
tlscert := getEnv("tlscert", "")
tlskey := getEnv("tlskey", "")
2023-12-05 13:28:08 -05:00
dbuser := getEnv("dbuser", "pokerogue")
dbpass := getEnv("dbpass", "pokerogue")
dbproto := getEnv("dbproto", "tcp")
dbaddr := getEnv("dbaddr", "localhost")
dbname := getEnv("dbname", "pokeroguedb")
2023-12-05 13:28:08 -05:00
discordclientid := getEnv("discordclientid", "")
discordsecretid := getEnv("discordsecretid", "")
googleclientid := getEnv("googleclientid", "")
googlesecretid := getEnv("googlesecretid", "")
2024-07-27 20:38:32 -04:00
callbackurl := getEnv("callbackurl", "http://localhost:8001/")
gameurl := getEnv("gameurl", "https://pokerogue.net")
discordbottoken := getEnv("discordbottoken", "")
discordguildid := getEnv("discordguildid", "")
2023-12-05 13:28:08 -05:00
account.GameURL = gameurl
2024-07-27 20:38:32 -04:00
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
// register gob types
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
2024-04-17 19:31:12 -04:00
// get database connection
err := db.Init(dbuser, dbpass, dbproto, dbaddr, dbname)
2023-12-05 13:28:08 -05:00
if err != nil {
log.Fatalf("failed to initialize database: %s", err)
}
// create listener
listener, err := createListener(proto, addr)
if err != nil {
log.Fatalf("failed to create net listener: %s", err)
}
2024-04-17 19:31:12 -04:00
mux := http.NewServeMux()
2024-04-17 19:31:12 -04:00
// init api
2024-05-11 14:06:47 +02:00
if err := api.Init(mux); err != nil {
log.Fatal(err)
}
// start web server
handler := prodHandler(mux, gameurl)
if debug {
2024-05-10 13:16:35 -04:00
handler = debugHandler(mux)
}
if tlscert == "" {
2024-05-10 13:16:35 -04:00
err = http.Serve(listener, handler)
2024-04-25 16:13:34 -04:00
} else {
err = http.ServeTLS(listener, handler, tlscert, tlskey)
2024-04-25 14:41:48 -04:00
}
2024-04-19 03:27:47 -04:00
if err != nil {
log.Fatalf("failed to create http server or server errored: %s", err)
2024-04-17 19:31:12 -04:00
}
}
func createListener(proto, addr string) (net.Listener, error) {
if proto == "unix" {
os.Remove(addr)
}
listener, err := net.Listen(proto, addr)
if err != nil {
return nil, err
}
if proto == "unix" {
2024-05-11 14:06:47 +02:00
if err := os.Chmod(addr, 0777); err != nil {
listener.Close()
return nil, err
}
}
return listener, nil
2024-04-25 14:41:48 -04:00
}
func prodHandler(router *http.ServeMux, clienturl string) http.Handler {
2024-04-25 14:41:48 -04:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-05-10 13:40:00 -04:00
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)
2024-04-25 16:13:34 -04:00
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
2024-04-25 14:41:48 -04:00
router.ServeHTTP(w, r)
})
}
2024-05-10 13:16:35 -04:00
2024-05-10 13:40:00 -04:00
func debugHandler(router *http.ServeMux) http.Handler {
2024-05-10 13:16:35 -04:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-05-10 13:40:00 -04:00
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Origin", "*")
2024-05-10 13:16:35 -04:00
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
router.ServeHTTP(w, r)
})
}
func getEnv(key string, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}