Adding approval process
This commit is contained in:
132
cmd/hub/main.go
132
cmd/hub/main.go
@@ -1,74 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
"go.fixergrid.net/servicedemon/pkg/discord"
|
||||
"go.fixergrid.net/servicedemon/pkg/pubsub"
|
||||
"go.fixergrid.net/servicedemon/pkg/registrar"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Starting .... the >HUB<")
|
||||
|
||||
token, isSet := os.LookupEnv("DISCORD_BOT_TOKEN")
|
||||
if !isSet {
|
||||
fmt.Println("please set the environment variable 'DISCORD_BOT_TOKEN'")
|
||||
return
|
||||
}
|
||||
|
||||
tokensrc := oauth2.StaticTokenSource(&oauth2.Token{
|
||||
AccessToken: token,
|
||||
TokenType: "Bot",
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
client := discord.
|
||||
NewClient(ctx, tokensrc).
|
||||
WithDefaultChannel("1125162127133523978")
|
||||
|
||||
resp, err := client.Get("https://discord.com/api/v10/channels/1125162127133523978")
|
||||
if err != nil {
|
||||
fmt.Printf("there was an error making the request: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
out, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to read response body: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Got response:\n%s\n", out)
|
||||
|
||||
messageContent := map[string]interface{}{
|
||||
"content": "Hello, world!",
|
||||
}
|
||||
|
||||
b, err := json.Marshal(messageContent)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to marshal message: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
r2, err := client.Post("https://discord.com/api/v10/channels/1125162127133523978/messages", "application/json", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
fmt.Printf("failed tdo send message to server: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
out, err = ioutil.ReadAll(r2.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to read response body on send message: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("status: %v - res: %v\n", r2.Status, out)
|
||||
type noopHandler struct {
|
||||
http.HandlerFunc
|
||||
}
|
||||
|
||||
func wrapHandlefunc(h http.HandlerFunc) noopHandler {
|
||||
return noopHandler{
|
||||
HandlerFunc: h,
|
||||
}
|
||||
}
|
||||
|
||||
func (h noopHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
h.HandlerFunc(w, req)
|
||||
}
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
logger := log.New(os.Stdout, "main: ", log.LstdFlags|log.Lshortfile)
|
||||
logger.Println("Starting .... the >HUB<")
|
||||
|
||||
pubsub := pubsub.New()
|
||||
repo := registrar.NewRepo()
|
||||
|
||||
r := registrar.NewRegistrar(
|
||||
pubsub,
|
||||
repo,
|
||||
registrar.WithLogger(log.New(os.Stdout, "registrar: ", log.LstdFlags|log.Lshortfile)),
|
||||
)
|
||||
al := registrar.NewApprovalListener(
|
||||
pubsub,
|
||||
nil,
|
||||
registrar.OptionLog(log.New(os.Stdout, "approvalListener: ", log.LstdFlags|log.Lshortfile)),
|
||||
)
|
||||
go al.Run(ctx)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/register", r.HandleRegistration)
|
||||
mux.Handle("/approvals/", http.StripPrefix("/approvals/", wrapHandlefunc(r.HandleApproval)))
|
||||
|
||||
certFile, err := os.Open("./certs/ca.pem")
|
||||
if err != nil {
|
||||
logger.Fatalf("failed to open ca.pem: %v", err)
|
||||
}
|
||||
|
||||
caCert, err := io.ReadAll(certFile)
|
||||
if err != nil {
|
||||
logger.Fatalf("failed to read in ca: %v", err)
|
||||
}
|
||||
|
||||
pool := x509.NewCertPool()
|
||||
pool.AppendCertsFromPEM(caCert)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: ":3001",
|
||||
TLSConfig: &tls.Config{
|
||||
ClientCAs: pool,
|
||||
ClientAuth: tls.RequireAndVerifyClientCert,
|
||||
},
|
||||
}
|
||||
server.Handler = mux
|
||||
log.Println(server.ListenAndServeTLS("./certs/combined.pem", "./certs/server-key.pem"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user