all repos — flounder @ 19944763fc195d2db224147841ebcbf2aafb435d

A small site builder for the Gemini protocol

http.go (view raw)

 1package main
 2
 3import (
 4	"fmt"
 5	"html/template"
 6	"log"
 7	"net/http"
 8)
 9
10var t *template.Template
11
12type IndexHandler struct {
13	Domain    string
14	SiteTitle string
15}
16
17func (h *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
18	indexFiles, err := getIndexFiles()
19	if err != nil {
20		log.Fatal(err)
21	}
22	allUsers, err := getUsers()
23	if err != nil {
24		log.Fatal(err)
25	}
26	data := struct {
27		Domain    string
28		PageTitle string
29		Files     []*File
30		Users     []string
31	}{h.Domain, h.SiteTitle, indexFiles, allUsers}
32	err = t.ExecuteTemplate(w, "index.html", data)
33	if err != nil {
34		log.Fatal(err)
35	}
36
37}
38
39func editFileHandler(w http.ResponseWriter, r *http.Request) {
40	// get vs post
41	// read file content
42	authUser := "alex"
43	files, _ := getUserFiles(authUser)
44	for _, file := range files {
45		fmt.Fprintf(w, "%s\n", file.Name)
46	}
47}
48
49func mySiteHandler(w http.ResponseWriter, r *http.Request) {
50	authUser := "alex"
51	files, _ := getUserFiles(authUser)
52	for _, file := range files {
53		fmt.Fprintf(w, "%s\n", file.Name)
54	}
55}
56
57func runHTTPServer(config *Config) {
58	var err error
59	t, err = template.ParseGlob("./templates/*.html") // TODO make template dir configruable
60	if err != nil {
61		log.Fatal(err)
62	}
63	http.Handle("/", &IndexHandler{config.RootDomain, config.SiteTitle})
64	http.HandleFunc("/my_site", mySiteHandler)
65	http.HandleFunc("/edit/", editFileHandler)
66	// http.HandleFunc("/delete/", deleteFileHandler)
67	// login+register functions
68	log.Fatal(http.ListenAndServe(":8080", nil))
69}