This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
/
main.go
82 lines (71 loc) · 2.33 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
75
76
77
78
79
80
81
82
package main
import (
"log"
"net"
"net/http"
"os"
"time"
"github.com/exlibris-fed/exlibris/handler"
"github.com/exlibris-fed/exlibris/model"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func main() {
host := os.Getenv("HOST")
if host == "" {
log.Fatalf("HOST not provided")
}
port := os.Getenv("PORT")
if port == "" {
log.Fatalf("PORT not provided")
}
if os.Getenv("DOMAIN") == "" {
log.Fatalf("DOMAIN not provided")
}
db, err := gorm.Open("postgres", os.Getenv("POSTGRES_CONNECTION"))
if err != nil {
log.Fatalf("unable to connect to database: %s", err)
}
defer db.Close()
db.AutoMigrate(model.Author{})
db.AutoMigrate(model.APObject{})
db.AutoMigrate(model.Book{})
db.AutoMigrate(model.BookAuthor{})
db.AutoMigrate(model.BookSubject{})
db.AutoMigrate(model.OutboxEntry{})
db.AutoMigrate(model.Read{})
db.AutoMigrate(model.Review{})
db.AutoMigrate(model.Subject{})
db.AutoMigrate(model.User{})
h := handler.New(db)
r := mux.NewRouter()
// 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("/book", h.SearchBooks)
api.HandleFunc("/book/{book}/read", h.Read).Methods(http.MethodPost, http.MethodOptions)
api.HandleFunc("/book/read", h.GetReads)
api.HandleFunc("/user/{username}/inbox", h.HandleInbox)
api.HandleFunc("/user/{username}/outbox", h.HandleOutbox)
api.HandleFunc("/@{username}/inbox", h.HandleInbox)
api.HandleFunc("/@{username}/outbox", h.HandleOutbox)
//api.HandleFunc("/fedtest", h.FederationTest).Methods(http.MethodPost, http.MethodOptions)
// App
r.HandleFunc("/.well-known/acme-challenge/{id}", h.HandleChallenge)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./dist/")))
corsRouter := handlers.CORS(handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedHeaders([]string{"Content-Type"}))
loggedRouter := handlers.LoggingHandler(os.Stdout, corsRouter(r))
addr := net.JoinHostPort(host, 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())
}