Skip to content

Commit

Permalink
Register mandatory and non-mandatory env vars
Browse files Browse the repository at this point in the history
This commit closes #21
  • Loading branch information
lucasgomide committed Oct 2, 2018
1 parent 30fb2f8 commit 6512a16
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
19 changes: 16 additions & 3 deletions server/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

## Run locally

* Install [dep](https://github.com/golang/dep)
* Run the following commands:
- Install [dep](https://github.com/golang/dep)
- Run the following commands:

```bash
dep init
dep ensure
PORT=8080 ENCRYPTION_KEY="the-key-has-to-be-32-bytes-long!" go run main.go
USERPASSWD="userpassword" ENCRYPTION_KEY="the-key-has-to-be-32-bytes-long!" go run main.go
```

The following environment variables are accepted:

```bash
# mandatory variables
export USERPASSWD="userpassword"
export ENCRYPTION_KEY="the-key-has-to-be-32-bytes-long!"

# non-mandatory variables
export PUBLIC_HTML="public"
export PORT="8081"
export MONGODB_URI="mongodb://127.0.0.1:27017/db"
```
41 changes: 23 additions & 18 deletions server/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"

"github.com/danielfireman/temp-to-go/server/tsmongo"
"github.com/gorilla/sessions"
"github.com/kelseyhightower/envconfig"
"github.com/labstack/echo"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/middleware"
Expand All @@ -21,29 +21,37 @@ const (
restrictedPath = "/restricted"
)

// Specification represents a map of enviornment variables
type Specification struct {
UserPassword string `envconfig:"USERPASSWD" required:"true"`
EncryptionKey string `split_words:"true" required:"true"`

MongodbURI string `default:"mongodb://127.0.0.1:27017/db" split_words:"true"`
Port string `default:"8080"`
PublicHTML string `split_words:"true" default:"public"`
}

func main() {
userPasswd := os.Getenv("USERPASSWD")
if userPasswd == "" {
log.Fatal("USERPASSWD must be set.")
var spec Specification
err := envconfig.Process("", &spec)
if err != nil {
log.Fatal(err.Error())
}
key := []byte(os.Getenv("ENCRYPTION_KEY"))

key := []byte(spec.EncryptionKey)
if len(key) != 32 {
log.Fatalf("ENCRYPTION_KEY must be 32-bytes long. Current key is \"%s\" which is %d bytes long.", key, len(key))
}
mgoURI := os.Getenv("MONGODB_URI")
if mgoURI == "" {
log.Fatalf("Invalid MONGODB_URI: %s", mgoURI)
}
tsmongoSession, err := tsmongo.Dial(mgoURI)
tsmongoSession, err := tsmongo.Dial(spec.MongodbURI)
if err != nil {
log.Fatalf("Error connecting to StatusDB: %s", mgoURI)
log.Fatalf("Error connecting to StatusDB: %s", spec.MongodbURI)
}
defer tsmongoSession.Close()
fanService := tsmongo.NewFanService(tsmongoSession)
bedroomService := tsmongo.NewBedroomService(tsmongoSession)
weatherService := tsmongo.NewWeatherService(tsmongoSession)

publicHTML := filepath.Join(os.Getenv("PUBLIC_HTML"))
publicHTML := filepath.Join(spec.PublicHTML)

// Initializing web framework.
e := echo.New()
Expand All @@ -62,7 +70,7 @@ func main() {

// Public Routes.
bedroomAPIHandler := bedroomAPIHandler{key, bedroomService}
loginHandler := loginHandler{userPasswd}
loginHandler := loginHandler{spec.UserPassword}
e.File("/", filepath.Join(publicHTML, "index.html"))
e.File("/favicon.ico", filepath.Join(publicHTML, "favicon.ico"))
e.Static("/", publicHTML)
Expand All @@ -83,12 +91,9 @@ func main() {
restricted.GET("/indoortemp", bedroomAPIHandler.handleGet)

// Starting server.
port := os.Getenv("PORT")
if port == "" {
log.Fatalf("Invalid PORT: %s", port)
}

s := &http.Server{
Addr: ":" + port,
Addr: ":" + spec.Port,
ReadTimeout: 5 * time.Minute,
WriteTimeout: 5 * time.Minute,
}
Expand Down

0 comments on commit 6512a16

Please sign in to comment.