sample-apps/go/server/main.go (38 lines of code) (raw):
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
func remoteHello(nextServer string) string {
resp, err := http.Get(nextServer)
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = resp.Body.Close()
if err != nil {
log.Fatal(err)
}
return string(body)
}
func hello(w http.ResponseWriter, _ *http.Request) {
url, exists := os.LookupEnv("NEXT_SERVER")
if !exists {
fmt.Fprintf(w, "hello\n")
} else {
fmt.Fprintf(w, "%s\n", remoteHello(url))
}
}
func main() {
http.HandleFunc("/hello", hello)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}