-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
146 lines (130 loc) · 4.41 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package server
import (
"encoding/json"
"net/http"
"time"
"cloud.google.com/go/storage"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/httplog"
"github.com/patrickmn/go-cache"
"github.com/rs/zerolog"
"github.com/raymonstah/asianamericanswiki/internal/humandao"
"github.com/raymonstah/asianamericanswiki/internal/openai"
"github.com/raymonstah/asianamericanswiki/internal/ratelimiter"
"github.com/raymonstah/asianamericanswiki/internal/userdao"
)
type Config struct {
AuthClient Authorizer
HumanDAO *humandao.DAO
UserDAO *userdao.DAO
Logger zerolog.Logger
Version string
OpenAIClient *openai.Client
StorageClient *storage.Client
Local bool
}
type Server struct {
authClient Authorizer
router chi.Router
logger zerolog.Logger
humanCache *cache.Cache
rateLimiter *ratelimiter.RateLimiter
humanDAO *humandao.DAO
userDAO *userdao.DAO
version string
openAIClient *openai.Client
storageClient *storage.Client
}
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
s.router.ServeHTTP(writer, request)
}
func NewServer(config Config) *Server {
r := chi.NewRouter()
humanCache := cache.New(5*time.Minute, 10*time.Minute)
s := &Server{
authClient: config.AuthClient,
router: r,
logger: config.Logger,
humanCache: humanCache,
rateLimiter: ratelimiter.New(3, time.Second),
humanDAO: config.HumanDAO,
userDAO: config.UserDAO,
version: config.Version,
openAIClient: config.OpenAIClient,
storageClient: config.StorageClient,
}
r.Use(middleware.RealIP)
r.Use(middleware.CleanPath)
r.Use(middleware.Recoverer)
r.Use(middleware.Compress(5))
r.Use(cors.Handler(cors.Options{
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
}))
s.setupRoutes()
htmlServer := NewServerHTML(ServerHTMLConfig{
Local: config.Local,
HumanDAO: config.HumanDAO,
Logger: config.Logger,
StorageClient: config.StorageClient,
AuthClient: config.AuthClient,
RollbarToken: "e1082079233c44628d29032fc1847ca7",
OpenaiClient: config.OpenAIClient,
})
if err := htmlServer.Register(r); err != nil {
panic(err)
}
return s
}
func (s *Server) setupRoutes() {
s.router.Group(func(r chi.Router) {
r.Use(httplog.RequestLogger(s.logger))
r.Route("/api/v1/", func(r chi.Router) {
r.Method(http.MethodGet, "/version", Handler(s.Version))
r.Method(http.MethodGet, "/humans/{humanID}/reactions", Handler(s.ReactionsForHuman))
r.Route("/humans", func(r chi.Router) {
r.Method(http.MethodGet, "/", Handler(s.HumansList))
r.With(s.AuthMiddleware).Method(http.MethodPost, "/search", Handler(s.HumansByID))
r.Method(http.MethodGet, "/{path}", Handler(s.HumanGet))
r.With(s.AuthMiddleware).Method(http.MethodPost, "/", Handler(s.HumanCreate))
r.With(s.AdminMiddleware).Method(http.MethodGet, "/drafts", Handler(s.HumansDraft))
r.With(s.AdminMiddleware).Method(http.MethodPost, "/{id}/review", Handler(s.HumansReview))
})
r.Route("/reactions", func(r chi.Router) {
r.Method(http.MethodGet, "/", Handler(s.GetReactions))
r.With(s.AuthMiddleware).Method(http.MethodPost, "/", Handler(s.PostReaction))
r.With(s.AuthMiddleware).Method(http.MethodDelete, "/{id}", Handler(s.DeleteReaction))
})
r.With(s.AuthMiddleware).Method(http.MethodGet, "/user", Handler(s.User))
r.With(s.AuthMiddleware).Method(http.MethodPost, "/humans/{humanID}/save", Handler(s.SaveHuman))
r.With(s.AuthMiddleware).Method(http.MethodDelete, "/humans/{humanID}/save", Handler(s.UnsaveHuman))
r.
With(s.RateLimitMiddleware).
With(s.OptionalAuthMiddleware).
Method(http.MethodPost, "/humans/{humanID}/view", Handler(s.ViewHuman))
})
})
}
func (s *Server) Version(w http.ResponseWriter, r *http.Request) error {
data := map[string]string{
"version": s.version,
"now": time.Now().String(),
}
s.writeData(w, http.StatusOK, data)
return nil
}
func (s *Server) writeData(w http.ResponseWriter, status int, data any) {
w.WriteHeader(status)
if status != http.StatusNoContent {
dataResponse := struct {
Data any `json:"data"`
}{
Data: data,
}
if err := json.NewEncoder(w).Encode(dataResponse); err != nil {
s.logger.Err(err).Msg("error encoding json data response")
}
}
}