notea-de-me/server/main.go

53 lines
1.2 KiB
Go

package main
import (
"errors"
"local/args"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
)
func main() {
as := args.NewArgSet()
as.Append(args.INT, "p", "port to listen on", 3004)
as.Append(args.STRING, "d", "root dir with /index.html and /media and /files", "./public")
as.Append(args.STRING, "auth", "auth mode [none, path/to/some.yaml, ldap", "none")
if err := as.Parse(); err != nil {
panic(err)
}
auth, err := authFactory(as.GetString("auth"))
if err != nil {
panic(err)
}
s := NewServer(as.GetString("d"), auth)
if err := s.Routes(); err != nil {
panic(err)
}
log.Printf("listening on %v with %s", as.GetInt("p"), as.GetString("auth"))
if err := http.ListenAndServe(":"+strconv.Itoa(as.GetInt("p")), s); err != nil {
panic(err)
}
}
func authFactory(key string) (auth, error) {
switch path.Base(strings.ToLower(key)) {
case "none", "":
return nil, nil
case "ldap":
return nil, errors.New("not impl ldap auth")
}
stat, err := os.Stat(key)
if os.IsNotExist(err) {
return nil, errors.New("looks like auth path does not exist")
} else if err != nil {
return nil, err
} else if stat.IsDir() {
return nil, errors.New("looks like auth path is a dir")
}
return NewFileAuth(key), nil
}