-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (57 loc) · 1.43 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/29-FYI/twentynine"
)
type twentyninefyi struct {
lr linkring
logger *log.Logger
}
func (tnfyi *twentyninefyi) Links(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(tnfyi.lr.Links())
}
func (tnfyi *twentyninefyi) CreateLink(w http.ResponseWriter, r *http.Request) {
var link twentynine.Link
if err := json.NewDecoder(r.Body).Decode(&link); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
nlr, ok := tnfyi.lr.LinkLink(link)
if !ok {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
tnfyi.lr = nlr
}
func (tnfyi *twentyninefyi) DeleteLinks(w http.ResponseWriter, r *http.Request) {
tnfyi.lr = linkring{}
}
func (tnfyi *twentyninefyi) Handler() http.Handler {
rtr := chi.NewRouter()
rtr.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "DELETE"},
}))
rtr.Get("/", tnfyi.Links)
rtr.Post("/", tnfyi.CreateLink)
rtr.Delete("/", tnfyi.DeleteLinks)
return rtr
}
func main() {
// logger
logger := log.New(os.Stderr, "", log.Lshortfile)
// handler
tnfyi := twentyninefyi{
logger: logger,
}
hndlr := tnfyi.Handler()
// HTTP
if err := http.ListenAndServe(":http", hndlr); err != nil {
logger.Fatalf("HTTP server: %s", err)
}
}