Skip to content

Commit

Permalink
Load key from config
Browse files Browse the repository at this point in the history
  • Loading branch information
peteretelej committed Aug 21, 2023
1 parent ed2d27a commit fb48a78
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
Expand All @@ -22,7 +21,7 @@ type App struct {
}

type Config struct {
key string
Key string
Apps []App `json:"apps"`
}

Expand Down Expand Up @@ -52,24 +51,18 @@ func main() {
}

func (c *Config) loadConfig() error {
configFile, err := os.Open("config.json")
if err != nil {
log.Fatal()
}
defer configFile.Close()

byteValue, err := io.ReadAll(configFile)
dat, err := os.ReadFile("config.json")
if err != nil {
return err
}

if err := json.Unmarshal(byteValue, &config); err != nil {
if err := json.Unmarshal(dat, &config); err != nil {
return fmt.Errorf("failed to parse config json: %v", err)
}
if config.key == "" {
if config.Key == "" {
return errors.New("no key found in config.json. Please add a key, see config.json.sample")
}
if config.key == "EXECAPI_KEY_HERE" {
if config.Key == "EXECAPI_KEY_HERE" {
return errors.New("please add a custom key to the config.json.")
}
if len(config.Apps) == 0 {
Expand All @@ -83,7 +76,7 @@ func (c *Config) loadConfig() error {
}

func handleRun(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer "+config.key {
if r.Header.Get("Authorization") != "Bearer "+config.Key {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
Expand Down

0 comments on commit fb48a78

Please sign in to comment.