Skip to content

Commit

Permalink
added env
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Jan 21, 2020
1 parent 63b1421 commit cae4fd3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HASHID_LENGTH=
TOPICS_SALT=
ARTICLES_SALT=
AUTHORS_SALT=
PATRONS_SALT=
HACKDOOR_ENDPOINT=
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fabric.properties
# Sonarlint plugin
.idea/**/sonarlint/
.idea
Makefile
.env

# SonarQube Plugin
.idea/**/sonarIssues.xml
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PORT=8080
MAIN=main.go

run:
ENV=production go run $(MAIN)

dev:
ENV=development PORT=$(PORT) go run $(MAIN)
33 changes: 18 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/speps/go-hashids"
"log"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -45,26 +47,27 @@ func makeHashId(id string, target string) string {

func main() {

if os.Getenv("ENV") == "development" {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}

port := os.Getenv("PORT")
hackdoor := os.Getenv("HACKDOOR_ENDPOINT")

var dynamicRoutes = [4]string{"articles", "tags", "authors", "patrons"}

for _, route := range dynamicRoutes {

fmt.Printf("Created route /%s\n", route)
http.HandleFunc("/{target:articles|tags|authors|patrons}/{id:[0-9]+}/{path:(\\w+?_?-?\\d?)+}", func (w http.ResponseWriter, r *http.Request) {
pathVars := mux.Vars(r)
target := pathVars["target"]
id := pathVars["id"]
path := pathVars["path"]
hashId := makeHashId(id, target)

http.HandleFunc("/" + route + "/{id:[0-9]+}/{path:(\\w+?_?-?\\d?)+}", func (w http.ResponseWriter, r *http.Request) {
pathVars := mux.Vars(r)
id := pathVars["id"]
path := pathVars["path"]
hashId := makeHashId(id, route)
redirectPath := hackdoor + "/" + target + "/" + hashId + "/" + path

redirectPath := hackdoor + "/" + route + "/" + hashId + "/" + path

http.Redirect(w, r, redirectPath, http.StatusPermanentRedirect)
})
}
http.Redirect(w, r, redirectPath, http.StatusPermanentRedirect)
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("[ROUTER] -> %s\n", r.URL.Path)
Expand Down

0 comments on commit cae4fd3

Please sign in to comment.