https://github.com/cybersayak/webserver1stapp
https://github.com/cybersayak/webserver1stapp
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/cybersayak/webserver1stapp
- Owner: Cybersayak
- Created: 2024-07-23T08:40:48.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-09-04T18:22:57.000Z (9 months ago)
- Last Synced: 2024-09-07T03:01:52.134Z (9 months ago)
- Language: Go
- Homepage: replit.com/@SAYAKGHOSH10/WebServer1stApp
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Basic Web Server in Golang
This repository contains a simple web server implementation using Go (Golang). The server responds with a html pages message for every request.
## Prerequisites
Before you begin, ensure you have met the following requirements:
- You have installed [Go](https://golang.org/doc/install) (at least version 1.16).
- You have a working internet connection.## Installation
1. Clone the repository:
```bash
git clone (https://github.com/Cybersayak/WebServer1stApp.git)Sure! Here's a README for your Web Server project that renders HTML pages:
---
## Project Structure
```
.
├── main.go
└── static
├── form.html
└── index.html
```## Files
### `main.go`
This is the entry point of the application. It sets up the HTTP server and routes.
```go
package mainimport (
"fmt"
"log"
"net/http"
)func formHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
}
fmt.Fprint(w, "POST request successful")
name := r.FormValue("name")
address := r.FormValue("address")
email := r.FormValue("email")
fmt.Fprintf(w, "Name = %s\n", name)
fmt.Fprintf(w, "Address = %s\n", address)
fmt.Fprintf(w, "Email = %s\n", email)
}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 not allowed", http.StatusNotFound)
return
}
fmt.Fprint(w, "Hello Commanders")
}func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)
fmt.Printf("Starting server on port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}```
### `static/form.html`
This is the HTML template for the home page.
```html
Static Site in Golang
Name:
Address:
Email:
```
### `static/index.html`
This is the HTML template for the about page.
```html
About Page
Welcome to the About Page
```
## Running the Project
1. Ensure you have Go installed on your machine.
2. Clone the repository or copy the project structure to your local machine.
3. Navigate to the project directory.
4. Run the application using the following command:```sh
go run main.go
```5. Open your web browser and navigate to `http://localhost:8000` to see the home page.
6. Navigate to `http://localhost:8000/about` to see the about page.## Conclusion
This project provides a simple example of how to set up a basic web server in Go with handlers, template rendering, and HTML templates. You can expand this project by adding more routes, templates, and functionality as needed.
---
You can find the project repository [here](https://github.com/Cybersayak/WebServer1stApp).