# GO 03 - HTTP Server

Here is a simple program that starts an HTTP server and listens for requests on port 8080. When the server receives a request, it checks the path of the request to determine whether to convert a temperature from Fahrenheit to Celsius, or from Celsius to Fahrenheit. The server uses the `fahrenheitToCelsius` and `celsiusToFahrenheit` functions that I provided earlier to perform the conversions:

```go
package main

import (
    "fmt"
    "net/http"
    "strconv"
)

func fahrenheitToCelsius(fahrenheit float64) float64 {
    return (fahrenheit - 32) * 5/9
}

func celsiusToFahrenheit(celsius float64) float64 {
    return celsius*9/5 + 32
}

func temperatureHandler(w http.ResponseWriter, r *http.Request) {
    tempStr := r.URL.Query().Get("temp")
    temp, err := strconv.ParseFloat(tempStr, 64)
    if err != nil {
        http.Error(w, "Invalid temperature", http.StatusBadRequest)
        return
    }

    switch r.URL.Path {
    case "/fahrenheit_to_celsius":
        celsius := fahrenheitToCelsius(temp)
        fmt.Fprintf(w, "%.2f°F is equivalent to %.2f°C", temp, celsius)
    case "/celsius_to_fahrenheit":
        fahrenheit := celsiusToFahrenheit(temp)
        fmt.Fprintf(w, "%.2f°C is equivalent to %.2f°F", temp, fahrenheit)
    default:
        http.Error(w, "Invalid path", http.StatusBadRequest)
    }
}

func main() {
    http.HandleFunc("/fahrenheit_to_celsius", temperatureHandler)
    http.HandleFunc("/celsius_to_fahrenheit", temperatureHandler)
    http.ListenAndServe(":8080", nil)
}
```

To use this program, save it to a file called `temp_server.go`, and then run it using the `go run` command:

`$ go run temp_server.go`

The server will now be running and listening for requests on port 8080. To convert a temperature from Fahrenheit to Celsius, send a GET request to [`http://localhost:8080/fahrenheit_to_celsius?temp=100.0`](http://localhost:8080/fahrenheit_to_celsius?temp=100.0). To convert a temperature from Celsius to Fahrenheit, send a GET request to [`http://localhost:8080/celsius_to_fahrenheit?temp=37.5`](http://localhost:8080/celsius_to_fahrenheit?temp=37.5).

For example, you can use the `curl` command to send a request to the server:

```plaintext
$ curl "http://localhost:8080/fahrenheit_to_celsius?temp=100.0"
100.00°F is equivalent to 37.78°C 
$ curl "http://localhost:8080/celsius_to_fahrenheit?temp=37.5"
37.50°C is equivalent to 99.50°F
```
