Skip to content
Snippets Groups Projects
main.go 1.45 KiB
Newer Older
gbe's avatar
gbe committed
package main

import (
	"embed"
	"log"
	"net/http"
gbe's avatar
gbe committed

	bolt "go.etcd.io/bbolt"

	"git.c3pb.de/gbe/invinoveritas/auth"
gbe's avatar
gbe committed
)

//go:embed templates/*.tpl
var templateFS embed.FS

//go:embed static/*
var staticFS embed.FS

gbe's avatar
gbe committed
func httpError(w http.ResponseWriter, msg string, err error, status int) {
gbe's avatar
gbe committed
	if err != nil {
		msg += ": " + err.Error()
	}

	log.Println(msg)

	http.Error(w, msg, status)
gbe's avatar
gbe committed
type Handler struct {
	db *bolt.DB
gbe's avatar
gbe committed
	ap authProvider
}

func logRequest(r *http.Request) {
	log.Println("handling", r.Method, r.URL, "from", r.RemoteAddr, "by", auth.Get(r))
gbe's avatar
gbe committed
func main() {
	db, err := bolt.Open("vino.db", 0644, nil)
gbe's avatar
gbe committed
	if err != nil {
		log.Fatalln("can't open DB:", err)
	}
	defer db.Close()

	err = initDB(db)
	if err != nil {
		log.Fatalln("can't initialize DB:", err)
	}

	http.HandleFunc("/favicon.ico", http.NotFound)

gbe's avatar
gbe committed
	http.Handle("/static/", http.FileServer(http.FS(staticFS)))

gbe's avatar
gbe committed
	ap := authProvider{
gbe's avatar
gbe committed
		db: db,
	}
gbe's avatar
gbe committed

gbe's avatar
gbe committed
	handler := Handler{
gbe's avatar
gbe committed
		db: db,
gbe's avatar
gbe committed
		ap: ap,
gbe's avatar
gbe committed
	}

	http.HandleFunc("/details/img", auth.Require(http.HandlerFunc(handler.img), ap))
	http.HandleFunc("/details/", auth.Require(http.HandlerFunc(handler.details), ap))
gbe's avatar
gbe committed
	http.HandleFunc("/user/", auth.Require(http.HandlerFunc(handler.user), ap))
	http.HandleFunc("/", auth.Require(http.HandlerFunc(handler.index), ap))
gbe's avatar
gbe committed

gbe's avatar
gbe committed
	const listenAddr = ":7878"
gbe's avatar
gbe committed

	log.Printf("here we go, listening on http://%s", listenAddr)

gbe's avatar
gbe committed
	err = http.ListenAndServe(listenAddr, nil)
gbe's avatar
gbe committed
	if err != nil {
gbe's avatar
gbe committed
		log.Fatalln("http handler failed:", err)
gbe's avatar
gbe committed
	}
}