Use separate tmpl/ folder
[fbfp.git] / main.go
blobb485d40a71704928aa5d2984b0b1a41a5821fa53
1 package main
3 import (
4 "fmt"
5 "html/template"
6 "log"
7 "net"
8 "net/http"
9 "net/http/fcgi"
11 "gorm.io/driver/sqlite"
12 "gorm.io/gorm"
15 var db *gorm.DB
16 var tmpl *template.Template
18 func handle_index(w http.ResponseWriter, req *http.Request) {
19 tmpl.ExecuteTemplate(w, "index", nil)
22 func handle_login(w http.ResponseWriter, req *http.Request) {
23 /* TODO: Invalidate current session */
24 openid_authorization_url := generate_authorization_url()
25 http.Redirect(w, req, openid_authorization_url, 303)
28 func main() {
29 var err error
30 fbfp_get_config("fbfp.scfg")
32 log.Printf("Opening database\n")
33 switch config.Db.Type {
34 case "sqlite":
35 db, err = gorm.Open(sqlite.Open(config.Db.Conn), &gorm.Config{})
36 e(err)
37 default:
38 e(fmt.Errorf("Database type %s unsupported", config.Db.Type))
41 log.Printf("Setting up templates\n")
42 tmpl, err = template.ParseGlob("tmpl/*")
43 e(err)
45 log.Printf("Registering handlers\n")
46 http.HandleFunc("/{$}", handle_index)
47 http.HandleFunc("/login", handle_login)
48 http.HandleFunc(config.Openid.Redirect, handle_oidc)
50 log.Printf(
51 "Establishing listener for net %s, addr %s\n",
52 config.Listen.Net,
53 config.Listen.Addr,
55 l, err := net.Listen(config.Listen.Net, config.Listen.Addr)
56 e(err)
58 log.Printf("Fetching OpenID Connect configuration\n")
59 get_openid_config(config.Openid.Endpoint)
61 if config.Listen.Proto == "http" {
62 log.Printf("Serving http\n")
63 err = http.Serve(l, nil)
64 } else if config.Listen.Proto == "fcgi" {
65 log.Printf("Serving fcgi\n")
66 err = fcgi.Serve(l, nil)
68 e(err)