Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Basic HTTP/1.1 Server in Go

This Go package implements a basic HTTP/1.1 server. It demonstrates how to set up a server, handle connections, and serve HTTP requests.

Table of Contents

Overview

This package provides a simple implementation of an HTTP/1.1 server in Go. The server listens on a specified port, accepts incoming connections, and handles HTTP requests according to the HTTP/1.1 specification.

Installation

To use this package, you need Go installed on your system. You can install it from the [this repo](https://golang.org/dl/.

Clone this repository and build the project:

git clone https://github.com/high-horse/http-1.1-implementation-in-go
cd http-1.1-implementation-in-go
make build

Usage

To start the server, you can use the server executable or import the package into your Go project. Running the Server

Run the server with the following command:

make serve

Code Example

Hereโ€™s a basic example of how to use the package in your own Go application:

package main

import(
	"log"
	
	ht"http1.1/http"
)

func main() {
  // Instanciate new server
	server := ht.NewServer()

  // Register handler functions
	server.Handle("/json", handleJSON)
	server.Handle("/string", handleString)

  //  start the server
	err := server.Init(":8000") 
	if err != nil {
		log.Fatalf("Error starting server: %v", err)
	}
}

// handleString handles requests to the root path ("/string")
func handleString(r *ht.Request) *ht.Response {
	return ht.NewTextResponse(200, "Hello, World!")
}

// handleJSON handles requests to the "/json" path
func handleJSON(request *ht.Request) *ht.Response {
	data := map[string]string{"message": "Hello, JSON!"}
	response, err := ht.NewJSONResponse(200, data)
	if err != nil {
		return ht.NewTextResponse(500, "Internal Server Error")
	}
	return response
}

About

๐ŸŒ GoSimpleHTTP : A minimal HTTP/1.1 server in Go for handling basic web requests and responses.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages