2 * The main logic for fbfp.
4 * Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
5 * SPDX-License-Identifier: AGPL-3.0-or-later
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
35 var tmpl
*template
.Template
37 func handle_index(w http
.ResponseWriter
, req
*http
.Request
) {
38 session_cookie
, err
:= req
.Cookie("session")
39 if errors
.Is(err
, http
.ErrNoCookie
) {
40 err
= tmpl
.ExecuteTemplate(w
, "index_login", nil)
48 err
= tmpl
.ExecuteTemplate(w
, "index", nil)
55 func handle_login(w http
.ResponseWriter
, req
*http
.Request
) {
56 /* TODO: Invalidate current session */
57 openid_authorization_url
:= generate_authorization_url()
58 http
.Redirect(w
, req
, openid_authorization_url
, 303)
63 * This seems necessary because in the following sections I need to
64 * assign to global variables like db and tmpl. If I use = there, and
65 * don't declare err here, then err is undeclared; but if I use :=
66 * there, my global variables are treated as local variables.
70 fbfp_get_config("fbfp.scfg")
72 log
.Printf("Setting up database\n")
75 log
.Printf("Setting up templates\n")
76 tmpl
, err
= template
.ParseGlob("tmpl/*")
80 log
.Printf("Registering static handle\n")
81 fs
:= http
.FileServer(http
.Dir("./static"))
82 http
.Handle("/static/", http
.StripPrefix("/static/", fs
))
85 log
.Printf("Registering handlers\n")
86 http
.HandleFunc("/{$}", handle_index
)
87 http
.HandleFunc("/login", handle_login
)
88 http
.HandleFunc("/oidc", handle_oidc
)
90 log
.Printf("Fetching OpenID Connect configuration\n")
91 get_openid_config(config
.Openid
.Endpoint
)
94 "Establishing listener for net \"%s\", addr \"%s\"\n",
98 l
, err
:= net
.Listen(config
.Listen
.Net
, config
.Listen
.Addr
)
101 if config
.Listen
.Proto
== "http" {
102 log
.Printf("Serving http\n")
103 err
= http
.Serve(l
, nil)
104 } else if config
.Listen
.Proto
== "fcgi" {
105 log
.Printf("Serving fcgi\n")
106 err
= fcgi
.Serve(l
, nil)