rogueserver/rogueserver.go

110 lines
2.7 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", "network address for api to listen on")
2023-12-05 10:28:08 -08:00
dbuser := flag.String("dbuser", "pokerogue", "database username")
dbpass := flag.String("dbpass", "", "database password")
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
api.Init(mux)
// start web server
2024-04-25 13:13:34 -07:00
if *debug {
2024-04-25 11:41:48 -07:00
err = http.Serve(listener, debugHandler(mux))
2024-04-25 13:13:34 -07:00
} else {
err = http.Serve(listener, mux)
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" {
os.Chmod(addr, 0777)
}
return listener, nil
2024-04-25 11:41:48 -07:00
}
func debugHandler(router *http.ServeMux) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-04-25 13:13:34 -07: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 11:41:48 -07:00
router.ServeHTTP(w, r)
})
}