ThePlaceHolders/server.go

59 lines
1.1 KiB
Go

package main
import (
"fmt"
"net/http"
)
const ADDRESS = "127.0.0.1"
const PORT = "8080"
type UserForm struct {
Email string
Password string
}
func extract_user_data(r *http.Request) UserForm {
return UserForm{
Email: r.FormValue("email"),
Password: r.FormValue("password"),
}
}
func handle_login(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
return
}
data := extract_user_data(r)
fmt.Fprintln(w, data.Email)
fmt.Fprintln(w, data.Password)
}
func handle_register(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
return
}
data := extract_user_data(r)
fmt.Fprintln(w, data.Email)
fmt.Fprintln(w, data.Password)
}
func main() {
// Host static files
static_files := http.FileServer(http.Dir("static/"))
http.Handle("/", static_files)
// Response generated by code
http.HandleFunc("/handle-register", handle_register)
http.HandleFunc("/handle-login", handle_login)
// Start web server at 127.0.0.1:8080
e := http.ListenAndServe(ADDRESS+":"+PORT, nil)
// Print any errors
if e != nil {
fmt.Println(e)
} else {
fmt.Println("Started server successfully")
}
}