server/main.go
2023-08-09 20:58:38 +02:00

33 lines
639 B
Go

package main
import (
"encoding/json"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
type Message struct { // placeholder
Name string
Body string
}
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.RealIP)
// Set a timeout value on the request context (ctx)
r.Use(middleware.Timeout(60 * time.Second))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
json, err := json.Marshal(Message{"Test", "Hello world"})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("An error has occured"))
}
w.Write(json)
})
}