aboutsummaryrefslogtreecommitdiff
path: root/_examples/simple/main.go
blob: 2248038518ad933ab20bce588c9dd0d6be85de10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/gogeneric/api"
)

func main() {
	h := &http.Server{Addr: "0.0.0.0:3000"}
	mux := http.NewServeMux()
	h.Handler = mux

	// Here is magic!
	mux.Handle("/hello", api.Wrap(handleHello))

	if err := h.ListenAndServe(); err != http.ErrServerClosed {
		log.Fatalln(err)
	}
}

func handleHello(ctx context.Context, req *helloRequest) (*helloResponse, error) {
	return &helloResponse{Message: fmt.Sprintf("Hello, %s!", req.Name)}, nil
}

type helloRequest struct {
	Name string `json:"name"`
}

type helloResponse struct {
	Message string `json:"message"`
}