ThePlaceHolders/server.go

59 lines
1.1 KiB
Go
Raw Normal View History

2024-09-25 18:05:04 -05:00
package main
import (
"fmt"
"net/http"
)
const ADDRESS = "127.0.0.1"
const PORT = "8080"
2024-09-25 19:05:33 -05:00
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)
2024-09-25 18:05:04 -05:00
}
func main() {
// Host static files
static_files := http.FileServer(http.Dir("static/"))
http.Handle("/", static_files)
// Response generated by code
2024-09-25 19:05:33 -05:00
http.HandleFunc("/handle-register", handle_register)
http.HandleFunc("/handle-login", handle_login)
2024-09-25 18:05:04 -05:00
// 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")
}
}