rogueserver/rogueserver.go

137 lines
3.5 KiB
Go
Raw Normal View History

2024-04-29 14:26:46 -07: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 12:32:58 -07:00
2023-12-05 10:28:08 -08:00
package main
import (
"encoding/gob"
2023-12-05 10:28:08 -08:00
"flag"
"log"
"net"
2023-12-05 10:28:08 -08:00
"net/http"
"os"
2023-12-05 10:28:08 -08:00
2024-04-29 12:22:27 -07:00
"github.com/pagefaultgames/rogueserver/api"
"github.com/pagefaultgames/rogueserver/db"
2023-12-05 10:28:08 -08:00
)
func main() {
2024-04-17 16:31:12 -07:00
// flag stuff
2024-04-25 11:41:48 -07:00
debug := flag.Bool("debug", false, "use debug mode")
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")
2023-12-05 10:28:08 -08:00
dbuser := flag.String("dbuser", "pokerogue", "database username")
dbpass := flag.String("dbpass", "pokerogue", "database password")
2023-12-05 10:28:08 -08:00
dbproto := flag.String("dbproto", "tcp", "protocol for database connection")
2023-12-28 19:18:57 -08:00
dbaddr := flag.String("dbaddr", "localhost", "database address")
2023-12-28 17:08:58 -08:00
dbname := flag.String("dbname", "pokeroguedb", "database name")
2023-12-05 10:28:08 -08:00
flag.Parse()
// register gob types
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
2024-04-17 16:31:12 -07:00
// get database connection
2023-12-05 10:28:08 -08:00
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)
if err != nil {
log.Fatalf("failed to create net listener: %s", err)
}
2024-04-17 16:31:12 -07:00
mux := http.NewServeMux()
2024-04-17 16:31:12 -07:00
// init api
2024-05-11 05:06:47 -07:00
if err := api.Init(mux); err != nil {
log.Fatal(err)
}
// start web server
2024-05-10 10:16:35 -07:00
handler := prodHandler(mux)
2024-04-25 13:13:34 -07:00
if *debug {
2024-05-10 10:16:35 -07:00
handler = debugHandler(mux)
}
if *tlscert == "" {
err = http.Serve(listener, handler)
2024-04-25 13:13:34 -07:00
} else {
2024-05-10 10:16:35 -07:00
err = http.ServeTLS(listener, handler, *tlscert, *tlskey)
2024-04-25 11:41:48 -07:00
}
2024-04-19 00:27:47 -07:00
if err != nil {
log.Fatalf("failed to create http server or server errored: %s", err)
2024-04-17 16:31:12 -07: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 05:06:47 -07:00
if err := os.Chmod(addr, 0777); err != nil {
listener.Close()
return nil, err
}
}
return listener, nil
2024-04-25 11:41:48 -07:00
}
2024-05-10 10:40:00 -07:00
func prodHandler(router *http.ServeMux) http.Handler {
2024-04-25 11:41:48 -07:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-05-10 10:40:00 -07: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 13:13:34 -07:00
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
2024-04-25 11:41:48 -07:00
router.ServeHTTP(w, r)
})
}
2024-05-10 10:16:35 -07:00
2024-05-10 10:40:00 -07:00
func debugHandler(router *http.ServeMux) http.Handler {
2024-05-10 10:16:35 -07:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-05-10 10:40:00 -07: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 10:16:35 -07:00
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
router.ServeHTTP(w, r)
})
}