[couchdb] konektor

This commit is contained in:
monastyr 2018-11-18 17:29:47 +01:00
parent f005452baf
commit 01b6a6e1c8
8 changed files with 227 additions and 0 deletions

View File

@ -0,0 +1,11 @@
1. Adres Bazy
http://127.0.0.1:5984/
2. Wyszuanie wszystkich baz przez curl
curl -X GET http://127.0.0.1:5984/_all_dbs
3. Futon - webowy klient
http://localhost:5984/_utils/
4. Baza
go_test

View File

@ -0,0 +1,68 @@
package main
import (
"fmt"
"net/url"
"github.com/zemirco/couchdb"
)
// create your own document
type dummyDocument struct {
couchdb.Document
Foo string `json:"foo"`
Beep string `json:"beep"`
}
// start
func main() {
u, err := url.Parse("http://127.0.0.1:5984/")
if err != nil {
panic(err)
}
// create a new client
client, err := couchdb.NewClient(u)
if err != nil {
panic(err)
}
// get some information about your CouchDB
info, err := client.Info()
if err != nil {
panic(err)
}
fmt.Println(info)
// // create a database
// if _, err = client.Create("dummy"); err != nil {
// panic(err)
// }
// use your new "dummy" database and create a document
db := client.Use("dummy")
doc := &dummyDocument{
Foo: "bar",
Beep: "bopp",
}
result, err := db.Post(doc)
if err != nil {
panic(err)
}
// get id and current revision.
if err := db.Get(doc, result.ID); err != nil {
panic(err)
}
// delete document
if _, err = db.Delete(doc); err != nil {
panic(err)
}
// and finally delete the database
if _, err = client.Delete("dummy"); err != nil {
panic(err)
}
}

View File

@ -0,0 +1,3 @@
module github.com/pawlaczyk/go_jwt/client
require github.com/dgrijalva/jwt-go v3.2.0+incompatible

View File

@ -0,0 +1,2 @@
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

View File

@ -0,0 +1,78 @@
package main
// go get github.com/dgrijalva/jwt-go
//cd client
// go mod init github.com/pawlaczyk/go-jwt/client
// [!] kropka przed nawiasami w token.Claims.(jwt.MapClaims)
//inaczej blad
//# command-line-arguments
// .\main.go:14:24: type jwt.MapClaims is not an expression
// .\main.go:14:24: cannot call non-function token.Claims (type jwt.Claims)
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
jwt "github.com/dgrijalva/jwt-go" //jwt jako alias do repo
)
var mySigningKey = []byte("mysupersecretphrase") //lepiej odczytywac to ze zmiennej srodowiskowej
// set My_JWT_TOKEN=mysupersecretphrase
//var mySigningKey = os.Get("My_JWT_TOKEN") dobra praktyka dla jakiegogolwkiek hasła, chroni przed upubliczniniem gdzies hasla na repo
func homePage(w http.ResponseWriter, r *http.Request) {
validToken, err := GenerateJWT()
if err != nil {
fmt.Fprintf(w, err.Error())
}
client := &http.Client{} //tworzy nowy Request
req, _ := http.NewRequest("GET", "http://localhost:9000/", nil)
req.Header.Set("Token", validToken) //dopisanie do hedera
res, err := client.Do(req)
if err != nil {
//jak cos nie tak
fmt.Fprintf(w, "Error: %s", err.Error())
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Fprintf(w, err.Error())
}
fmt.Fprintf(w, string(body))
}
func handleRequests() {
http.HandleFunc("/", homePage)
log.Fatal(http.ListenAndServe(":9001", nil))
}
func GenerateJWT() (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims) //[!] Kropka przed nawiasami !!!!!!!!!
claims["authorized"] = true
claims["client"] = "Elliot Forbes"
claims["exp"] = time.Now().Add(time.Minute * 30).Unix()
tokenString, err := token.SignedString(mySigningKey)
if err != nil {
fmt.Println("Somethig get wrong %s", err.Error())
return "", err
}
return tokenString, nil
}
func main() {
fmt.Println("My Simple Client")
handleRequests()
}

View File

@ -0,0 +1,3 @@
module github.com/pawlaczyk/go-jwt/server
require github.com/dgrijalva/jwt-go v3.2.0+incompatible

View File

@ -0,0 +1,2 @@
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

View File

@ -0,0 +1,60 @@
package main
import (
"fmt"
"log"
"net/http"
jwt "github.com/dgrijalva/jwt-go" //jwt jako alias do repo =~ import numpy as np ; jwt to takie np
)
var mySigningKey = []byte("mysupersecretphrase")
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Super Secret Information from Server")
fmt.Println("Endpoint Hit: homePage")
}
func isAuthorized(endpoint func(http.ResponseWriter, *http.Request)) http.Handler {
//modyfikacja funkcji homePage =~ jak dektorator w pythonie
//sprawdza czy jest prawidlowy JWTToken
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { //[!] handleR nie samo handle
if r.Header["Token"] != nil {
//walidacja
//parsowanie naglowka z tokenem
// token *jwt.Token token to zmienna - [!] bez przecinka w func(token *jwt.Token) (interface{}, error)
// inaczej bład:
//# command-line-arguments
// .\main.go:26:55: undefined: token
// .\main.go:27:17: undefined: token
token, err := jwt.Parse(r.Header["Token"][0], func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("There was an error")
}
return mySigningKey, nil //nil zamias errora
})
if err != nil {
fmt.Fprintf(w, err.Error())
}
if token.Valid {
endpoint(w, r) //jak wszystko ok, to zwraca oryginalnego endpointa, przekazanego jako argument
}
} else {
//brak tokena
fmt.Fprintf(w, "Not Authorized")
}
})
}
func handleRequests() {
http.Handle("/", isAuthorized(homePage))
log.Fatal(http.ListenAndServe(":9000", nil))
}
func main() {
handleRequests()
}