{"id":22097141,"url":"https://github.com/joelteogom/my-http-server","last_synced_at":"2025-03-24T01:19:30.148Z","repository":{"id":263986163,"uuid":"891937944","full_name":"JoelTeoGom/my-http-server","owner":"JoelTeoGom","description":"MyHTTPServer is a custom-built HTTP server written in Go, created as a learning project to understand how HTTP works under the hood. The goal of this project is to explore the core functionality of HTTP servers, such as handling requests, managing routes, and building responses manually, without relying on high-level frameworks.","archived":false,"fork":false,"pushed_at":"2024-11-25T08:46:49.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-29T07:41:30.622Z","etag":null,"topics":["golang","http","http-server","http-server-handler","http-server-middleware"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JoelTeoGom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-21T08:21:18.000Z","updated_at":"2024-11-25T08:53:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"7a046061-8212-4a7e-8d54-f61029e73770","html_url":"https://github.com/JoelTeoGom/my-http-server","commit_stats":null,"previous_names":["joelteogom/my-http-server"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoelTeoGom%2Fmy-http-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoelTeoGom%2Fmy-http-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoelTeoGom%2Fmy-http-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoelTeoGom%2Fmy-http-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JoelTeoGom","download_url":"https://codeload.github.com/JoelTeoGom/my-http-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245191629,"owners_count":20575253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","http","http-server","http-server-handler","http-server-middleware"],"created_at":"2024-12-01T04:14:17.883Z","updated_at":"2025-03-24T01:19:30.129Z","avatar_url":"https://github.com/JoelTeoGom.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MyHTTPServer\n\n## Description\nThis project implements a basic HTTP server written in Go. The server architecture allows handling specific routes with different HTTP methods (GET, POST, etc.) and responds to requests with custom logic defined through handlers. It is a modular and extensible foundation for building HTTP servers in Go.\n\n## Key Features\n- **HTTP Methods Support**: Supports common HTTP methods like GET, POST, PUT, DELETE, PATCH, OPTIONS, and HEAD.\n- **Custom Handlers**: Define custom logic for each route using handlers.\n- **Routing**: Match HTTP requests to specific handlers based on method and path.\n- **Middleware**: Add functionality like logging, authentication, or validation by wrapping handlers.\n- **Concurrency**: Handles multiple client connections using Go’s lightweight goroutines.\n\n## Project Structure\n```\nmyhttp/\n├── myhttp.go    # Core HTTP server logic and routing\nmain.go          # Entry point defining routes and starting the server\n```\n\n### `myhttp.go`\n- **`HttpRequest`**: Represents an HTTP request, including method, URI, version, headers, and body.\n- **`HttpResponse`**: Represents an HTTP response, including status line, headers, and body.\n- **`HandleFunc`**: Type alias for request handlers (`func(req *HttpRequest, res *HttpResponse)`).\n- **`Server`**:\n  - `HandleFunction(method, path, handler)`: Registers a handler for a specific method and path.\n  - `Serve(req)`: Routes the request to the appropriate handler.\n  - `HttpServer(address)`: Starts the server on the specified address (e.g., `0.0.0.0:6969`).\n\n### `main.go`\nDefines routes and handlers using the `myhttp` server implementation.\n\n## Example Usage\n### Starting the Server\nThe server listens on `0.0.0.0:6969`. Example routes are defined in `main.go`:\n```go\nhttp := myhttp.NewServer()\n\nhttp.HandleFunction(myhttp.GET, \"/hello\", func(req *myhttp.HttpRequest, res *myhttp.HttpResponse) {\n    res.Headers[\"Content-Type\"] = \"text/plain\"\n    res.Body = []byte(\"Hello, world from GET handler!\")\n})\n\nhttp.HandleFunction(myhttp.POST, \"/hello\", func(req *myhttp.HttpRequest, res *myhttp.HttpResponse) {\n    res.Headers[\"Content-Type\"] = \"text/plain\"\n    res.Body = []byte(\"Hello, world from POST handler!\")\n})\n\nhttp.HttpServer(\"0.0.0.0:6969\")\n```\n\n### Example Requests\n#### GET `/hello`\n```bash\ncurl -X GET http://localhost:6969/hello\n# Response:\n# Hello, world from GET handler!\n```\n\n#### POST `/hello`\n```bash\ncurl -X POST http://localhost:6969/hello\n# Response:\n# Hello, world from POST handler!\n```\n\n### Middleware Example\nMiddleware can wrap handlers to add functionality like authentication or logging:\n```go\nfunc Middleware(next myhttp.HandleFunc) myhttp.HandleFunc {\n    return func(req *myhttp.HttpRequest, res *myhttp.HttpResponse) {\n        log.Println(\"Request received:\", req.URI)\n        next(req, res)\n    }\n}\n\nhttp.HandleFunction(myhttp.GET, \"/protected\", Middleware(func(req *myhttp.HttpRequest, res *myhttp.HttpResponse) {\n    res.Headers[\"Content-Type\"] = \"text/plain\"\n    res.Body = []byte(\"Protected route accessed!\")\n}))\n```\n\n### Protected Route Example\n#### GET `/protected`\n```bash\ncurl -X GET http://localhost:6969/protected\n# Response:\n# Protected route accessed!\n```\n\n## Future Enhancements\n- **Cookie Handling**: Add functionality to set and retrieve cookies.\n- **Session Management**: Integrate session handling for user authentication.\n- **Enhanced Middleware**: Add support for common middlewares like authentication, logging, and rate limiting.\n- **HTTPS Support**: Add support for serving over TLS.\n- **Error Handling**: Implement robust error handling for edge cases.\n\n## Running the Project\n1. Clone the repository.\n2. Build and run the server:\n   ```bash\n   go run main.go\n   ```\n3. Test the server using tools like `curl` or a web browser.\n\n---\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoelteogom%2Fmy-http-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoelteogom%2Fmy-http-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoelteogom%2Fmy-http-server/lists"}