-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
129 lines (118 loc) · 2.87 KB
/
server.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
// Greeting greeting
type Greeting struct {
Message string `json:"message"`
Name string `json:"name"`
}
// ErrorResponse error response
type ErrorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
// HelloService hello service
func HelloService(ctx context.Context, name string, tm time.Time) (*Greeting, error) {
if name == "" {
return &Greeting{
Message: "hello!",
Name: "anonymous",
}, nil
} else if name == "8maki" {
var msg string
noon := time.Date(tm.Year(), tm.Month(), tm.Day(), 12, 0, 0, 0, time.Local)
evening := time.Date(tm.Year(), tm.Month(), tm.Day(), 18, 0, 0, 0, time.Local)
switch {
case tm.Before(noon):
msg = "good morning"
case tm.After(noon) && tm.Before(evening):
msg = "good afternoon"
case tm.After(evening):
msg = "good evening"
}
return &Greeting{
Message: msg,
Name: "my boss",
}, nil
} else if name == "moqada" {
return &Greeting{
Message: "sup",
Name: "my man",
}, nil
}
return &Greeting{
Message: "hello",
Name: name,
}, nil
}
// Greeting greeting
func (app *App) Greeting(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
res, err := HelloService(r.Context(), "", time.Now())
if err != nil {
app.Logger.Printf("error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
encoder.Encode(ErrorResponse{
Code: http.StatusInternalServerError,
Message: "something went wrong",
})
return
}
app.Logger.Printf("ok: %v", res)
w.WriteHeader(http.StatusOK)
encoder.Encode(res)
return
}
// GreetingWithName greeting with name
func (app *App) GreetingWithName(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
val := mux.Vars(r)
res, err := HelloService(r.Context(), val["name"], time.Now())
if err != nil {
app.Logger.Printf("error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
encoder.Encode(ErrorResponse{
Code: http.StatusInternalServerError,
Message: "something went wrong",
})
return
}
app.Logger.Printf("ok: %v", res)
w.WriteHeader(http.StatusOK)
encoder.Encode(res)
return
}
// App application
type App struct {
Host string
Name string
Logger *log.Logger
}
func main() {
host, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
app := App{
Name: "my-service",
Host: host,
Logger: log.New(os.Stdout, fmt.Sprintf("[host=%s] ", host), log.LstdFlags),
}
// for gorilla/mux
router := mux.NewRouter()
r := router.PathPrefix("/api").Subrouter()
r.Methods("GET").Path("/hello").HandlerFunc(app.Greeting)
r.Methods("GET").Path("/hello/staticName").HandlerFunc(app.Greeting)
r.Methods("GET").Path("/hello/{name}").HandlerFunc(app.GreetingWithName)
if err := http.ListenAndServe(":8080", router); err != nil {
log.Fatal(err)
}
}