all repos — flounder @ 821746c4cd42fecad123995d2013f3c2f549df24

A small site builder for the Gemini protocol

main.go (view raw)

  1package main
  2
  3import (
  4	"database/sql"
  5	"flag"
  6	"fmt"
  7	"github.com/gorilla/sessions"
  8	"io/ioutil"
  9	"log"
 10	"os"
 11	"path"
 12	"path/filepath"
 13	"sort"
 14	"strings"
 15	"sync"
 16	"time"
 17)
 18
 19var c Config // global var to hold static configuration
 20
 21type File struct {
 22	Creator     string
 23	Name        string
 24	UpdatedTime time.Time
 25}
 26
 27func getUsers() ([]string, error) {
 28	rows, err := DB.Query(`SELECT username from user`)
 29	if err != nil {
 30		return nil, err
 31	}
 32	var users []string
 33	for rows.Next() {
 34		var user string
 35		err = rows.Scan(&user)
 36		if err != nil {
 37			return nil, err
 38		}
 39		users = append(users, user)
 40	}
 41	return users, nil
 42}
 43
 44/// Perform some checks to make sure the file is OK
 45func checkIfValidFile(filename string, fileBytes []byte) error {
 46	ext := strings.ToLower(path.Ext(filename))
 47	found := false
 48	for _, mimetype := range c.OkExtensions {
 49		if ext == mimetype {
 50			found = true
 51		}
 52	}
 53	if !found {
 54		return fmt.Errorf("Invalid file extension: %s", ext)
 55	}
 56	if len(fileBytes) > c.MaxFileSize {
 57		return fmt.Errorf("File too large. File was %s bytes, Max file size is %s", len(fileBytes), c.MaxFileSize)
 58	}
 59	return nil
 60}
 61
 62func getIndexFiles() ([]*File, error) { // cache this function
 63	result := []*File{}
 64	err := filepath.Walk(c.FilesDirectory, func(path string, info os.FileInfo, err error) error {
 65		if err != nil {
 66			log.Printf("Failure accessing a path %q: %v\n", path, err)
 67			return err // think about
 68		}
 69		// make this do what it should
 70		result = append(result, &File{
 71			Name:        info.Name(),
 72			Creator:     "alex",
 73			UpdatedTime: info.ModTime(),
 74		})
 75		return nil
 76	})
 77	if err != nil {
 78		return nil, err
 79	}
 80	// sort
 81	// truncate
 82	sort.Slice(result, func(i, j int) bool {
 83		return result[i].UpdatedTime.Before(result[j].UpdatedTime)
 84	})
 85	if len(result) > 50 {
 86		result = result[:50]
 87	}
 88	return result, nil
 89} // todo clean up paths
 90
 91func getUserFiles(user string) ([]*File, error) {
 92	result := []*File{}
 93	files, err := ioutil.ReadDir(path.Join(c.FilesDirectory, user))
 94	if err != nil {
 95		return nil, err
 96	}
 97	for _, file := range files {
 98		result = append(result, &File{
 99			Name:        file.Name(),
100			Creator:     user,
101			UpdatedTime: file.ModTime(),
102		})
103	}
104	return result, nil
105}
106
107func main() {
108	configPath := flag.String("c", "flounder.toml", "path to config file")
109	var err error
110	c, err = getConfig(*configPath)
111	if err != nil {
112		log.Fatal(err)
113	}
114	SessionStore = sessions.NewCookieStore([]byte(c.CookieStoreKey))
115	DB, err = sql.Open("sqlite3", c.DBFile)
116	if err != nil {
117		log.Fatal(err)
118	}
119
120	wg := new(sync.WaitGroup)
121	wg.Add(2)
122	go func() {
123		runHTTPServer()
124		wg.Done()
125	}()
126	go func() {
127		runGeminiServer()
128		wg.Done()
129	}()
130	wg.Wait()
131}