aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 4bb76746892d76824b170318f6c0f8f95c3b20c1 (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * The main logic for fbfp.
 *
 * Copyright (C) 2024  Runxi Yu <https://runxiyu.org>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package main

import (
	"errors"
	"fmt"
	"html/template"
	"log"
	"net"
	"net/http"
	"net/http/fcgi"

	"gorm.io/gorm"
)

var db *gorm.DB
var tmpl *template.Template

func handle_index(w http.ResponseWriter, req *http.Request) {
	session_cookie, err := req.Cookie("session")
	if errors.Is(err, http.ErrNoCookie) {
		err = tmpl.ExecuteTemplate(
			w,
			"index_login",
			map[string]string{
				"authUrl": generate_authorization_url(),
			},
		)
		if err != nil {
			log.Println(err)
			return
		}
		return
	} else if err != nil {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.WriteHeader(400)
		w.Write([]byte(fmt.Sprintf(
			"Error\n" +
				"Unable to check cookie.",
		)))
		return
	}
	var session session_t
	err = db.First(&session, session_t{Cookie: session_cookie.Value}).Error
	if err != nil {
		err = tmpl.ExecuteTemplate(
			w,
			"index_login",
			map[string]interface{}{
				"authUrl": generate_authorization_url(),
				"notes":   []string{"Cookie lookup failed. You are now unauthenticated."},
			},
		)
		if err != nil {
			log.Println(err)
			return
		}
		return
	}
	fmt.Println(session.User)
	user := session.User
	err = tmpl.ExecuteTemplate(
		w,
		"index",
		map[string]interface{}{
			"user": user,
		},
	)
	if err != nil {
		log.Println(err)
		return
	}
}

func main() {
	/*
	 * This seems necessary because in the following sections I need to
	 * assign to global variables like db and tmpl. If I use = there, and
	 * don't declare err here, then err is undeclared; but if I use :=
	 * there, my global variables are treated as local variables.
	 */
	var err error

	fbfp_get_config("fbfp.scfg")

	log.Printf("Setting up database\n")
	e(setup_database())

	log.Printf("Setting up templates\n")
	tmpl = er(template.ParseGlob(config.Tmpl + "/*"))

	if config.Static {
		log.Printf("Registering static handle\n")
		fs := http.FileServer(http.Dir("./static"))
		http.Handle("/static/", http.StripPrefix("/static/", fs))
	}

	log.Printf("Registering handlers\n")
	http.HandleFunc("/{$}", handle_index)
	http.HandleFunc("/oidc", handle_oidc)

	log.Printf("Fetching OpenID Connect configuration\n")
	get_openid_config(config.Openid.Endpoint)

	log.Printf(
		"Establishing listener for net \"%s\", addr \"%s\"\n",
		config.Listen.Net,
		config.Listen.Addr,
	)
	l := er(net.Listen(config.Listen.Net, config.Listen.Addr))

	if config.Listen.Proto == "http" {
		log.Printf("Serving http\n")
		err = http.Serve(l, nil)
	} else if config.Listen.Proto == "fcgi" {
		log.Printf("Serving fcgi\n")
		err = fcgi.Serve(l, nil)
	}
	e(err)
}