Backend/main.go

54 lines
885 B
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
type response struct {
Field1 string
Field2 string
}
func main() {
test_response := response{
Field1: "hi",
Field2: "there",
}
http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Hi, ", r.Header)
result, err := json.Marshal(test_response)
if err != nil {
log.Fatal(err)
}
w.Write(result)
})
http_server := &http.Server{
Addr: ":8080",
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
log.Fatal(http_server.ListenAndServe())
}()
fmt.Println("Server started! Listening in on ", http_server.Addr)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
<-sigs
fmt.Println("Server stopped!")
os.Exit(1)
}