aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 98a302022f303cb75b70f266411cb13101f47f8a (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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"strings"

	"fagott.pw/charakterin"
	"fagott.pw/grilist/dataimport"
	"fagott.pw/grilist/eventlogging"
	"fagott.pw/grilist/frontend"
	"fagott.pw/grilist/grilist"
	"fagott.pw/grilist/modules/grils"
	"fagott.pw/grilist/modules/likes"
	"fagott.pw/grilist/modules/lists"
	"fagott.pw/grilist/modules/search"
	"fagott.pw/grilist/modules/series"
	"fagott.pw/grilist/modules/tags"
	"fagott.pw/grilist/modules/user"
	"fagott.pw/nsa"

	"github.com/julienschmidt/httprouter"
	_ "github.com/lib/pq"
)

var app *grilist.Grilist

func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	if strings.Index(r.Referer(), "127.0.0.1") != -1 {
		w.WriteHeader(200)
		return
	}
	http.Redirect(w, r, "/dashboard", 301)
}

func loadModule(mod grilist.Module) error {
	if _, ok := app.Modules[mod.Name()]; ok {
		return fmt.Errorf("module with name %s already exists", mod.Name())
	}
	app.Modules[mod.Name()] = mod
	mod.Init(app)
	return nil
}

func main() {
	config := grilist.LoadConfig()
	db, err := sql.Open("postgres", config.DBConnectionString())
	if err != nil {
		log.Fatal(err)
	}
	if err := db.Ping(); err != nil {
		log.Fatal(err)
	}

	log.Println("database connection established")

	if config.EnableNSA {
		if err := nsa.Init(db); err != nil {
			panic(err)
		}
		if err := nsa.SetEvents(eventlogging.Events); err != nil {
			panic(err)
		}
	}
	dataimport.Init(db)

	functions := charakterin.GetRendererFunctions()

	renderer := frontend.New("views", functions)
	login := charakterin.New(db)
	login.UseRenderer(renderer)
	if config.AnilistAPI.ClientID != "" && config.AnilistAPI.ClientSecret != "" {
		login.EnableAnilistAPI(config.AnilistAPI.ClientID, config.AnilistAPI.ClientSecret)
	}

	router := httprouter.New()

	app = &grilist.Grilist{
		config,
		renderer,
		login,
		db,
		make(map[string]grilist.Module),
		router,
	}

	router.GET("/", index)
	router.GET("/health", app.HealthCheck)
	router.HandlerFunc("GET", "/login", login.DisplayLogin)
	router.HandlerFunc("POST", "/login", login.Login)
	router.HandlerFunc("GET", "/settings", login.DisplayUserSettings)
	router.HandlerFunc("POST", "/settings", login.SaveUserRoute)
	router.HandlerFunc("GET", "/logout", login.Logout)
	router.HandlerFunc("GET", "/register", login.DisplayRegistration)
	router.HandlerFunc("POST", "/register", login.Register)
	router.GET("/settings/api/:type/couple", login.DisplayAPICouplePage)
	router.POST("/settings/api/:type/couple", login.CoupleAPI)

	fs := http.FileServer(http.Dir("assets"))
	http.Handle("/assets/", http.StripPrefix("/assets/", fs))
	http.Handle("/", router)

	// Module laden
	loadModule(grils.New())
	loadModule(lists.New())
	loadModule(tags.New())
	loadModule(search.New())
	loadModule(user.New())
	loadModule(series.New())
	loadModule(likes.New())
	router.GET("/dashboard", viewDashboard)

	log.Fatal(http.ListenAndServe(":8080", nil))
}