From 5ccd438fbf4d2f5a3fa359788fe6b0bfd4b3b2e4 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 25 Sep 2022 22:16:21 -0400 Subject: [PATCH] Fix ignore --- .gitignore | 1 + cmd/blas/main.go | 30 +++++++++++++++++ pkg/server/authorize.go | 71 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 cmd/blas/main.go create mode 100644 pkg/server/authorize.go diff --git a/.gitignore b/.gitignore index 1d00a46..3a86718 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ blas +!cmd/blas/ Session.vim coverage.txt diff --git a/cmd/blas/main.go b/cmd/blas/main.go new file mode 100644 index 0000000..7691964 --- /dev/null +++ b/cmd/blas/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "log" + + "dynatron.me/x/blasphem/internal/common" + "dynatron.me/x/blasphem/pkg/cmd/serve" + "dynatron.me/x/blasphem/pkg/config" + + "github.com/spf13/cobra" +) + +func main() { + rootCmd := cobra.Command{ + Use: common.AppName, + } + + config, err := config.ReadConfig() + if err != nil { + log.Fatal(err) + } + + rootCmd.AddCommand(serve.Command(config)) + // rootCmd.AddCommand(package.MyCommand([conf]), ...) + + err = rootCmd.Execute() + if err != nil { + panic(err) + } +} diff --git a/pkg/server/authorize.go b/pkg/server/authorize.go new file mode 100644 index 0000000..e70e57f --- /dev/null +++ b/pkg/server/authorize.go @@ -0,0 +1,71 @@ +package server + +import ( + "encoding/json" + "io" + "net/http" +) + +type AuthProvider interface { + ProviderName() string + ProviderID() *string + ProviderType() string +} + +type AuthProviderBase struct { + Name string `json:"name"` + ID *string `json:"id"` + Type string `json:"type"` +} + + +func (bp *AuthProviderBase) ProviderName() string { return bp.Name } +func (bp *AuthProviderBase) ProviderID() *string { return bp.ID } +func (bp *AuthProviderBase) ProviderType() string { return bp.Type } + +type LocalProvider struct { + AuthProviderBase +} + +func hassProvider() *LocalProvider { + return &LocalProvider{ + AuthProviderBase: AuthProviderBase{ + Name: "Home Assistant Local", + Type: "homeassistant", + }, + } +} + +// TODO: make this configurable +func (s *Server) providersHandler(w http.ResponseWriter, r *http.Request) { + providers := []AuthProvider{ + hassProvider(), + } + + rjs, err := json.Marshal(providers) + if err != nil { + panic(err) + } + + logRequest(http.StatusOK, r) + + _, err = w.Write(rjs) + if err != nil { + panic(err) + } +} + +func (s *Server) authorizeHandler(w http.ResponseWriter, r *http.Request) { + authPage, err := s.rootFS.Open("authorize.html") + if err != nil { + panic(err) + } + defer authPage.Close() + + logRequest(http.StatusOK, r) + + _, err = io.Copy(w, authPage) + if err != nil { + panic(err) + } +}