forked from jason/cpolis
		
	Added config for Firebase key
This commit is contained in:
		@@ -14,6 +14,7 @@ type Config struct {
 | 
			
		||||
	DBName      string
 | 
			
		||||
	Description string
 | 
			
		||||
	Domain      string
 | 
			
		||||
	FirebaseKey string
 | 
			
		||||
	KeyFile     string
 | 
			
		||||
	Link        string
 | 
			
		||||
	LogFile     string
 | 
			
		||||
@@ -27,13 +28,14 @@ type Config struct {
 | 
			
		||||
 | 
			
		||||
func newConfig() *Config {
 | 
			
		||||
	return &Config{
 | 
			
		||||
		DBName:  "cpolis",
 | 
			
		||||
		KeyFile: "/var/www/cpolis/cpolis.key",
 | 
			
		||||
		LogFile: "/var/log/cpolis.log",
 | 
			
		||||
		PDFDir:  "/var/www/cpolis/pdfs",
 | 
			
		||||
		PicsDir: "/var/www/cpolis/pics",
 | 
			
		||||
		RSSFile: "/var/www/cpolis/cpolis.rss",
 | 
			
		||||
		WebDir:  "/var/www/cpolis/web",
 | 
			
		||||
		DBName:      "cpolis",
 | 
			
		||||
		FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
 | 
			
		||||
		KeyFile:     "/var/www/cpolis/cpolis.key",
 | 
			
		||||
		LogFile:     "/var/log/cpolis.log",
 | 
			
		||||
		PDFDir:      "/var/www/cpolis/pdfs",
 | 
			
		||||
		PicsDir:     "/var/www/cpolis/pics",
 | 
			
		||||
		RSSFile:     "/var/www/cpolis/cpolis.rss",
 | 
			
		||||
		WebDir:      "/var/www/cpolis/web",
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -76,19 +78,25 @@ func (c *Config) handleCliArgs() error {
 | 
			
		||||
	port := 8080
 | 
			
		||||
 | 
			
		||||
	flag.StringVar(&c.DBName, "db", c.DBName, "DB name")
 | 
			
		||||
	flag.StringVar(&c.Description, "desc", c.Description, "Channel description")
 | 
			
		||||
	flag.StringVar(&c.Description, "desc", c.Description, "channel description")
 | 
			
		||||
	flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
 | 
			
		||||
	flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file")
 | 
			
		||||
	flag.StringVar(&c.KeyFile, "key", c.KeyFile, "key file")
 | 
			
		||||
	flag.StringVar(&c.Link, "link", c.Link, "Channel Link")
 | 
			
		||||
	flag.StringVar(&c.Link, "link", c.Link, "channel Link")
 | 
			
		||||
	flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
 | 
			
		||||
	flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
 | 
			
		||||
	flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
 | 
			
		||||
	flag.StringVar(&c.RSSFile, "rss", c.RSSFile, "RSS file")
 | 
			
		||||
	flag.StringVar(&c.Title, "title", c.Title, "Channel title")
 | 
			
		||||
	flag.StringVar(&c.Title, "title", c.Title, "channel title")
 | 
			
		||||
	flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
 | 
			
		||||
	flag.IntVar(&port, "port", port, "port")
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
 | 
			
		||||
	c.FirebaseKey, err = filepath.Abs(c.FirebaseKey)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error finding absolute path for Firebase service account key file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.KeyFile, err = filepath.Abs(c.KeyFile)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error finding absolute path for key file: %v", err)
 | 
			
		||||
 
 | 
			
		||||
@@ -12,12 +12,12 @@ type Client struct {
 | 
			
		||||
	*auth.Client
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewClient() (*Client, error) {
 | 
			
		||||
func NewClient(c *Config) (*Client, error) {
 | 
			
		||||
	var err error
 | 
			
		||||
	client := new(Client)
 | 
			
		||||
 | 
			
		||||
	ctx := context.Background()
 | 
			
		||||
	opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")
 | 
			
		||||
	opt := option.WithCredentialsFile(c.FirebaseKey)
 | 
			
		||||
 | 
			
		||||
	app, err := firebase.NewApp(ctx, nil, opt)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ import (
 | 
			
		||||
 | 
			
		||||
func ServePDFList(c *b.Config) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		if tokenIsVerified(w, r) {
 | 
			
		||||
		if tokenIsVerified(w, r, c) {
 | 
			
		||||
			files, err := os.ReadDir(c.PDFDir)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println(err)
 | 
			
		||||
@@ -37,7 +37,7 @@ func ServePDFList(c *b.Config) http.HandlerFunc {
 | 
			
		||||
 | 
			
		||||
func ServePDF(c *b.Config) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		if tokenIsVerified(w, r) {
 | 
			
		||||
		if tokenIsVerified(w, r, c) {
 | 
			
		||||
			http.ServeFile(w, r, fmt.Sprint(c.PDFDir, "/", r.PathValue("id")))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ import (
 | 
			
		||||
 | 
			
		||||
func ServeRSS(c *b.Config) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		if tokenIsVerified(w, r) {
 | 
			
		||||
		if tokenIsVerified(w, r, c) {
 | 
			
		||||
			http.ServeFile(w, r, c.RSSFile)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// tokenIsVerified verifies that a request is authorized. It returns a bool.
 | 
			
		||||
func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
 | 
			
		||||
func tokenIsVerified(w http.ResponseWriter, r *http.Request, c *b.Config) bool {
 | 
			
		||||
	idToken := r.Header.Get("Authorization")
 | 
			
		||||
	if idToken == "" {
 | 
			
		||||
		log.Println("Authorization header missing")
 | 
			
		||||
@@ -16,7 +16,7 @@ func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	client, err := b.NewClient()
 | 
			
		||||
	client, err := b.NewClient(c)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user