
This project will help you build a basic HTTPS server using Go's net/http package. The goal is to serve a simple webpage and handle both GET and POST requests, while also leveraging goroutines to handle concurrent requests.
net/http package to handle HTTP requests and responses.net/http package. For security reasons, we'll need an SSL certificate and a private key for HTTPS.package main
import (
"fmt"
"net/http"
"html/template"
"log"
)
// Struct to store form data
type FormData struct {
Name string
Email string
}
// Handler for GET request
func getHandler(w http.ResponseWriter, r *http.Request) {
// Serve a simple HTML form on GET request
tmpl := `
<!DOCTYPE html>
<html>
<head>
<title>Simple Web Server</title>
</head>
<body>
<h1>Submit Your Information</h1>
<form action="/submit" method="POST">
<label for="name">Name: </label><input type="text" name="name" required><br>
<label for="email">Email: </label><input type="email" name="email" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>`
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(tmpl))
}
// Handler for POST request
func postHandler(w http.ResponseWriter, r *http.Request) {
// Parse the form data
err := r.ParseForm()
if err != nil {
http.Error(w, "Error parsing form", http.StatusBadRequest)
return
}
// Create a FormData struct and extract values
formData := FormData{
Name: r.FormValue("name"),
Email: r.FormValue("email"),
}
// Display the submitted data
tmpl := `
<!DOCTYPE html>
<html>
<head>
<title>Form Submission Result</title>
</head>
<body>
<h1>Thank You for Your Submission</h1>
<p><strong>Name:</strong> {{.Name}}</p>
<p><strong>Email:</strong> {{.Email}}</p>
</body>
</html>`
// Parsing the template with the form data
t, err := template.New("result").Parse(tmpl)
if err != nil {
http.Error(w, "Error parsing template", http.StatusInternalServerError)
return
}
// Execute the template with the formData
t.Execute(w, formData)
}
// Main function to set up the server
func main() {
// Route to serve the HTML form (GET)
http.HandleFunc("/", getHandler)
// Route to handle form submissions (POST)
http.HandleFunc("/submit", postHandler)
// Start the server with HTTPS
log.Println("Starting HTTPS server on :8080")
err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil) // Path to your SSL certificate and key
if err != nil {
log.Fatal("Error starting server: ", err)
}
}
getHandler):
getHandler function serves a simple HTML form. When the user navigates to the server's root URL (e.g., https://localhost:8080/), they will be presented with an input form where they can submit their name and email.postHandler):
postHandler function is responsible for handling POST requests made when the user submits the form. It parses the form data, stores it in a struct (FormData), and then renders a response showing the submitted information back to the user.html/template package is used to render dynamic HTML responses. It injects the data (name and email) submitted by the user into the HTML response.http.ListenAndServeTLS function starts an HTTPS server on port 8080. This requires an SSL certificate (cert.pem) and a private key (key.pem).openssl. For production, ensure that you use a valid SSL certificate from a trusted Certificate Authority (CA).openssl command: openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout key.pem -out cert.pemmain.go), and run the following command in the terminal: go run main.go This will start the server at https://localhost:8080.https://localhost:8080/. You should see the form where you can enter your name and email.This is a basic example of a web server using HTTPS in Go. It demonstrates how to handle GET and POST requests, how to serve dynamic content with templates, and how to implement a basic form submission system. You can extend this project by adding features like validation, database integration, or improving security.
āĻāĻ āĻĒā§āϰāĻā§āĻā§āĻā§, āĻāĻĒāύāĻŋ Go āĻāϰ net/http āĻĒā§āϝāĻžāĻā§āĻ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāĻāĻāĻŋ āĻŦā§āϏāĻŋāĻ HTTPS āϏāĻžāϰā§āĻāĻžāϰ āϤā§āϰāĻŋ āĻāϰāĻŦā§āύāĨ¤ āĻŽā§āϞ āϞāĻā§āώā§āϝ āĻšāϞ āĻāĻāĻāĻŋ āϏāĻžāϧāĻžāϰāĻŖ āĻāϝāĻŧā§āĻŦāĻĒā§āĻ āϏāĻžāϰā§āĻ āĻāϰāĻž āĻāĻŦāĻ GET āĻāĻŦāĻ POST āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻāĻā§āϞāĻŋ āĻšā§āϝāĻžāύā§āĻĄāϞ āĻāϰāĻž, āϏā§āĻāϏāĻžāĻĨā§ goroutines āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāύāĻāĻžāϰā§āύā§āĻ āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻāĻā§āϞāĻŋ āĻĒāϰāĻŋāĻāĻžāϞāύāĻž āĻāϰāĻžāĨ¤
net/http āĻĒā§āϝāĻžāĻā§āĻ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠HTTP āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻ āĻāĻŦāĻ āϰā§āϏāĻĒāύā§āϏ āĻšā§āϝāĻžāύā§āĻĄāϞ āĻāϰāĻžāĨ¤net/http āĻĒā§āϝāĻžāĻā§āĻ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰā§āĨ¤ āύāĻŋāϰāĻžāĻĒāϤā§āϤāĻžāϰ āĻāĻžāϰāĻŖā§, HTTPS āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻžāϰ āĻāύā§āϝ SSL āϏāĻžāϰā§āĻāĻŋāĻĢāĻŋāĻā§āĻ āĻāĻŦāĻ āĻĒā§āϰāĻžāĻāĻā§āĻ āĻā§ āĻĒā§āϰāϝāĻŧā§āĻāύ āĻšāĻŦā§āĨ¤package main
import (
"fmt"
"net/http"
"html/template"
"log"
)
// āĻĢāϰā§āĻŽ āĻĄā§āĻāĻž āϏāĻāϰāĻā§āώāĻŖ āĻāϰāĻžāϰ āĻāύā§āϝ āϏā§āĻā§āϰāĻžāĻā§āĻ
type FormData struct {
Name string
Email string
}
// GET āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻā§āϰ āĻāύā§āϝ āĻšā§āϝāĻžāύā§āĻĄāϞāĻžāϰ
func getHandler(w http.ResponseWriter, r *http.Request) {
// GET āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻā§ āĻāĻāĻāĻŋ āϏāĻžāϧāĻžāϰāĻŖ HTML āĻĢāϰā§āĻŽ āϏāϰāĻŦāϰāĻžāĻš āĻāϰāĻž
tmpl := `
<!DOCTYPE html>
<html>
<head>
<title>Simple Web Server</title>
</head>
<body>
<h1>Submit Your Information</h1>
<form action="/submit" method="POST">
<label for="name">Name: </label><input type="text" name="name" required><br>
<label for="email">Email: </label><input type="email" name="email" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>`
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(tmpl))
}
// POST āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻā§āϰ āĻāύā§āϝ āĻšā§āϝāĻžāύā§āĻĄāϞāĻžāϰ
func postHandler(w http.ResponseWriter, r *http.Request) {
// āĻĢāϰā§āĻŽ āĻĄā§āĻāĻž āĻĒāĻžāϰā§āϏ āĻāϰāĻž
err := r.ParseForm()
if err != nil {
http.Error(w, "Error parsing form", http.StatusBadRequest)
return
}
// FormData āϏā§āĻā§āϰāĻžāĻā§āĻ āϤā§āϰāĻŋ āĻāĻŦāĻ āĻŽāĻžāύ āĻŦā§āϰ āĻāϰāĻž
formData := FormData{
Name: r.FormValue("name"),
Email: r.FormValue("email"),
}
// āϏāĻžāĻŦāĻŽāĻŋāĻ āĻāϰāĻž āĻĄā§āĻāĻž āĻĒā§āϰāĻĻāϰā§āĻļāύ āĻāϰāĻž
tmpl := `
<!DOCTYPE html>
<html>
<head>
<title>Form Submission Result</title>
</head>
<body>
<h1>Thank You for Your Submission</h1>
<p><strong>Name:</strong> {{.Name}}</p>
<p><strong>Email:</strong> {{.Email}}</p>
</body>
</html>`
// āĻā§āĻŽāĻĒā§āϞā§āĻ āĻĒāĻžāϰā§āϏ āĻāϰāĻž
t, err := template.New("result").Parse(tmpl)
if err != nil {
http.Error(w, "Error parsing template", http.StatusInternalServerError)
return
}
// āĻā§āĻŽāĻĒā§āϞā§āĻ āĻāĻā§āϏāĻŋāĻāĻŋāĻāĻ āĻāϰāĻž
t.Execute(w, formData)
}
// āĻŽā§āĻāύ āĻĢāĻžāĻāĻļāύ, āϝā§āĻāĻžāύ⧠āϏāĻžāϰā§āĻāĻžāϰ āϏā§āĻāĻāĻĒ āĻāϰāĻž āĻšāĻŦā§
func main() {
// HTML āĻĢāϰā§āĻŽ āϏāĻžāϰā§āĻ āĻāϰāĻžāϰ āĻāύā§āϝ GET āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻ āĻšā§āϝāĻžāύā§āĻĄāϞāĻžāϰ
http.HandleFunc("/", getHandler)
// āĻĢāϰā§āĻŽ āϏāĻžāĻŦāĻŽāĻŋāĻļāύ āĻšā§āϝāĻžāύā§āĻĄāϞ āĻāϰāĻžāϰ āĻāύā§āϝ POST āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻ āĻšā§āϝāĻžāύā§āĻĄāϞāĻžāϰ
http.HandleFunc("/submit", postHandler)
// HTTPS āϏāĻžāϰā§āĻāĻžāϰ āĻāĻžāϞ⧠āĻāϰāĻž
log.Println("Starting HTTPS server on :8080")
err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil) // SSL āϏāĻžāϰā§āĻāĻŋāĻĢāĻŋāĻā§āĻ āĻāĻŦāĻ āĻā§ āĻĒā§āϰāϝāĻŧā§āĻāύ
if err != nil {
log.Fatal("Error starting server: ", err)
}
}
getHandler):
getHandler āĻĢāĻžāĻāĻļāύ āĻāĻāĻāĻŋ āϏāĻžāϧāĻžāϰāĻŖ HTML āĻĢāϰā§āĻŽ āϏāϰāĻŦāϰāĻžāĻš āĻāϰā§āĨ¤ āϝāĻāύ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻāĻžāϰ⧠āϏāĻžāϰā§āĻāĻžāϰā§āϰ āĻŽā§āϞ URL (āϝā§āĻŽāύ https://localhost:8080/) āĻ āϝāĻžāĻŦā§, āϤāĻāύ āĻāĻāĻāĻŋ āĻāύāĻĒā§āĻ āĻĢāϰā§āĻŽ āĻĻā§āĻāĻžāύ⧠āĻšāĻŦā§ āϝā§āĻāĻžāύ⧠āϤāĻžāϰāĻž āϤāĻžāĻĻā§āϰ āύāĻžāĻŽ āĻāĻŦāĻ āĻāĻŽā§āĻāϞ āϏāĻžāĻŦāĻŽāĻŋāĻ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āĨ¤postHandler):
postHandler āĻĢāĻžāĻāĻļāύ POST āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻ āĻšā§āϝāĻžāύā§āĻĄāϞ āĻāϰā§, āϝāĻāύ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻāĻžāϰ⧠āĻĢāϰā§āĻŽ āϏāĻžāĻŦāĻŽāĻŋāĻ āĻāϰā§āĨ¤ āĻāĻāĻŋ āĻĢāϰā§āĻŽ āĻĄā§āĻāĻž āĻĒāĻžāϰā§āϏ āĻāϰ⧠āĻāĻŦāĻ āϤāĻž āĻāĻāĻāĻŋ āϏā§āĻā§āϰāĻžāĻā§āĻ (FormData) āĻ āϏāĻāϰāĻā§āώāĻŖ āĻāϰā§, āϤāĻžāϰāĻĒāϰ āϏā§āĻ āĻĄā§āĻāĻž āĻĻāĻŋāϝāĻŧā§ āĻāĻāĻāĻŋ āϧāύā§āϝāĻŦāĻžāĻĻ āĻŦāĻžāϰā§āϤāĻž āĻĻā§āĻāĻžāϝāĻŧāĨ¤html/template āĻĒā§āϝāĻžāĻā§āĻ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āĻšāϝāĻŧā§āĻā§ āĻĄāĻžāĻāύāĻžāĻŽāĻŋāĻ HTML āϰā§āϏāĻĒāύā§āϏ āϰā§āύā§āĻĄāĻžāϰ āĻāϰāϤā§āĨ¤ āĻāϤ⧠āĻŦā§āϝāĻŦāĻšāĻžāϰāĻāĻžāϰā§āϰ āĻĻā§āĻāϝāĻŧāĻž āύāĻžāĻŽ āĻāĻŦāĻ āĻāĻŽā§āĻāϞ āĻĄā§āĻāĻž HTML āϰā§āϏāĻĒāύā§āϏ⧠āĻāύāĻā§āĻā§āĻ āĻāϰāĻž āĻšāϝāĻŧāĨ¤http.ListenAndServeTLS āĻĢāĻžāĻāĻļāύ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āϏāĻžāϰā§āĻāĻžāϰāĻāĻŋ 8080 āĻĒā§āϰā§āĻā§ HTTPS āĻāĻžāϞ⧠āĻšāϝāĻŧāĨ¤ āĻāĻāĻŋ āĻāĻāĻāĻŋ SSL āϏāĻžāϰā§āĻāĻŋāĻĢāĻŋāĻā§āĻ (cert.pem) āĻāĻŦāĻ āĻĒā§āϰāĻžāĻāĻā§āĻ āĻā§ (key.pem) āĻĒā§āϰāϝāĻŧā§āĻāύāĨ¤openssl āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāĻāĻāĻŋ āϏā§āϞā§āĻĢ-āϏāĻžāĻāύāĻĄ āϏāĻžāϰā§āĻāĻŋāĻĢāĻŋāĻā§āĻ āϤā§āϰāĻŋ āĻāϰāϤ⧠āĻĒāĻžāϰā§āύāĨ¤openssl āĻāĻŽāĻžāύā§āĻĄ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāĻāĻāĻŋ āϏā§āϞā§āĻĢ-āϏāĻžāĻāύāĻĄ āϏāĻžāϰā§āĻāĻŋāĻĢāĻŋāĻā§āĻ āϤā§āϰāĻŋ āĻāϰāϤ⧠āĻĒāĻžāϰā§āύ: openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout key.pem -out cert.pemmain.go) āϏāĻāϰāĻā§āώāĻŖ āĻāϰā§āύ, āϤāĻžāϰāĻĒāϰ āĻāĻžāϰā§āĻŽāĻŋāύāĻžāϞ⧠āĻāĻ āĻāĻŽāĻžāύā§āĻĄāĻāĻŋ āϰāĻžāύ āĻāϰā§āύ: go run main.go āĻāϰ āĻĢāϞ⧠āϏāĻžāϰā§āĻāĻžāϰāĻāĻŋ https://localhost:8080 āĻ āĻŋāĻāĻžāύāĻžāϝāĻŧ āĻāĻžāϞ⧠āĻšāĻŦā§āĨ¤https://localhost:8080/ āĻ āϝāĻžāύāĨ¤ āĻāĻĒāύāĻŋ āĻāĻāĻāĻŋ āĻĢāϰā§āĻŽ āĻĻā§āĻāϤ⧠āĻĒāĻžāĻŦā§āύ āϝā§āĻāĻžāύ⧠āĻāĻĒāύāĻŋ āĻāĻĒāύāĻžāϰ āύāĻžāĻŽ āĻāĻŦāĻ āĻāĻŽā§āĻāϞ āĻāύāĻĒā§āĻ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āύāĨ¤āĻāĻāĻŋ āĻāĻāĻāĻŋ āĻŦā§āϏāĻŋāĻ HTTPS āϏāĻžāϰā§āĻāĻžāϰ āϤā§āϰāĻŋāϰ āĻāĻĻāĻžāĻšāϰāĻŖ āϝāĻž Go-āϤ⧠āϤā§āϰāĻŋ āĻāϰāĻž āĻšāϝāĻŧā§āĻā§āĨ¤ āĻāĻāĻžāύ⧠GET āĻāĻŦāĻ POST āϰāĻŋāĻā§āϝāĻŧā§āϏā§āĻ āĻšā§āϝāĻžāύā§āĻĄāϞāĻŋāĻ, āĻā§āĻŽāĻĒā§āϞā§āĻ āϰā§āύā§āĻĄāĻžāϰāĻŋāĻ āĻāĻŦāĻ āĻāĻāĻāĻŋ āĻŦā§āϏāĻŋāĻ āĻĢāϰā§āĻŽ āϏāĻžāĻŦāĻŽāĻŋāĻļāύ āϏāĻŋāϏā§āĻā§āĻŽ āĻĻā§āĻāĻžāύ⧠āĻšāϝāĻŧā§āĻā§āĨ¤ āĻāĻĒāύāĻŋ āĻāĻ āĻĒā§āϰāĻā§āĻā§āĻāĻāĻŋ āĻāϰāĻ āĻāύā§āύāϤ āĻāϰāϤ⧠āĻĒāĻžāϰā§āύ, āϝā§āĻŽāύ āĻā§āϝāĻžāϞāĻŋāĻĄā§āĻļāύ āϝā§āĻ āĻāϰāĻž, āĻĄāĻžāĻāĻžāĻŦā§āϏ āϏāĻāϝā§āĻ āĻāϰāĻž, āĻ āĻĨāĻŦāĻž āύāĻŋāϰāĻžāĻĒāϤā§āϤāĻž āĻāύā§āύāϤ āĻāϰāĻžāĨ¤