This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
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.
feat: add api routes and multiple handler
- Loading branch information
Nicolas Levée
committed
Apr 19, 2020
1 parent
2c1f8fa
commit ebb79a0
Showing
12 changed files
with
334 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export WOOS_KEY=woos-abcdefghijklmnop0123456789 | ||
export WOOS_DOMAIN=mondomaine.fr |
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 |
---|---|---|
|
@@ -13,3 +13,5 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
vendor/ | ||
|
||
.env |
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,16 @@ | ||
package api | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
// StartServer demarrage du server | ||
func StartServer(host string, port string) { | ||
r := mux.NewRouter() | ||
addStoreRoutes(r) | ||
addScrapperRoutes(r) | ||
log.Fatal(http.ListenAndServe(host+":"+port, r)) | ||
} |
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 api | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/nlevee/go-auchan-drive-checker/pkg/auchan" | ||
) | ||
|
||
// AddScrapperRoutes populate router | ||
func addScrapperRoutes(r *mux.Router) { | ||
// ajoute un scrapper sur le store : storeid | ||
r.HandleFunc("/scrappers/{storeid}", addScrapper).Methods(http.MethodPut) | ||
// récupère l'état du scrapper sur le store : storeid | ||
r.HandleFunc("/scrappers/{storeid}", getScrapperState).Methods(http.MethodGet) | ||
} | ||
|
||
// GetScrapperState récuperation d'un état de scrapper | ||
func getScrapperState(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
vars := mux.Vars(r) | ||
log.Printf("storeId: %v\n", vars["storeid"]) | ||
|
||
json.NewEncoder(w).Encode(auchan.GetDriveState(vars["storeid"])) | ||
} | ||
|
||
// AddScrapper ajoute un scrapper | ||
func addScrapper(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusCreated) | ||
|
||
vars := mux.Vars(r) | ||
log.Printf("storeId: %v\n", vars["storeid"]) | ||
|
||
go auchan.NewDriveHandler(vars["storeid"]) | ||
} |
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,32 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/nlevee/go-auchan-drive-checker/pkg/auchan" | ||
) | ||
|
||
// AddStoreRoutes populate router | ||
func addStoreRoutes(r *mux.Router) { | ||
// récuperation des stores | ||
r.HandleFunc("/stores", getStores).Methods(http.MethodGet) | ||
} | ||
|
||
// GetStores récupere la liste des stores filtrés | ||
func getStores(w http.ResponseWriter, r *http.Request) { | ||
params := r.URL.Query() | ||
cp := params["postalCode"] | ||
if len(cp) == 0 { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
storeIDs, _ := auchan.GetStoreByPostalCode(string(cp[0])) | ||
|
||
json.NewEncoder(w).Encode(storeIDs) | ||
} |
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
Oops, something went wrong.