
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 রিকোয়েস্ট হ্যান্ডলিং, টেমপ্লেট রেন্ডারিং এবং একটি বেসিক ফর্ম সাবমিশন সিস্টেম দেখানো হয়েছে। আপনি এই প্রজেক্টটি আরও উন্নত করতে পারেন, যেমন ভ্যালিডেশন যোগ করা, ডাটাবেস সংযোগ করা, অথবা নিরাপত্তা উন্নত করা।