Simple Web Server in Go

List Topics
February 18, 2025
No Comments
8 min read

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.

Core Concepts:

  • HTTP Handling: Using the net/http package to handle HTTP requests and responses.
  • Goroutines: Handling requests concurrently for better performance and responsiveness.

Project Overview:

  1. Serve a Simple Webpage: We will create a basic HTML webpage that will be served by our HTTPS server.
  2. Handle GET and POST Requests: The server will handle GET requests to display the webpage and POST requests to accept data from the user.
  3. Concurrency with Goroutines: To handle multiple requests simultaneously, we will use Go's goroutines, which allow for concurrent execution.

Step-by-Step Code Implementation:

  1. Setting up an HTTPS server: We'll create a simple HTTPS server using the net/http package. For security reasons, we'll need an SSL certificate and a private key for HTTPS.
  2. Handling GET and POST Requests:
    • GET: When the user accesses the root of the server, the server will return an HTML form.
    • POST: When the form is submitted, the server will process the data and display a response.

Full Code Example:

Go
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)
	}
}

Explanation:

  1. HTTP GET Handler (getHandler):
    • The 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.
  2. HTTP POST Handler (postHandler):
    • The 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.
  3. Template Rendering:
    • Go's 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.
  4. HTTPS Server Setup:
    • The http.ListenAndServeTLS function starts an HTTPS server on port 8080. This requires an SSL certificate (cert.pem) and a private key (key.pem).
    • Note: For testing purposes, you can create self-signed certificates using openssl. For production, ensure that you use a valid SSL certificate from a trusted Certificate Authority (CA).

Running the Server:

  1. Create SSL Certificates (for local testing): If you don't already have SSL certificates, you can generate a self-signed certificate using the following openssl command: openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout key.pem -out cert.pem
  2. Run the Server: Save the above Go code in a file (e.g., main.go), and run the following command in the terminal: go run main.go This will start the server at https://localhost:8080.
  3. Testing:
    • Open your browser and navigate to https://localhost:8080/. You should see the form where you can enter your name and email.
    • After submitting the form, the server will show a thank-you message with the data you submitted.

Conclusion:

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 āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĻ•āύāĻ•āĻžāϰ⧇āĻ¨ā§āϟ āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟāϗ⧁āϞāĻŋ āĻĒāϰāĻŋāϚāĻžāϞāύāĻž āĻ•āϰāĻžāĨ¤

āĻŽā§‚āϞ āĻ•āύāϏ⧇āĻĒā§āϟ:

  • HTTP āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞāĻŋāĻ‚: net/http āĻĒā§āϝāĻžāϕ⧇āϜ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ HTTP āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻāĻŦāĻ‚ āϰ⧇āϏāĻĒāĻ¨ā§āϏ āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞ āĻ•āϰāĻžāĨ¤
  • Goroutines: āĻāĻ•āĻžāϧāĻŋāĻ• āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻāĻ•āϏāĻžāĻĨ⧇ (āĻ•āύāĻ•āĻžāϰ⧇āĻ¨ā§āϟāϞāĻŋ) āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ goroutines āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻžāĨ¤

āĻĒā§āϰāĻœā§‡āĻ•ā§āĻŸā§‡āϰ āϏāĻžāϰāĻžāĻ‚āĻļ:

  1. āĻāĻ•āϟāĻŋ āϏāĻžāϧāĻžāϰāĻŖ āĻ“āϝāĻŧ⧇āĻŦāĻĒ⧇āϜ āϏāĻžāĻ°ā§āĻ­ āĻ•āϰāĻž: āĻāĻ•āϟāĻŋ āĻŦ⧇āϏāĻŋāĻ• HTML āĻĢāĻ°ā§āĻŽ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āϝāĻž āφāĻŽāĻžāĻĻ⧇āϰ HTTPS āϏāĻžāĻ°ā§āĻ­āĻžāϰ āĻĻā§āĻŦāĻžāϰāĻž āϏāĻžāĻ°ā§āĻ­ āĻ•āϰāĻž āĻšāĻŦ⧇āĨ¤
  2. GET āĻāĻŦāĻ‚ POST āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞāĻŋāĻ‚: āϏāĻžāĻ°ā§āĻ­āĻžāϰāϟāĻŋ GET āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āĻŸā§‡āϰ āĻŽāĻžāĻ§ā§āϝāĻŽā§‡ āĻ“āϝāĻŧ⧇āĻŦāĻĒ⧇āϜāϟāĻŋ āĻĒā§āϰāĻĻāĻ°ā§āĻļāύ āĻ•āϰāĻŦ⧇ āĻāĻŦāĻ‚ POST āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āĻŸā§‡āϰ āĻŽāĻžāĻ§ā§āϝāĻŽā§‡ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻ•āĻžāϰ⧀āϰ āϤāĻĨā§āϝ āĻ—ā§āϰāĻšāĻŖ āĻ•āϰāĻŦ⧇āĨ¤
  3. āĻ—ā§‹āϰ⧁āϟāĻŋāύ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĻ•āύāĻ•āĻžāϰ⧇āĻ¨ā§āϟ āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞāĻŋāĻ‚: āĻāĻ•āĻžāϧāĻŋāĻ• āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻāĻ•āϏāĻžāĻĨ⧇ āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ Go āĻāϰ goroutines āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšāĻŦ⧇āĨ¤

āϕ⧋āĻĄ āωāĻĻāĻžāĻšāϰāĻŖ:

  1. HTTPS āϏāĻžāĻ°ā§āĻ­āĻžāϰ āϏ⧇āϟāφāĻĒ: āφāĻŽāϰāĻž āĻāĻ•āϟāĻŋ āϏāĻžāϧāĻžāϰāĻŖ HTTPS āϏāĻžāĻ°ā§āĻ­āĻžāϰ āϤ⧈āϰāĻŋ āĻ•āϰāĻŦ net/http āĻĒā§āϝāĻžāϕ⧇āϜ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇āĨ¤ āύāĻŋāϰāĻžāĻĒāĻ¤ā§āϤāĻžāϰ āĻ•āĻžāϰāϪ⧇, HTTPS āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ SSL āϏāĻžāĻ°ā§āϟāĻŋāĻĢāĻŋāϕ⧇āϟ āĻāĻŦāĻ‚ āĻĒā§āϰāĻžāχāϭ⧇āϟ āϕ⧀ āĻĒā§āϰāϝāĻŧā§‹āϜāύ āĻšāĻŦ⧇āĨ¤
  2. GET āĻāĻŦāĻ‚ POST āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞāĻŋāĻ‚:
    • GET: āϝāĻ–āύ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻ•āĻžāϰ⧀ āϏāĻžāĻ°ā§āĻ­āĻžāϰ⧇āϰ āĻŽā§‚āϞ URL āĻ āĻĒā§āϰāĻŦ⧇āĻļ āĻ•āϰāĻŦ⧇, āϤāĻ–āύ āĻāĻ•āϟāĻŋ HTML āĻĢāĻ°ā§āĻŽ āĻĒā§āϰāĻĻāĻ°ā§āĻļāĻŋāϤ āĻšāĻŦ⧇āĨ¤
    • POST: āϝāĻ–āύ āĻĢāĻ°ā§āĻŽāϟāĻŋ āϏāĻžāĻŦāĻŽāĻŋāϟ āĻšāĻŦ⧇, āϏāĻžāĻ°ā§āĻ­āĻžāϰāϟāĻŋ āĻĄā§‡āϟāĻž āĻĒā§āϰāĻ•ā§āϰāĻŋāϝāĻŧāĻž āĻ•āϰāĻŦ⧇ āĻāĻŦāĻ‚ āĻāĻ•āϟāĻŋ āϰ⧇āϏāĻĒāĻ¨ā§āϏ āĻĒā§āϰāĻĻāĻ°ā§āĻļāύ āĻ•āϰāĻŦ⧇āĨ¤

āϏāĻŽā§āĻĒā§‚āĻ°ā§āĻŖ āϕ⧋āĻĄ āωāĻĻāĻžāĻšāϰāĻŖ:

Go
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)
	}
}

āĻŦā§āϝāĻžāĻ–ā§āϝāĻž:

  1. HTTP GET āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞāĻžāϰ (getHandler):
    • getHandler āĻĢāĻžāĻ‚āĻļāύ āĻāĻ•āϟāĻŋ āϏāĻžāϧāĻžāϰāĻŖ HTML āĻĢāĻ°ā§āĻŽ āϏāϰāĻŦāϰāĻžāĻš āĻ•āϰ⧇āĨ¤ āϝāĻ–āύ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻ•āĻžāϰ⧀ āϏāĻžāĻ°ā§āĻ­āĻžāϰ⧇āϰ āĻŽā§‚āϞ URL (āϝ⧇āĻŽāύ https://localhost:8080/) āĻ āϝāĻžāĻŦ⧇, āϤāĻ–āύ āĻāĻ•āϟāĻŋ āχāύāĻĒ⧁āϟ āĻĢāĻ°ā§āĻŽ āĻĻ⧇āĻ–āĻžāύ⧋ āĻšāĻŦ⧇ āϝ⧇āĻ–āĻžāύ⧇ āϤāĻžāϰāĻž āϤāĻžāĻĻ⧇āϰ āύāĻžāĻŽ āĻāĻŦāĻ‚ āχāĻŽā§‡āχāϞ āϏāĻžāĻŦāĻŽāĻŋāϟ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āĨ¤
  2. HTTP POST āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞāĻžāϰ (postHandler):
    • postHandler āĻĢāĻžāĻ‚āĻļāύ POST āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞ āĻ•āϰ⧇, āϝāĻ–āύ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻ•āĻžāϰ⧀ āĻĢāĻ°ā§āĻŽ āϏāĻžāĻŦāĻŽāĻŋāϟ āĻ•āϰ⧇āĨ¤ āĻāϟāĻŋ āĻĢāĻ°ā§āĻŽ āĻĄā§‡āϟāĻž āĻĒāĻžāĻ°ā§āϏ āĻ•āϰ⧇ āĻāĻŦāĻ‚ āϤāĻž āĻāĻ•āϟāĻŋ āĻ¸ā§āĻŸā§āϰāĻžāĻ•ā§āϟ (FormData) āĻ āϏāĻ‚āϰāĻ•ā§āώāĻŖ āĻ•āϰ⧇, āϤāĻžāϰāĻĒāϰ āϏ⧇āχ āĻĄā§‡āϟāĻž āĻĻāĻŋāϝāĻŧ⧇ āĻāĻ•āϟāĻŋ āϧāĻ¨ā§āϝāĻŦāĻžāĻĻ āĻŦāĻžāĻ°ā§āϤāĻž āĻĻ⧇āĻ–āĻžāϝāĻŧāĨ¤
  3. āĻŸā§‡āĻŽāĻĒā§āϞ⧇āϟ āϰ⧇āĻ¨ā§āĻĄāĻžāϰāĻŋāĻ‚:
    • Go āĻāϰ html/template āĻĒā§āϝāĻžāϕ⧇āϜ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšāϝāĻŧ⧇āϛ⧇ āĻĄāĻžāχāύāĻžāĻŽāĻŋāĻ• HTML āϰ⧇āϏāĻĒāĻ¨ā§āϏ āϰ⧇āĻ¨ā§āĻĄāĻžāϰ āĻ•āϰāϤ⧇āĨ¤ āĻāϤ⧇ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻ•āĻžāϰ⧀āϰ āĻĻ⧇āĻ“āϝāĻŧāĻž āύāĻžāĻŽ āĻāĻŦāĻ‚ āχāĻŽā§‡āχāϞ āĻĄā§‡āϟāĻž HTML āϰ⧇āϏāĻĒāĻ¨ā§āϏ⧇ āχāύāĻœā§‡āĻ•ā§āϟ āĻ•āϰāĻž āĻšāϝāĻŧāĨ¤
  4. HTTPS āϏāĻžāĻ°ā§āĻ­āĻžāϰ āϏ⧇āϟāφāĻĒ:
    • http.ListenAndServeTLS āĻĢāĻžāĻ‚āĻļāύ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āϏāĻžāĻ°ā§āĻ­āĻžāϰāϟāĻŋ 8080 āĻĒā§‹āĻ°ā§āĻŸā§‡ HTTPS āϚāĻžāϞ⧁ āĻšāϝāĻŧāĨ¤ āĻāϟāĻŋ āĻāĻ•āϟāĻŋ SSL āϏāĻžāĻ°ā§āϟāĻŋāĻĢāĻŋāϕ⧇āϟ (cert.pem) āĻāĻŦāĻ‚ āĻĒā§āϰāĻžāχāϭ⧇āϟ āϕ⧀ (key.pem) āĻĒā§āϰāϝāĻŧā§‹āϜāύāĨ¤
    • āύ⧋āϟ: āĻĒāϰ⧀āĻ•ā§āώāĻžāϰ āϜāĻ¨ā§āϝ āφāĻĒāύāĻŋ openssl āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĻāĻ•āϟāĻŋ āϏ⧇āĻ˛ā§āĻĢ-āϏāĻžāχāύāĻĄ āϏāĻžāĻ°ā§āϟāĻŋāĻĢāĻŋāϕ⧇āϟ āϤ⧈āϰāĻŋ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āύāĨ¤

āϏāĻžāĻ°ā§āĻ­āĻžāϰ āϚāĻžāϞāĻžāύ⧋:

  1. SSL āϏāĻžāĻ°ā§āϟāĻŋāĻĢāĻŋāϕ⧇āϟ āϤ⧈āϰāĻŋ āĻ•āϰāĻž (āϞ⧋āĻ•āĻžāϞ āĻĒāϰ⧀āĻ•ā§āώāĻžāϰ āϜāĻ¨ā§āϝ): āϝāĻĻāĻŋ āφāĻĒāύāĻžāϰ SSL āϏāĻžāĻ°ā§āϟāĻŋāĻĢāĻŋāϕ⧇āϟ āύāĻž āĻĨāĻžāϕ⧇, āϤāĻžāĻšāϞ⧇ āφāĻĒāύāĻŋ āύāĻŋāĻŽā§āύāϞāĻŋāĻ–āĻŋāϤ openssl āĻ•āĻŽāĻžāĻ¨ā§āĻĄ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĻāĻ•āϟāĻŋ āϏ⧇āĻ˛ā§āĻĢ-āϏāĻžāχāύāĻĄ āϏāĻžāĻ°ā§āϟāĻŋāĻĢāĻŋāϕ⧇āϟ āϤ⧈āϰāĻŋ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āύ: openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout key.pem -out cert.pem
  2. āϏāĻžāĻ°ā§āĻ­āĻžāϰ āϚāĻžāϞāĻžāύ⧋: āωāĻĒāϰ⧇āϰ Go āϕ⧋āĻĄāϟāĻŋ āĻāĻ•āϟāĻŋ āĻĢāĻžāχāϞ⧇ (āϝ⧇āĻŽāύ main.go) āϏāĻ‚āϰāĻ•ā§āώāĻŖ āĻ•āϰ⧁āύ, āϤāĻžāϰāĻĒāϰ āϟāĻžāĻ°ā§āĻŽāĻŋāύāĻžāϞ⧇ āĻāχ āĻ•āĻŽāĻžāĻ¨ā§āĻĄāϟāĻŋ āϰāĻžāύ āĻ•āϰ⧁āύ: go run main.go āĻāϰ āĻĢāϞ⧇ āϏāĻžāĻ°ā§āĻ­āĻžāϰāϟāĻŋ https://localhost:8080 āĻ āĻŋāĻ•āĻžāύāĻžāϝāĻŧ āϚāĻžāϞ⧁ āĻšāĻŦ⧇āĨ¤
  3. āĻŸā§‡āĻ¸ā§āϟāĻŋāĻ‚:
    • āĻŦā§āϰāĻžāωāϜāĻžāϰ⧇ āĻ—āĻŋāϝāĻŧ⧇ https://localhost:8080/ āĻ āϝāĻžāύāĨ¤ āφāĻĒāύāĻŋ āĻāĻ•āϟāĻŋ āĻĢāĻ°ā§āĻŽ āĻĻ⧇āĻ–āϤ⧇ āĻĒāĻžāĻŦ⧇āύ āϝ⧇āĻ–āĻžāύ⧇ āφāĻĒāύāĻŋ āφāĻĒāύāĻžāϰ āύāĻžāĻŽ āĻāĻŦāĻ‚ āχāĻŽā§‡āχāϞ āχāύāĻĒ⧁āϟ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āύāĨ¤
    • āĻĢāĻ°ā§āĻŽ āϏāĻžāĻŦāĻŽāĻŋāϟ āĻ•āϰāĻžāϰ āĻĒāϰ, āϏāĻžāĻ°ā§āĻ­āĻžāϰāϟāĻŋ āϧāĻ¨ā§āϝāĻŦāĻžāĻĻ āϜāĻžāύāĻŋāϝāĻŧ⧇ āĻāĻ•āϟāĻŋ āĻŦāĻžāĻ°ā§āϤāĻž āĻĒā§āϰāĻĻāĻ°ā§āĻļāύ āĻ•āϰāĻŦ⧇ āĻāĻŦāĻ‚ āĻŦā§āϝāĻŦāĻšāĻžāϰāĻ•āĻžāϰ⧀āϰ āĻĻ⧇āĻ“āϝāĻŧāĻž āĻĄā§‡āϟāĻž āĻĻ⧇āĻ–āĻžāĻŦ⧇āĨ¤

āωāĻĒāϏāĻ‚āĻšāĻžāϰ:

āĻāϟāĻŋ āĻāĻ•āϟāĻŋ āĻŦ⧇āϏāĻŋāĻ• HTTPS āϏāĻžāĻ°ā§āĻ­āĻžāϰ āϤ⧈āϰāĻŋāϰ āωāĻĻāĻžāĻšāϰāĻŖ āϝāĻž Go-āϤ⧇ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āĻšāϝāĻŧ⧇āϛ⧇āĨ¤ āĻāĻ–āĻžāύ⧇ GET āĻāĻŦāĻ‚ POST āϰāĻŋāϕ⧋āϝāĻŧ⧇āĻ¸ā§āϟ āĻšā§āϝāĻžāĻ¨ā§āĻĄāϞāĻŋāĻ‚, āĻŸā§‡āĻŽāĻĒā§āϞ⧇āϟ āϰ⧇āĻ¨ā§āĻĄāĻžāϰāĻŋāĻ‚ āĻāĻŦāĻ‚ āĻāĻ•āϟāĻŋ āĻŦ⧇āϏāĻŋāĻ• āĻĢāĻ°ā§āĻŽ āϏāĻžāĻŦāĻŽāĻŋāĻļāύ āϏāĻŋāĻ¸ā§āĻŸā§‡āĻŽ āĻĻ⧇āĻ–āĻžāύ⧋ āĻšāϝāĻŧ⧇āϛ⧇āĨ¤ āφāĻĒāύāĻŋ āĻāχ āĻĒā§āϰāĻœā§‡āĻ•ā§āϟāϟāĻŋ āφāϰāĻ“ āωāĻ¨ā§āύāϤ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧇āύ, āϝ⧇āĻŽāύ āĻ­ā§āϝāĻžāϞāĻŋāĻĄā§‡āĻļāύ āϝ⧋āĻ— āĻ•āϰāĻž, āĻĄāĻžāϟāĻžāĻŦ⧇āϏ āϏāĻ‚āϝ⧋āĻ— āĻ•āϰāĻž, āĻ…āĻĨāĻŦāĻž āύāĻŋāϰāĻžāĻĒāĻ¤ā§āϤāĻž āωāĻ¨ā§āύāϤ āĻ•āϰāĻžāĨ¤

Š2025 Linux Bangla | Developed & Maintaind by Linux Bangla.