summaryrefslogtreecommitdiffstats
path: root/api/router.go
blob: 94ec88dc64a6364ccffda7e2557eeb2e67c9bb92 (plain)
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
package api

import (
	"net/http"

	"git.leonardobishop.net/stash/api/handlers"
	"git.leonardobishop.net/stash/api/middleware"
	"git.leonardobishop.net/stash/internal/config"
	"git.leonardobishop.net/stash/pkg/entries"
	"git.leonardobishop.net/stash/pkg/html"
	"git.leonardobishop.net/stash/pkg/kinds"
)

type ApiServices struct {
	EntiresService entries.Service
	KindsService   kinds.Service
	HtmlService    html.Service

	Config *config.Config
}

func NewServer(api ApiServices) *http.ServeMux {
	mux := http.NewServeMux()

	cors := middleware.PermissiveCors()
	auth := middleware.MustAuthenticate(api.Config.Token)

	mux.HandleFunc("POST /record", cors(auth(handlers.RecordEntry(api.EntiresService))))
	mux.HandleFunc("PUT /record", cors(auth(handlers.UpdateEntry(api.EntiresService))))
	mux.HandleFunc("DELETE /record", cors(auth(handlers.DeleteEntry(api.EntiresService))))
	mux.HandleFunc("GET /entry", cors(auth(handlers.GetEntryURLs(api.EntiresService))))
	mux.HandleFunc("POST /entry", cors(auth(handlers.GetEntry(api.EntiresService))))
	mux.HandleFunc("GET /html", handlers.GetEntriesHtml(api.EntiresService, api.KindsService, api.HtmlService))
	mux.HandleFunc("GET /", handlers.Pong())

	return mux
}