ThePlaceHolders/server.go

52 lines
1.1 KiB
Go

package main
import (
"crypto/rand"
"fmt"
"log"
"net/http"
"github.com/gorilla/sessions"
)
const ADDRESS = "127.0.0.1"
const PORT = "8080"
type Server struct {
Users map[string]UserData
Sessions *sessions.CookieStore
}
func main() {
// Set up encryption for session tokens
fmt.Print("Generating encryption key... ")
secret := make([]byte, 32)
_, err := rand.Read(secret)
if err != nil {
fmt.Println("Error generating key:")
log.Fatal(err)
return
}
fmt.Println("Done!")
// Create server object
server := Server{
Users: make(map[string]UserData),
Sessions: sessions.NewCookieStore(secret),
}
// Host static files
static_files := http.FileServer(http.Dir("static/"))
http.Handle("/", static_files)
// Response generated by code
http.HandleFunc("/handle-register", server.handle_register)
http.HandleFunc("/handle-login", server.handle_login)
// Start web server at 127.0.0.1:8080
fmt.Printf("Listening to %s on port %s...\n", ADDRESS, PORT)
e := http.ListenAndServe(ADDRESS+":"+PORT, nil)
// Print any errors
if e != nil {
fmt.Println("Error starting server:")
log.Fatal(e)
}
}