rogueserver/rogueserver.go

111 lines
2.4 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"
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:01:31 +02:00
debug := os.Getenv("debug")
2024-04-25 14:41:48 -04:00
2024-05-08 20:03:57 +02:00
proto = "tcp"
addr = "0.0.0.0:8001"
2023-12-05 13:28:08 -05:00
2024-05-08 20:01:31 +02:00
2024-05-08 20:03:57 +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
2023-12-05 13:28:08 -05: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 19:31:12 -04:00
mux := http.NewServeMux()
2024-04-17 19:31:12 -04:00
// init api
api.Init(mux)
// start web server
2024-04-25 16:13:34 -04:00
if *debug {
2024-04-25 14:41:48 -04:00
err = http.Serve(listener, debugHandler(mux))
2024-04-25 16:13:34 -04:00
} else {
err = http.Serve(listener, mux)
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" {
os.Chmod(addr, 0777)
}
return listener, nil
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)
})
}