all repos — flounder @ 5eec59dca9972e3f10e4bd24bb8ce0127b9ea6be

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		err := deleteUser(username)
32		log.Fatal(err)
33	}
34	// reset password
35
36}
37
38func activateUser(username string) error {
39	_, err := DB.Exec("UPDATE user SET active = true WHERE username = $1", username)
40	if err != nil {
41		return err
42	}
43	log.Println("Activated user", username)
44	baseIndex := `# Welcome to Flounder!
45## About
46Welcome 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:
47=> //admin.flounder.online
48
49And 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.
50=> //admin.flounder.online/gemini_text_guide.gmi
51
52Have fun!`
53	// Redundant filepath.Clean call just in case.
54	username = filepath.Clean(username)
55	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
56	ioutil.WriteFile(path.Join(c.FilesDirectory, username, "index.gmi"), []byte(baseIndex), 0644)
57	os.Mkdir(path.Join(c.FilesDirectory, username), os.ModePerm)
58	return nil
59}
60
61func deleteUser(username string) error {
62	// not sure whether we should delete files too
63	_, err := DB.Exec("DELETE FROM user WHERE username = $1", username)
64	if err != nil {
65		return err
66	}
67	return nil
68}