Skip to content
Snippets Groups Projects
Commit 19c1cc9c authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

added gowww project

parent 17a96cb3
No related branches found
No related tags found
No related merge requests found
main
go*
*.pb.go
.DS_Store
......@@ -12,3 +12,5 @@ use ./src/gourl
use ./src/hello-grpc/client
use ./src/hello-grpc/server
use ./src/hello-kafka
use ./src/gowww/client
use ./src/gowww/server
package main
import (
"io"
"log"
"net/http"
)
const (
url = "http://localhost:8080/hellowww"
)
func main() {
log.Printf("Calling service %s\n", url)
resp, err := http.Get(url)
if err != nil {
log.Fatalf("Error reading from %s\n%#v", url, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading from %s\n%#v", url, err)
}
log.Printf("Result: %s", body)
}
module github.com/turngeek/examples-go/src/gowww/client
go 1.20
module github.com/turngeek/examples-go/src/gowww/server
go 1.20
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
type helloWWWRequest struct {
Name string `json:"name"`
}
type helloWWWResponse struct {
Message string `json:"message"`
}
func main() {
port := 8080
r := mux.NewRouter()
r.HandleFunc("/hellowww", helloWWWHandler).Methods("GET")
r.HandleFunc("/hellowww", helloYouHandler).Methods("POST")
log.Printf("Starting server on port %v\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), r))
}
func helloWWWHandler(w http.ResponseWriter, r *http.Request) {
response := helloWWWResponse{Message: "Hello WWW"}
encoder := json.NewEncoder(w)
encoder.Encode(&response)
}
func helloYouHandler(w http.ResponseWriter, r *http.Request) {
var request helloWWWRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
response := helloWWWResponse{Message: "Hello " + request.Name}
encoder := json.NewEncoder(w)
encoder.Encode(&response)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment