db.go
[fbfp.git] / main.go
blob4f6243ed9b152daeff7391656452cc2ffdb80403
1 /*
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/>.
21 package main
23 import (
24 "errors"
25 "html/template"
26 "log"
27 "net"
28 "net/http"
29 "net/http/fcgi"
31 "gorm.io/gorm"
34 var db *gorm.DB
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)
41 if err != nil {
42 log.Println(err)
43 return
45 return
47 _ = session_cookie
48 err = tmpl.ExecuteTemplate(w, "index", nil)
49 if err != nil {
50 log.Println(err)
51 return
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)
61 func main() {
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.
68 var err error
70 fbfp_get_config("fbfp.scfg")
72 log.Printf("Setting up database\n")
73 e(setup_database())
75 log.Printf("Setting up templates\n")
76 tmpl, err = template.ParseGlob("tmpl/*")
77 e(err)
79 if config.Static {
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)
93 log.Printf(
94 "Establishing listener for net \"%s\", addr \"%s\"\n",
95 config.Listen.Net,
96 config.Listen.Addr,
98 l, err := net.Listen(config.Listen.Net, config.Listen.Addr)
99 e(err)
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)
108 e(err)