-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose HTTP endpoint to trigger a poll (#48)
* expose HTTP endpoint to trigger a poll * add Procfile * update CI & CD configs * add basic auth * add warning logs * update readme & improve logging
- Loading branch information
Showing
12 changed files
with
180 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: bin/server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
|
||
"github.com/utkuufuk/entrello/internal/config" | ||
"github.com/utkuufuk/entrello/internal/logger" | ||
"github.com/utkuufuk/entrello/internal/service" | ||
) | ||
|
||
func main() { | ||
var configFile string | ||
flag.StringVar(&configFile, "c", "config.json", "config file path") | ||
flag.Parse() | ||
|
||
cfg, err := config.ReadConfig(configFile) | ||
if err != nil { | ||
log.Fatalf("Could not read configuration: %v", err) | ||
} | ||
|
||
if err = service.Poll(cfg); err != nil { | ||
logger.Error(err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/utkuufuk/entrello/internal/config" | ||
"github.com/utkuufuk/entrello/internal/logger" | ||
"github.com/utkuufuk/entrello/internal/service" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", handler) | ||
http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), nil) | ||
} | ||
|
||
func handler(w http.ResponseWriter, req *http.Request) { | ||
if req.Method != http.MethodPost { | ||
logger.Warn("Method %s not allowed", req.Method) | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
user, pwd, ok := req.BasicAuth() | ||
if !ok { | ||
logger.Warn("Could not parse basic auth.") | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
if user != os.Getenv("USERNAME") { | ||
logger.Warn("Invalid user name: %s", user) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
if pwd != os.Getenv("PASSWORD") { | ||
logger.Warn("Invalid password: %s", pwd) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
body, err := ioutil.ReadAll(req.Body) | ||
if err != nil { | ||
logger.Error("Could not read request body: %v", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
var cfg config.Config | ||
if err = json.Unmarshal(body, &cfg); err != nil { | ||
logger.Warn("Invalid request body: %v", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err = service.Poll(cfg); err != nil { | ||
logger.Error(err.Error()) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/utkuufuk/entrello/internal/config" | ||
"github.com/utkuufuk/entrello/pkg/trello" | ||
) | ||
|
||
func Poll(cfg config.Config) error { | ||
loc, err := time.LoadLocation(cfg.TimezoneLocation) | ||
if err != nil { | ||
return fmt.Errorf("invalid timezone location: %v", loc) | ||
} | ||
|
||
sources, labels := getSources(cfg.Sources, time.Now().In(loc)) | ||
if len(sources) == 0 { | ||
return nil | ||
} | ||
|
||
client, err := trello.NewClient(cfg.Trello) | ||
if err != nil { | ||
return fmt.Errorf("could not create trello client: %v", err) | ||
} | ||
|
||
if err := client.LoadBoard(labels); err != nil { | ||
return fmt.Errorf("Could not load existing cards from the board: %v", err) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(len(sources)) | ||
for _, src := range sources { | ||
go process(src, client, &wg) | ||
} | ||
wg.Wait() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package service | ||
|
||
import ( | ||
"encoding/json" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
|