diff --git a/README.md b/README.md index 0e7935b..8a44ab0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,64 @@ -# playground-go +# Playground GO +A simple web server implementation in Go that demonstrates basic HTTP server concepts. + +## Overview + +This project implements a basic HTTP server that: +- Listens on a specified address and port +- Handles HTTP requests +- Processes requests concurrently using goroutines + +## Project Structure + +``` +. +├── main.go # Application entry point +├── server/ +│ ├── server.go # Server implementation +│ └── handler.go # HTTP request handlers +├── go.mod # Go module definition +└── README.md # This file +``` + +## Prerequisites + +- Go 1.16 or higher + +## Usage + +1. Import the server package in your Go code: + +```go +import "your-module-name/server" +``` + +2. Create a new server instance: + +```go +server := server.NewServer(":8080") // Listen on port 8080 +``` + +3. Start the server: + +```go +server.ListenAndServe() +``` + +## Features + +- Concurrent request handling using goroutines +- HTTP request processing +- Graceful error handling +- Simple and clean API design + +## Error Handling + +The server includes basic error handling for: +- Address binding errors +- Request processing errors +- HTTP response errors + +## Contributing + +Feel free to submit issues and enhancement requests! diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d0a1d07 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.okseby.com/okseby/playground-go + +go 1.23.5 diff --git a/main.go b/main.go new file mode 100644 index 0000000..7a8b17f --- /dev/null +++ b/main.go @@ -0,0 +1,8 @@ +package main + +import "git.okseby.com/okseby/playground-go/server" + +func main() { + server := server.NewServer("0.0.0.0:7878") + server.ListenAndServe() +} diff --git a/server/handler.go b/server/handler.go new file mode 100644 index 0000000..f9f66be --- /dev/null +++ b/server/handler.go @@ -0,0 +1,30 @@ +package server + +import ( + "bufio" + "fmt" + "io" + "net" +) + +func handleConnection(conn net.Conn) { + defer conn.Close() + + reader := bufio.NewReader(conn) + request, err := reader.ReadString('\n') + if err != nil { + if err != io.EOF { + fmt.Println("read request error:", err) + } + return + } + + fmt.Println("request:", request) + + response := "HTTP/1.1 200 OK\r\n" + + "Content-Length: 13\r\n" + + "Content-Type: text/plain\r\n\r\n" + + "Hello, world!" + + conn.Write([]byte(response)) +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..d14f16a --- /dev/null +++ b/server/server.go @@ -0,0 +1,33 @@ +package server + +import ( + "fmt" + "net" +) + +type Server struct { + addr string +} + +func NewServer(addr string) *Server { + return &Server{addr: addr} +} + +func (s *Server) ListenAndServe() { + listener, err := net.Listen("tcp", s.addr) + if err != nil { + fmt.Println("Error binding to address:", err) + } + defer listener.Close() + + fmt.Printf("Server listening on %s\n", s.addr) + + for { + conn, err := listener.Accept() + if err != nil { + fmt.Println("Error accepting connection:", err) + continue + } + go handleConnection(conn) + } +}