Skip to content

Commit

Permalink
initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Urban Ishimwe committed Apr 20, 2020
1 parent dc1d1e2 commit 2d8bfc2
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fmt:
gofmt -w . || go fmt -w .

test:
go test -v -race ./...
go test -v -race -coverprofile=coverage.coverprofile -covermode=atomic -tags integration ./...

build:
go build -o bin/main main.go
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.7.4
github.com/joho/godotenv v1.3.0
github.com/slack-go/slack v0.6.3 // indirect
github.com/slack-go/slack v0.6.3
go.mongodb.org/mongo-driver v1.3.2
)
33 changes: 33 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package main

import (
"context"
"net/http"
"os"
"os/signal"
"time"

"github.com/rwandaopensource/botx/pkg/database"
"github.com/rwandaopensource/botx/pkg/helper"
"github.com/rwandaopensource/botx/pkg/route"
"github.com/rwandaopensource/botx/pkg/util"
)

Expand All @@ -11,4 +19,29 @@ func main() {
if util.Command() {
return
}

s := &http.Server{
Addr: route.ADDR,
Handler: route.Router(),
ReadTimeout: 20 * time.Second,
WriteTimeout: 20 * time.Second,
MaxHeaderBytes: 1 << 20,
}
helper.Print("starting server", route.ADDR)

go func() {
helper.FatalError(s.ListenAndServe(), "")
}()

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

// Block until we receive our signal.
<-c

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
helper.Verbose("gracefully shutting down")
s.Shutdown(ctx)
return
}
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Config(enforce bool) {
mode string = os.Getenv("GO_ENV")
file = ".env"
)
// if you want to enforce the environment variables don't run test with GO_ENV
// if you want to enforce the environment during tests don't run set GO_ENV=test
enforce = mode == "test"
switch mode {
case "development", "production", "test":
Expand Down
17 changes: 17 additions & 0 deletions pkg/route/addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package route

import (
"os"
)

// ADDR of the server
var ADDR string

func init() {
addr := os.Getenv("PORT")
if addr == "" {
ADDR = ":8080"
return
}
ADDR = ":" + addr
}
11 changes: 11 additions & 0 deletions pkg/route/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package route

// import (
// "net/http"

// "github.com/gorilla/mux"
// )

// func CommandRoute(r *mux.Route) {
// r.HandlerFunc().Methods(http.MethodPost)
// }
34 changes: 30 additions & 4 deletions pkg/route/route.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
package route

import (
"net/http"
"os"

"github.com/gorilla/mux"
)

var R *mux.Router

// Router the
func Router() *mux.Router {
R := mux.NewRouter()
return R
var origin = os.Getenv("ORIGIN")
if origin == "" {
origin = "*"
}

r := mux.NewRouter()

// HOME ROUTE == PONG
r.HandleFunc("/", func(rw http.ResponseWriter, _ *http.Request) {
rw.Write([]byte("PONG"))
}).Methods(http.MethodGet, http.MethodPost)

// OPTIONS request
r.Methods(http.MethodOptions).HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE")
rw.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "true")
rw.WriteHeader(http.StatusNoContent)
})

r.HandleFunc("*", func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusNotFound)
}).Methods(http.MethodGet, http.MethodPost)

return r
}
26 changes: 26 additions & 0 deletions slack/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package slack

import (
"fmt"
"os"
)

// InstallHTML return html for installing app
func InstallHTML() string {
slackClientID := os.Getenv("SLACK_CLIENT_ID")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
host := "http://localhost:" + port
return fmt.Sprintf(`
<html lang="en">
<head>
<title> Install slack </tile>
</head>
<h1> Install this app in your slack workspace </h1>
<a href="https://slack.com/oauth/authorize?client_id=%s&scope=im:write,chat:write,users:read,reactions:read&redirect_uri=%s"
</html>
`, slackClientID)
}
18 changes: 18 additions & 0 deletions slack/oauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package slack

import (
"net/http"
"os"

"github.com/slack-go/slack"
)

var (
slackClientID = os.Getenv("SLACK_CLIENT_ID")
slackClientSecret = os.Getenv("SLACK_CLIENT_SECRET")
)

// OAuthToken return slack authentication on a worskpace
func OAuthToken(code string, redirectURI string) (string, string, error) {
return slack.GetOAuthToken(http.DefaultClient, slackClientID, slackClientSecret, code, redirectURI)
}

0 comments on commit 2d8bfc2

Please sign in to comment.