Are you a beginner looking to learn about building a basic web server using the Go programming language? In this tutorial, we'll walk you through creating a straightforward web server that serves static files and handles basic HTTP requests. By the end of this tutorial, you'll have a solid understanding of the key concepts involved in building a web server with Go.
What is a Web Server?
A web server is a software application that handles incoming requests from clients (typically web browsers) and responds by serving web content. When you type a URL into your web browser and hit Enter, your browser sends a request to a web server, which processes the request and sends back the appropriate response. This response can be a web page, an image, a file, or even dynamic content generated by the server.
Web servers play a crucial role in the architecture of the World Wide Web, as they facilitate the exchange of information between clients and servers. They are fundamental building blocks for serving websites, web applications, and other online services.
Prerequisites
Before you begin, make sure you have the following:
- Basic understanding of Go programming concepts.
You can refer to this repo for Basics: https://github.com/ChetanThapliyal/get-started-with-Go/tree/main OR Read the Go blog Series: https://mechanical2devops.hashnode.dev/series/learn-golang
- Go programming language installed on your machine. You can download and install Go from the official website: https://golang.org/dl/
Getting Started
Let's start by creating a simple web server that serves static HTML files and handles HTTP requests. Follow these steps:
Step 1: Setting Up the Project
- Create a new directory for your project:
COPY
COPY
mkdir WebServer-with-Go cd WebServer-with-Go
- Inside the project directory, create a subdirectory named
static
: COPY COPYmkdir static
- Create two HTML files named
index.html
andform.html
inside thestatic
directory. You can use any text editor to create these files and add basic HTML content. COPY COPYcd static touch index.html form.html hello.html
Visit this to populate the above files (copy the code from the link mentioned below and paste in the above files): form.html : https://github.com/ChetanThapliyal/WebServer-wih-Go/blob/main/static/form.html index.html: https://github.com/ChetanThapliyal/WebServer-wih-Go/blob/main/static/index.html hello.html : https://github.com/ChetanThapliyal/WebServer-wih-Go/blob/main/static/hello.html
- Use
tree
command to check directory structure: COPY COPYtree WebServer-with-Go/ ├── main.go └── static ├── form.html ├── hello.html └── index.html
Step 2: Writing the Go Code
- Create a file named
main.go
in the project directory. - Open
main.go
in a text editor and copy the following code: COPY COPYpackage main import ( "fmt" "log" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/hello" { http.Error(w, "404 Not Found", http.StatusNotFound) return } if r.Method != "GET" { http.Error(w, "Method is not supported", http.StatusNotFound) return } fmt.Fprintf(w, "Hello there") } func formHandler(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { fmt.Fprintf(w, "ParseForm() err: %v", err) return } fmt.Fprintf(w, "POST Request Successful\n") name := r.FormValue("name") address := r.FormValue("address") fmt.Fprintf(w, "Name = %s\n", name) fmt.Fprintf(w, "Address = %s", address) } func main() { fileServer := http.FileServer(http.Dir("./static")) http.Handle("/", fileServer) http.HandleFunc("/form", formHandler) http.HandleFunc("/hello", helloHandler) fmt.Printf("Starting server at port 8080\n") if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) } }
Step 3: Running the Web Server
- Open a terminal window and navigate to your project directory.
- Run the following command to build and run the web server:
COPY
COPY
go run main.go
- Open your web browser and visit http://localhost:8080 to see your web server in action. You should be able to see the following: localhost:8080 localhost:8080/hello.html localhost:8080/form.html
General Understanding of the Code
Let's break down the key components of the code you just created:
-
Importing Packages: We import necessary packages like
fmt
: Package for formatted I/O (used for printing messages).log
: Package for logging.net/http
Package for building HTTP servers & for handling HTTP requests and responses. -
Handlers: We define two handler functions,
helloHandler
: Handles requests to the "/hello" route. It checks if the URL path is "/hello" and if the HTTP method is "GET". If not, it returns a 404 error. If everything is in order, it responds with "Hello there".formHandler
: Handles POST requests to the "/form" route. It parses the form data from the request, extracts the values for the "name" and "address" fields, and then responds with a success message along with the extracted data. to handle specific paths and HTTP methods. -
Serving Static Files: We create a file server to serve static files from the
static
directory usinghttp.FileServer
. -
Route Handling: We use
http.HandleFunc
to associate our handler functions with specific routes, such as/hello
and/form
. -
Listening and Serving: We use
http.ListenAndServe
to start the web server on port 8080. If an error occurs, we log it.
Detailed Understanding of Functions
helloHandler
-
func helloHandler(w http.ResponseWriter, r *http.Request)
: This line defines the function with two parameters:-
w http.ResponseWriter
: This parameter allows you to write the HTTP response back to the client. -
r *http.Request
: This parameter holds information about the incoming HTTP request.
-
-
if r.URL.Path != "/hello" { ... }
: This line checks whether the URL path of the incoming request (r.URL.Path
) is not equal to "/hello". If it's not, it means the request is not intended for the "/hello" route. In that case, the server responds with a "404 Not Found" error and returns early. -
if r.Method != "GET" { ... }
: This line checks whether the HTTP method used in the request (r.Method
) is not "GET". If the method is not "GET", it means that the "/hello" route only supports GET requests. If another method is used (like POST or PUT), the server responds with a "Method is not supported" error and returns early. -
fmt.Fprintf(w, "Hello there")
: If the URL path is "/hello" and the HTTP method is "GET", this line writes the response "Hello there" to thew
(ResponseWriter) parameter, which sends the response back to the client.
formHandler
-
if err := r.ParseForm(); err != nil { ... }
: This line parses the form data from the incoming request (r
) and populates ther.Form
map with the form values. If there's an error during parsing, it means the form data is not correctly structured, and the server responds with an error message indicating the parsing error. -
fmt.Fprintf(w, "POST Request Successful\n")
: After successfully parsing the form data, the server writes "POST Request Successful" to the response. This indicates that the form submission was received and processed successfully. -
name := r.FormValue("name")
andaddress := r.FormValue("address")
: These lines extract the values of the "name" and "address" fields from the parsed form data. Ther.FormValue
function is used to retrieve the value associated with a specific form key. -
fmt.Fprintf(w, "Name = %s\n", name)
andfmt.Fprintf(w, "Address = %s", address)
: These lines write the extracted "name" and "address" values to the response. The%s
placeholder is replaced with the actual values.
main()
-
fileServer := http.FileServer(http.Dir("./static"))
: This line creates an HTTP file server (fileServer
) that serves static files from the "./static" directory. Thehttp.Dir("./static")
part indicates the directory from which the files will be served. -
http.Handle("/", fileServer)
: This line sets up a handler for the root ("/") route. Any request to the root route will be served by thefileServer
created in the previous step, meaning that static files from the "./static" directory will be served when you access the root URL. -
http.HandleFunc("/form", formHandler)
: This line sets up a handler for the "/form" route. When a request is made to the "/form" route, theformHandler
function we discussed earlier will be called to handle the request. -
http.HandleFunc("/hello", helloHandler)
: Similarly, this line sets up a handler for the "/hello" route. Requests to the "/hello" route will be handled by thehelloHandler
function. -
fmt.Printf("Starting server at port 8080\n")
: This line prints a message indicating that the server is starting. You'll see this message in the console when you run the program. -
if err := http.ListenAndServe(":8080", nil); err != nil { ... }
: This line starts the HTTP server on port 8080. Thehttp.ListenAndServe(":8080", nil)
function call does the following:- It listens for incoming HTTP requests on port 8080.
- It uses the handlers you set up earlier (for "/", "/form", and "/hello") to route and handle the requests.
If there's an error during server startup or while serving requests, the log.Fatal(err)
line will print the error and terminate the program.
Usage
When this program is executed, it starts an HTTP server on port 8080. Here's what it does for different routes:
- Visiting the root ("/") route serves static files from the "./static" directory.
- Accessing the "/form" route displays a simple HTML form. Upon submitting the form, it processes the data and displays the "name" and "address" values.
- Accessing the "/hello" route responds with "Hello there".
Conclusion
Congratulations! You've successfully created a simple web server using the Go programming language. You've learned about serving static files, handling different HTTP requests, and setting up routes. This project provides a foundation for building more complex web applications and APIs using Go. Feel free to explore and expand upon this project to learn more about web development with Go.
Remember, practice makes perfect! Experiment with the code, add new features and explore the Go documentation to deepen your understanding of web development and the Go programming language. Happy coding!
This article was written by Chetan Thapliyal
Top comments (2)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.