all repos — flounder @ 383d618d53b72f3ca8aec4643ca60e7a1695f685

A small site builder for the Gemini protocol

admin.go (view raw)

 1package main
 2
 3// Commands for administering your instance
 4// reset user password -> generate link
 5// delete user
 6
 7// Run some scripts to setup your instance
 8
 9import (
10	"fmt"
11	"io/ioutil"
12	"log"
13	"os"
14	"path"
15	"path/filepath"
16)
17
18// TODO improve cli
19func runAdminCommand() {
20	if len(os.Args) < 4 {
21		fmt.Println("expected subcommand with parameter")
22		os.Exit(1)
23	}
24	switch os.Args[2] {
25	case "activate-user":
26		username := os.Args[3]
27		err := activateUser(username)
28		log.Fatal(err)
29	case "delete-user":
30		username := os.Args[3]
31		// TODO add confirmation
32		err := deleteUser(username)
33		log.Fatal(err)
34	}
35	// reset password
36
37}
38
39func activateUser(username string) error {
40	_, err := DB.Exec("UPDATE user SET active = true WHERE username = $1", username)
41	if err != nil {
42		return err
43	}
44	log.Println("Activated user", username)
45	baseIndex := `# Welcome to Flounder!
46## About
47Welcome to an ultra-lightweight platform for making and sharing small websites. You can get started by editing this page -- remove this content and replace it with whatever you like! It will be live at <your-name>.flounder.online. You can go there right now to see what this page currently looks like. Here is a link to a page which will give you more information about using flounder:
48=> //admin.flounder.online
49
50And here's a guide to the text format that Flounder uses to create pages, Gemini. These pages are converted into HTML so they can be displayed in a web browser.
51=> //admin.flounder.online/gemini_text_guide.gmi
52
53Have fun!`
54	// Redundant filepath.Clean call just in case.
55	username = filepath.Clean(username)
56	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
57	ioutil.WriteFile(path.Join(c.FilesDirectory, username, "index.gmi"), []byte(baseIndex), 0644)
58	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
59	return nil
60}
61
62func deleteUser(username string) error {
63	_, err := DB.Exec("DELETE FROM user WHERE username = $1", username)
64	if err != nil {
65		return err
66	}
67	username = filepath.Clean(username)
68	os.RemoveAll(path.Join(c.FilesDirectory, username))
69	return nil
70}