cabinet/web/routes.go

99 lines
2.9 KiB
Go
Raw Permalink Normal View History

2019-10-30 15:31:07 +00:00
package web
import (
2020-03-15 10:44:27 +00:00
"fmt"
2019-11-01 17:09:17 +00:00
"net/http"
"os"
2019-10-30 15:31:07 +00:00
"time"
2020-03-15 10:44:27 +00:00
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"code.chrissexton.org/cws/cabinet/auth"
2019-10-30 20:19:35 +00:00
"code.chrissexton.org/cws/cabinet/db"
2019-10-30 15:31:07 +00:00
"github.com/stretchr/graceful"
)
type Web struct {
addr string
db *db.Database
salt string
static http.FileSystem
2019-10-30 15:31:07 +00:00
}
func New(addr string, db *db.Database, static http.FileSystem) *Web {
2019-10-30 15:31:07 +00:00
w := &Web{
addr: addr,
db: db,
static: static,
2019-10-30 15:31:07 +00:00
}
if err := db.MakeDB(); err != nil {
log.Fatal().
Err(err).
Msg("could not create database")
}
return w
}
type AuthMiddleware struct {
db *db.Database
}
2020-03-15 10:44:27 +00:00
func (aw *AuthMiddleware) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := r.Header.Get("X-Auth-Key")
u, err := auth.GetByKey(aw.db, key)
if key == "" || err != nil {
2020-03-15 10:44:27 +00:00
w.WriteHeader(401)
fmt.Fprint(w, "invalid login")
return
2020-03-15 10:44:27 +00:00
}
log.Debug().Msgf("This shit is authed to user %s!", u.Name)
2020-03-15 10:44:27 +00:00
next.ServeHTTP(w, r)
})
}
2019-11-01 17:09:17 +00:00
func (web *Web) routeSetup() http.Handler {
2019-10-30 15:31:07 +00:00
r := mux.NewRouter()
2019-11-03 20:01:24 +00:00
api := r.PathPrefix("/v1/").Subrouter()
auth := AuthMiddleware{web.db}
2020-03-15 10:44:27 +00:00
authedApi := r.PathPrefix("/v1/").Subrouter()
authedApi.Use(auth.Middleware)
api.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
log.Debug().Msg("test json")
}).Methods(http.MethodPost).HeadersRegexp("Content-Type", `application/json.*`)
api.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
log.Debug().Msg("test markdown")
}).Methods(http.MethodPost).HeadersRegexp("Content-Type", `application/markdown.*`)
// curl 'http://127.0.0.1:8080/v1/test' -X POST -H 'Accept: application/json, text/plain, */*' --compressed -H 'Content-Type: application/json;charset=utf-8' --data '{ "test": 1 }'
authedApi.HandleFunc("/entries", web.newEntry).Methods(http.MethodPost)
authedApi.HandleFunc("/entries", web.newEntry).Methods(http.MethodPost).
2020-03-15 10:44:27 +00:00
HeadersRegexp("Content-Type", "application/(text|json).*")
authedApi.HandleFunc("/entries", web.newMarkdownEntry).Methods(http.MethodPost).
2019-11-16 13:49:07 +00:00
HeadersRegexp("Content-Type", "application/markdown.*")
authedApi.HandleFunc("/entries/{slug}", web.removeEntry).Methods(http.MethodDelete)
authedApi.HandleFunc("/entries/{slug}", web.editEntry).Methods(http.MethodPut)
2020-03-15 10:44:27 +00:00
2019-11-03 20:01:24 +00:00
api.HandleFunc("/entries/{slug}", web.getEntry).Methods(http.MethodGet)
api.HandleFunc("/entries", web.allEntries).Methods(http.MethodGet)
api.HandleFunc("/auth/new", web.newUser).Methods(http.MethodPost)
2019-11-11 00:26:36 +00:00
api.HandleFunc("/auth", web.auth).Methods(http.MethodPost)
2019-10-30 15:31:07 +00:00
r.PathPrefix("/").HandlerFunc(web.indexHandler("/index.html"))
2019-11-01 17:09:17 +00:00
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
return loggedRouter
2019-10-30 15:31:07 +00:00
}
func (web *Web) Serve() {
middle := web.routeSetup()
log.Info().Str("addr", "http://"+web.addr).Msg("serving HTTP")
graceful.Run(web.addr, 10*time.Second, middle)
}