This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (60 loc) · 2.7 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
69
70
71
72
73
74
package main
import (
"log"
"net"
"net/http"
"os"
"time"
"github.com/exlibris-fed/exlibris/config"
"github.com/exlibris-fed/exlibris/handler"
"github.com/exlibris-fed/exlibris/handler/middleware"
"github.com/exlibris-fed/exlibris/infrastructure"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func main() {
cfg := config.Load()
db := infrastructure.New(cfg.DSN)
defer db.Close()
infrastructure.Migrate(db)
h := handler.New(db, cfg)
m := middleware.New(db)
r := mux.NewRouter()
r.Use(m.ExtractUsername)
// APIs
api := r.PathPrefix("/api").Subrouter()
api.HandleFunc("/register", h.Register).Methods(http.MethodPost, http.MethodOptions)
api.HandleFunc("/authenticate", h.Authenticate).Methods(http.MethodPost, http.MethodOptions)
api.HandleFunc("/verify/resend/{user}", h.ResendVerificationKey).Methods(http.MethodPost, http.MethodOptions)
api.HandleFunc("/verify/{key}", h.VerifyKey).Methods(http.MethodGet, http.MethodOptions)
api.Handle("/user/{username}", http.HandlerFunc(h.HandleActivityPubProfile))
books := api.PathPrefix("/book").Subrouter()
books.Use(m.WithUserModel)
books.HandleFunc("", h.SearchBooks).Methods(http.MethodGet, http.MethodOptions)
books.HandleFunc("/{book}/read", h.Read).Methods(http.MethodPost, http.MethodOptions)
books.HandleFunc("/read", h.GetReads).Methods(http.MethodGet, http.MethodOptions)
books.HandleFunc("/{book}", h.GetBook).Methods(http.MethodGet, http.MethodOptions)
books.HandleFunc("/{book}/review", h.Review).Methods(http.MethodPost, http.MethodOptions, http.MethodGet)
// inbox/outbox handle authentication as part of the go-fed flow. ExtractUsername will populate it if present.
r.HandleFunc("/user/{username}", http.HandlerFunc(h.HandleActivityPubProfile))
r.Handle("/user/{username}/inbox", m.WithUserModel(http.HandlerFunc(h.HandleInbox)))
r.Handle("/user/{username}/outbox", m.WithUserModel(http.HandlerFunc(h.HandleOutbox)))
r.PathPrefix("/user/").Handler(http.HandlerFunc(h.HandleActivityPubAction))
// App
r.HandleFunc("/.well-known/acme-challenge/{id}", h.HandleChallenge)
r.HandleFunc("/.well-known/webfinger", h.HandleWebfinger)
r.PathPrefix("/").Handler(http.HandlerFunc(h.HandleStaticFile))
corsRouter := handlers.CORS(handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedHeaders([]string{"Content-Type", "Authorization", "Access-Control-Allow-Origin"}))
loggedRouter := handlers.LoggingHandler(os.Stdout, corsRouter(r))
addr := net.JoinHostPort(cfg.Host, cfg.Port)
log.Println("Starting on", addr)
server := &http.Server{
Handler: loggedRouter,
Addr: addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(server.ListenAndServe())
}