ThePlaceHolders/server.go

52 lines
1.1 KiB
Go
Raw Normal View History

2024-09-25 18:05:04 -05:00
package main
import (
2024-09-26 01:11:22 -05:00
"crypto/rand"
2024-09-25 18:05:04 -05:00
"fmt"
2024-09-26 01:11:22 -05:00
"log"
2024-09-25 18:05:04 -05:00
"net/http"
2024-09-26 01:11:22 -05:00
"github.com/gorilla/sessions"
2024-09-25 18:05:04 -05:00
)
const ADDRESS = "127.0.0.1"
const PORT = "8080"
2024-09-26 01:11:22 -05:00
type Server struct {
Users map[string]UserData
Sessions *sessions.CookieStore
2024-09-25 19:05:33 -05:00
}
2024-09-26 01:11:22 -05:00
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)
2024-09-25 19:05:33 -05:00
return
}
2024-09-26 01:11:22 -05:00
fmt.Println("Done!")
// Create server object
server := Server{
Users: make(map[string]UserData),
Sessions: sessions.NewCookieStore(secret),
2024-09-25 19:05:33 -05:00
}
2024-09-25 18:05:04 -05:00
// Host static files
static_files := http.FileServer(http.Dir("static/"))
http.Handle("/", static_files)
// Response generated by code
2024-09-26 01:11:22 -05:00
http.HandleFunc("/handle-register", server.handle_register)
http.HandleFunc("/handle-login", server.handle_login)
2024-09-25 18:05:04 -05:00
// Start web server at 127.0.0.1:8080
2024-09-26 01:11:22 -05:00
fmt.Printf("Listening to %s on port %s...\n", ADDRESS, PORT)
2024-09-25 18:05:04 -05:00
e := http.ListenAndServe(ADDRESS+":"+PORT, nil)
// Print any errors
if e != nil {
2024-09-26 01:11:22 -05:00
fmt.Println("Error starting server:")
log.Fatal(e)
2024-09-25 18:05:04 -05:00
}
}