This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
90 lines (79 loc) · 2.2 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
package server
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/nu50218/go-cacher"
"github.com/nu50218/nuinfo-syllabus-go/syllabus"
)
type Server struct {
handler http.Handler
client *syllabus.Client
subjectsCache cacher.Cacher
subjectCache *cacher.Map
}
type Config struct {
Endpoint string `env:"ENDPOINT" envDefault:"https://syllabus.i.nagoya-u.ac.jp/i/"`
Expires time.Duration `env:"EXPIRES" envDefault:"1h"`
Interval time.Duration `env:"INTERVAL" envDefault:"500ms"`
}
func New(c Config) *Server {
s := &Server{
client: syllabus.NewClient(c.Endpoint, c.Interval),
}
// cacher
s.subjectsCache = cacher.New(c.Expires, func() interface{} {
subjects, err := s.client.GetAllConciseSubjects()
if err != nil {
return err
}
return subjects
})
s.subjectCache = cacher.NewMap(func(key interface{}) cacher.Cacher {
return cacher.New(c.Expires, func() interface{} {
subject, err := s.client.GetSubject(key.(string))
if err != nil {
return err
}
return subject
})
})
// router
r := chi.NewRouter()
r.Get("/subjects", s.getAllSubjects)
r.Get("/subjects/{code}", s.getSubject)
s.handler = r
return s
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.handler.ServeHTTP(w, r)
}
func (s *Server) getAllSubjects(w http.ResponseWriter, r *http.Request) {
i := s.subjectsCache.Load()
if err, ok := i.(error); ok {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(i); err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
func (s *Server) getSubject(w http.ResponseWriter, r *http.Request) {
code := chi.URLParam(r, "code")
i := s.subjectCache.Get(code).Load()
if err, ok := i.(error); ok {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(i); err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}