Skip to content

akshit-git24/WebServer-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Go Web Server Project

Overview

This project is a simple web server built with Go's net/http package. It serves static files, handles a "Hello!" endpoint, and processes form submissions. The server includes a styled HTML form (index.html) in the ./static directory, designed with Tailwind CSS for a modern, responsive user interface.

Features

  • Static File Serving: Serves files from the ./static directory at the root path (/), including index.html with a form.
  • Hello Endpoint: Responds with "Hello!" for GET requests to /hello.
  • Form Handling: Processes POST requests to /form, parsing and returning name and address form data.
  • Port: Runs on localhost:2007.
  • Styled UI: The index.html uses Tailwind CSS and Inter font for a clean, professional look.

Prerequisites

  • Go: Install Go (version 1.16 or later) from golang.org.
  • Browser: Any modern web browser to access the server.
  • Basic Knowledge: Familiarity with Go, HTTP, and HTML is helpful.

Project Structure

go-web-server/
├── main.go          # Go server code
├── static/
│   └── index.html   # HTML file with form and Tailwind CSS

Setup

  1. Create the Project Directory:

    • Create a directory, e.g., go-web-server.
    • Place the main.go file in the root directory.
  2. Set Up the Static Directory:

    • Create a ./static directory in the project root.
    • Save the provided index.html (below) in ./static/index.html.
  3. Example index.html: The index.html file includes a navigation bar, a form for submitting name and address, and a link to the /hello endpoint. It uses Tailwind CSS via CDN and the Inter font.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Go Web Server</title>
        <script src="https://cdn.tailwindcss.com"></script>
        <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
        <style>
            body {
                font-family: 'Inter', sans-serif;
            }
            .hero-bg {
                background: linear-gradient(to right, #6b7280, #1f2937);
            }
        </style>
    </head>
    <body class="bg-gray-100">
        <!-- Navigation -->
        <nav class="bg-white shadow-md sticky top-0 z-50">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="flex justify-between h-16">
                    <div class="flex items-center">
                        <span class="text-2xl font-bold text-gray-800">Go Web Server</span>
                    </div>
                    <div class="flex items-center space-x-4">
                        <a href="/" class="text-gray-600 hover:text-gray-800 px-3 py-2 rounded-md text-sm font-medium">Home</a>
                        <a href="/hello" class="text-gray-600 hover:text-gray-800 px-3 py-2 rounded-md text-sm font-medium">Hello</a>
                    </div>
                </div>
            </div>
        </nav>
    
        <!-- Hero Section with Form -->
        <section class="hero-bg text-white py-20">
            <div class="max-w-lg mx-auto px-4 sm:px-6 lg:px-8">
                <h1 class="text-3xl md:text-4xl font-bold text-center mb-8">Contact Us</h1>
                <form action="/form" method="POST" class="bg-white text-gray-800 p-8 rounded-lg shadow-lg">
                    <div class="space-y-6">
                        <div>
                            <label for="name" class="block text-sm font-medium text-gray-700">Name</label>
                            <input type="text" id="name" name="name" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-gray-500 focus:border-gray-500" placeholder="Your Name" required>
                        </div>
                        <div>
                            <label for="address" class="block text-sm font-medium text-gray-700">Address</label>
                            <input type="text" id="address" name="address" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-gray-500 focus:border-gray-500" placeholder="Your Address" required>
                        </div>
                        <div class="text-center">
                            <button type="submit" class="inline-block bg-gray-800 text-white px-6 py-3 rounded-lg font-semibold hover:bg-gray-700 transition duration-300">Submit</button>
                        </div>
                    </div>
                </form>
                <p class="text-center mt-6 text-lg">Try the <a href="/hello" class="underline hover:text-gray-300">Hello endpoint</a>!</p>
            </div>
        </section>
    
        <!-- Footer -->
        <footer class="bg-gray-800 text-white py-8">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
                <p>© 2025 Go Web Server Project. All rights reserved.</p>
                <div class="mt-4 space-x-4">
                    <a href="#" class="text-gray-400 hover:text-white">Privacy Policy</a>
                    <a href="#" class="text-gray-400 hover:text-white">Terms of Service</a>
                </div>
            </div>
        </footer>
    </body>
    </html>
  4. Install Dependencies:

    • No external Go dependencies are required, as the project uses the standard library (net/http, fmt, log).
    • The index.html uses Tailwind CSS via CDN, so no local installation is needed.

Running the Server

  1. Open a terminal in the project directory.

  2. Run the server:

    go run main.go
  3. The server starts on http://localhost:2007, and you’ll see:

    Starting server at port 2007
    

Usage

  • Root URL:

    • Visit http://localhost:2007/ in a browser to load index.html, which displays a form for submitting name and address.
  • Form Submission:

    • Fill out the form with name and address, then click "Submit". The server responds with:

      POST request successful
      Name: [Your Input]
      Address= [Your Input]
      

      Example: For name=Alice and address=Wonderland, the response is:

      POST request successful
      Name: Alice
      Address= Wonderland
      
  • Hello Endpoint:

    • Click the "Hello endpoint" link or visit http://localhost:2007/hello to see:

      Hello!
      

Code Structure

  • main.go:

    • Defines the server using net/http.
    • Routes:
      • /: Serves static files from ./static (e.g., index.html).
      • /hello: Handles GET requests, returns "Hello!".
      • /form: Handles POST requests, parses and returns form data.
    • Uses http.FileServer, http.HandleFunc, and http.ListenAndServe.
  • static/index.html:

    • A responsive HTML page with a form (action="/form" method="POST") and a link to /hello.
    • Styled with Tailwind CSS and Inter font.

Notes

  • Error Handling: The server includes basic error handling (e.g., 404 for invalid paths/methods, form parsing errors).
  • Security: This is a demo server without HTTPS or CSRF protection. For production, add security features.
  • Static Directory: Ensure ./static and index.html exist, or the root path returns a 404.
  • Port: Uses port 2007. If it’s in use, modify main.go to use another port (e.g., ":8080").

Example Requests

  • GET /hello:

    curl http://localhost:2007/hello

    Response: Hello!

  • POST /form:

    curl -X POST -d "name=John&address=123 Main St" http://localhost:2007/form

    Response:

    POST request successful
    Name: John
    Address= 123 Main St
    

Troubleshooting

  • Server Fails to Start: Check if port 2007 is in use (netstat -an | grep 2007). Change the port in main.go if needed.
  • 404 on Root: Verify ./static/index.html exists.
  • Form Errors: Ensure the form uses POST and includes name and address fields.
  • Styling Issues: Confirm your browser has internet access for the Tailwind CSS CDN.

Future Improvements

  • Add input validation for the form.
  • Implement HTTPS for secure communication.
  • Add CSRF protection for form submissions.
  • Support additional endpoints or dynamic content.
  • Include local CSS instead of relying on a CDN.

License

This project is unlicensed and provided for educational purposes. Feel free to modify and use as needed.

About

Simple web server written in go which uses net/http package

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors