rogueserver/rogueserver.go

135 lines
3.1 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
"flag"
"log"
"net"
2023-12-05 13:28:08 -05:00
"net/http"
"os"
2024-05-08 20:13:22 +02:00
"strconv"
2023-12-05 13:28:08 -05:00
2024-04-29 15:22:27 -04:00
"github.com/pagefaultgames/rogueserver/api"
"github.com/pagefaultgames/rogueserver/db"
2023-12-05 13:28:08 -05:00
)
func main() {
2024-04-17 19:31:12 -04:00
// flag stuff
2024-05-08 20:13:22 +02:00
debug, errDebugBoolParse := strconv.ParseBool(os.Getenv("debug"))
if errDebugBoolParse != nil {
log.Fatalf("failed to parse debug value: %s", errDebugBoolParse)
}
proto := "tcp"
addr := "0.0.0.0:8001"
2023-12-05 13:28:08 -05:00
2024-05-08 20:13:22 +02:00
dbuser := os.Getenv("dbuser")
dbpass := os.Getenv("dbpass")
dbproto := "tcp"
dbaddr := os.Getenv("dbaddr")
dbname := os.Getenv("dbname")
2023-12-05 13:28:08 -05:00
flag.Parse()
// register gob types
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
2024-04-17 19:31:12 -04:00
// get database connection
2024-05-08 20:13:22 +02:00
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
2024-05-08 20:13:22 +02:00
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
2024-05-10 13:16:35 -04:00
handler := prodHandler(mux)
if debug {
2024-05-10 13:16:35 -04:00
handler = debugHandler(mux)
2024-04-25 16:13:34 -04:00
} else {
2024-05-14 21:12:01 +02:00
err = http.Serve(listener, handler)
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
}
2024-05-10 13:40:00 -04:00
func prodHandler(router *http.ServeMux) 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", "https://pokerogue.net")
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-04-25 14:41:48 -04:00
func debugHandler(router *http.ServeMux) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-04-25 16:13:34 -04:00
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
2024-04-25 14:41:48 -04:00
router.ServeHTTP(w, r)
})
}