aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZach Latta <zach@zachlatta.com>2016-03-06 08:00:57 -0500
committerZach Latta <zach@zachlatta.com>2016-03-06 08:00:57 -0500
commitc578e565e2f482b624f2b940fa63c0e26c9c3ba0 (patch)
treee63ff880a20d8039577a2fb1f1d7f10cc8197de6
parent0539dde67b82d8bac5663f956ee7a3dc455fff22 (diff)
downloadsshtron-c578e565e2f482b624f2b940fa63c0e26c9c3ba0.tar.gz
Create HTTP server for website
-rw-r--r--main.go28
1 files changed, 20 insertions, 8 deletions
diff --git a/main.go b/main.go
index 79df826..632dda6 100644
--- a/main.go
+++ b/main.go
@@ -5,13 +5,16 @@ import (
"golang.org/x/crypto/ssh"
"io/ioutil"
"net"
+ "net/http"
"os"
)
const (
- portEnv = "PORT"
+ sshPortEnv = "SSH_PORT"
+ httpPortEnv = "PORT"
- defaultPort = "2022"
+ defaultSshPort = "2022"
+ defaultHttpPort = "3000"
)
func handler(conn net.Conn, gm *GameManager, config *ssh.ServerConfig) {
@@ -62,16 +65,19 @@ func handler(conn net.Conn, gm *GameManager, config *ssh.ServerConfig) {
}
}
-func port() string {
+func port(env, def string) string {
var port string
- if os.Getenv(portEnv) == "" {
- port = defaultPort
+ if os.Getenv(env) == "" {
+ port = def
}
return fmt.Sprintf(":%s", port)
}
func main() {
+ sshPort := port(sshPortEnv, defaultSshPort)
+ httpPort := port(httpPortEnv, defaultHttpPort)
+
// Everyone can login!
config := &ssh.ServerConfig{
NoClientAuth: true,
@@ -93,15 +99,21 @@ func main() {
gm := NewGameManager()
go gm.Run()
+ fmt.Printf(
+ "Listening on port %s for SSH and port %s for HTTP...\n",
+ sshPort,
+ httpPort,
+ )
+
+ go panic(http.ListenAndServe(httpPort, http.FileServer(http.Dir("./static/"))))
+
// Once a ServerConfig has been configured, connections can be
// accepted.
- listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0%s", port()))
+ listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0%s", sshPort))
if err != nil {
panic("failed to listen for connection")
}
- fmt.Printf("Listening on port %s...\n", port())
-
for {
nConn, err := listener.Accept()
if err != nil {
Un proyecto texto-plano.xyz