108 lines
2.1 KiB
Go
108 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"sync"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type server struct {
|
|
Config *config
|
|
|
|
Authors map[string]*author
|
|
AuthorsLock sync.RWMutex
|
|
|
|
Notebooks map[string]*notebook
|
|
NotebooksLock sync.RWMutex
|
|
}
|
|
|
|
func initialize(configPath string, debug bool) {
|
|
var err error
|
|
|
|
stick = &server{}
|
|
stick.Config, err = readConfigFile(configPath)
|
|
checkError(err)
|
|
|
|
stick.Config.Debug = debug
|
|
stickSalt = []byte(stick.Config.Salt)
|
|
|
|
stick.loadAuthors()
|
|
stick.loadNotebooks()
|
|
}
|
|
|
|
func (s *server) loadAuthors() {
|
|
// TODO: Allow reloading config
|
|
|
|
stick.Authors = map[string]*author{}
|
|
for _, a := range stick.Config.Authors {
|
|
stick.Authors[hash(a.Email)] = &author{Key: hash(a.Email), Email: a.Email, Name: a.Name}
|
|
}
|
|
|
|
stick.Authors["public"] = publicAuthor
|
|
}
|
|
|
|
func (s *server) loadNotebooks() {
|
|
// TODO: Allow reloading config
|
|
|
|
stick.Notebooks = make(map[string]*notebook)
|
|
for _, nbconfig := range stick.Config.Notebooks {
|
|
notebook, err := loadNotebook(nbconfig.Repo)
|
|
if err != nil {
|
|
log.Fatal(errors.Wrapf(err, "failed to load %s", nbconfig.Repo))
|
|
}
|
|
|
|
notebook.ID = hash(nbconfig.Label)
|
|
notebook.Label = nbconfig.Label
|
|
|
|
notebook.Serve = make(map[string]int)
|
|
for serveauthor, serveaccess := range nbconfig.Serve {
|
|
var servehash string
|
|
if serveauthor == "public" {
|
|
servehash = "public"
|
|
} else {
|
|
servehash = hash(serveauthor)
|
|
}
|
|
notebook.Serve[servehash] = serveaccess
|
|
}
|
|
|
|
stick.Notebooks[hash(nbconfig.Label)] = notebook
|
|
}
|
|
|
|
var author *author
|
|
for _, notebook := range stick.Notebooks {
|
|
for serveauth := range notebook.Serve {
|
|
author = stick.getAuthor(serveauth)
|
|
if author == nil {
|
|
log.Println("no author", notebook.Label, stick.getAuthor(serveauth))
|
|
continue
|
|
}
|
|
|
|
author.Notebooks = append(author.Notebooks, notebook)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (s *server) getAuthor(key string) *author {
|
|
s.AuthorsLock.RLock()
|
|
defer s.AuthorsLock.RUnlock()
|
|
|
|
if a, ok := s.Authors[key]; ok {
|
|
return a
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *server) getNotebook(key string) *notebook {
|
|
s.NotebooksLock.RLock()
|
|
defer s.NotebooksLock.RUnlock()
|
|
|
|
if n, ok := s.Notebooks[key]; ok {
|
|
return n
|
|
}
|
|
|
|
return nil
|
|
}
|