├── README.md ├── best-practices ├── context-http-server │ └── main.go ├── context-loop │ └── main.go ├── context-value-nono │ └── main.go ├── context-value │ └── main.go ├── context │ └── main.go ├── empty-string-check │ └── main.go ├── error-behaviour │ ├── external_lib.go │ └── main.go ├── error-handling-2 │ ├── main.go │ └── test.txt ├── error-handling-3 │ ├── main.go │ └── test.txt ├── error-handling │ ├── main.go │ └── test.txt ├── file-io-large │ └── main.go ├── file-io │ ├── main.go │ └── test.txt ├── force-interface │ └── main.go ├── goroutine-pooling │ └── main.go ├── logging-defer │ └── main.go ├── map-contains │ └── main.go ├── project-structure │ ├── cmd-sample │ │ ├── cmd │ │ │ ├── myapp │ │ │ │ └── myapp.go │ │ │ └── mycli │ │ │ │ └── mycli.go │ │ ├── handler.go │ │ ├── myapp │ │ ├── registration.go │ │ └── service.go │ ├── dependency-sample │ │ ├── cmd │ │ │ └── main.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── http │ │ │ └── rest │ │ │ │ ├── handler.go │ │ │ │ └── handler_test.go │ │ ├── mocks │ │ │ └── user_service.go │ │ ├── postgres │ │ │ └── user_service.go │ │ └── user.go │ ├── flat-sample │ │ ├── handler.go │ │ ├── main.go │ │ ├── registration.go │ │ └── service.go │ ├── internal-sample │ │ ├── a │ │ │ └── b │ │ │ │ ├── c │ │ │ │ ├── g │ │ │ │ │ └── app.go │ │ │ │ ├── internal │ │ │ │ │ └── d │ │ │ │ │ │ └── e │ │ │ │ │ │ └── f │ │ │ │ │ │ └── accessor.go │ │ │ │ └── runner.go │ │ │ │ └── g │ │ │ │ └── helper.go │ │ └── go.mod │ ├── layer-sample │ │ ├── cmd │ │ │ └── main.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── handlers │ │ │ ├── customer.go │ │ │ └── registration.go │ │ ├── models │ │ │ ├── customer.go │ │ │ └── registration.go │ │ ├── services │ │ │ ├── customer.go │ │ │ └── registration.go │ │ └── storage │ │ │ ├── db.go │ │ │ └── xml.go │ ├── module-sample │ │ ├── cmd │ │ │ └── main.go │ │ ├── registration │ │ │ ├── handler.go │ │ │ ├── registration.go │ │ │ └── service.go │ │ ├── storage │ │ │ ├── db.go │ │ │ └── xml.go │ │ └── user │ │ │ ├── handler.go │ │ │ ├── service.go │ │ │ └── user.go │ └── packagename │ │ ├── cmd │ │ └── main.go │ │ ├── go.mod │ │ └── util │ │ └── log.go ├── single-method-interface │ └── main.go ├── struct-to-text │ └── main.go └── synchron-api │ └── main.go ├── cloud-provider ├── aws-beanstak-sample │ ├── .gitignore │ ├── Buildfile │ ├── Dockerfile │ ├── Procfile │ ├── README.txt │ ├── build.sh │ ├── go.mod │ └── main.go ├── aws-lambda-go │ ├── README.txt │ ├── function.zip │ └── main.go ├── azure-kubernetes │ ├── Dockerfile │ ├── README.txt │ ├── main.go │ └── manifest.yml ├── docker-compose │ ├── Dockerfile.receiver │ ├── Dockerfile.sender │ ├── cmd │ │ ├── receiver │ │ │ └── main.go │ │ └── sender │ │ │ └── main.go │ ├── docker-compose.yml │ ├── go.mod │ └── go.sum ├── docker-sample-multistage │ ├── Dockerfile │ └── main.go ├── docker-sample │ ├── Dockerfile │ └── main.go ├── go-cloud │ ├── Dockerfile │ ├── docker-compose.yml │ ├── go.mod │ ├── go.sum │ ├── main.go │ ├── manifest.yml │ └── minio │ │ └── gocloud │ │ └── gopher.png ├── google-cloud-hello │ ├── .gitignore │ ├── .idea │ │ ├── .gitignore │ │ ├── externalDependencies.xml │ │ ├── go-hello-world.iml │ │ └── modules.xml │ ├── .vscode │ │ ├── extensions.json │ │ ├── launch.json │ │ └── tasks.json │ ├── Dockerfile │ ├── README.md │ ├── cmd │ │ └── hello-world │ │ │ └── main.go │ ├── go.mod │ ├── go.sum │ ├── img │ │ └── diagram.png │ ├── kubernetes-manifests │ │ ├── hello.deployment.yaml │ │ └── hello.service.yaml │ └── skaffold.yaml ├── google-cloud-sample │ ├── .vscode │ │ └── launch.json │ ├── Dockerfile │ ├── README.txt │ ├── deployment-manifest.yml │ ├── main.go │ ├── service-manifest.yml │ └── skaffold.yaml └── minikube-sample │ ├── Dockerfile │ ├── README.txt │ ├── main.go │ └── manifest.yml ├── concurrency ├── best-practices-generator-function │ └── main.go ├── best-practices-multiplexer-select │ └── main.go ├── best-practices-multiplexer │ └── main.go ├── best-practices-sync │ └── main.go ├── best-practices-timeout │ └── main.go ├── channel-mit-close │ └── main.go ├── channel-range │ └── main.go ├── channels │ └── main.go ├── first-go-routine-mit-channel │ └── main.go ├── first-go-routine │ └── main.go ├── graceful-shutdown │ ├── .vscode │ │ └── settings.json │ ├── go.mod │ └── main.go └── java-threads-beispiel │ └── Main.java ├── configuration ├── application-configuration-os │ └── main.go ├── application-configuration-viper │ ├── config.yml │ ├── go.mod │ ├── go.sum │ └── main.go └── application-configuration │ ├── cmd │ └── main.go │ ├── configuration.go │ ├── go.mod │ └── go.sum ├── first-module-dependency ├── cmd │ └── main.go ├── go.mod ├── go.sum └── main.go ├── golang-language-samples ├── collection-array-slice │ └── collection.go ├── collection-array │ └── collection.go ├── collection-capa │ └── collection.go ├── collection-for-range │ └── collection.go ├── defer-file-open │ ├── defer.go │ └── testfile ├── defer-lock │ └── defer.go ├── defer │ └── defer.go ├── errors-new │ └── errors.go ├── errors-owntyp-wrap │ └── errors.go ├── errors-owntyp │ └── errors.go ├── errors │ └── errors.go ├── fmt-package │ └── main.go ├── for-range │ └── main.go ├── pointer-parameter │ └── pointer.go ├── pointer-receiver │ └── pointer.go ├── pointer-struct │ └── pointer.go ├── pointer │ └── pointer.go ├── sampledoc │ ├── first.go │ └── first_example_test.go ├── switch-case-falltrough │ └── switch-case.go ├── switch-case-no-expression │ └── switch-case.go ├── switch-case │ └── switch-case.go ├── type-alias-definition │ └── type-definition.go ├── type-definition-assertion │ └── type-definition.go ├── type-definition-interface-receiver │ └── type-definition.go ├── type-definition-interface │ └── type-definition.go ├── type-definition-struct │ └── type-definition.go ├── type-definition-switch │ └── type-definition.go ├── type-definition │ └── type-definition.go ├── type-failure │ └── type-failure.go └── varadic-parameter │ └── main.go ├── hello-channel-post └── main.go ├── hello-channel └── main.go ├── hello-go-channel └── main.go ├── hello-interface └── main.go ├── hello-world ├── cplusplus │ ├── README.md │ ├── a.out │ └── hello.cpp ├── go │ ├── README.md │ └── main.go ├── java │ ├── Main.java │ └── README.md ├── javascript │ ├── README.md │ └── index.js ├── python │ ├── README.md │ └── hello.py └── typescript │ ├── README.md │ ├── hello.js │ └── hello.ts ├── library-dependency ├── go.mod ├── go.sum └── main.go ├── microservices ├── container │ ├── Dockerfile │ ├── README.md │ └── main.go ├── default-mux-demo │ └── main.go ├── formrequest │ ├── Form-request.http │ ├── go.mod │ └── main.go ├── go-channel │ └── main.go ├── gorilla-mux │ ├── go.mod │ ├── go.sum │ └── main.go ├── gorm-hooks │ ├── go.mod │ ├── go.sum │ ├── main.go │ └── test.db ├── gorm-relation │ ├── go.mod │ ├── go.sum │ └── main.go ├── gorm │ ├── go.mod │ ├── go.sum │ └── main.go ├── grpc │ ├── client │ │ ├── cmd │ │ │ └── main.go │ │ ├── go.mod │ │ └── go.sum │ ├── hello │ │ ├── go.mod │ │ ├── go.sum │ │ ├── hello-message.pb.go │ │ ├── hello-message.proto │ │ └── hello-message_grpc.pb.go │ └── server │ │ ├── cmd │ │ └── main.go │ │ ├── go.mod │ │ ├── go.sum │ │ └── helloService.go ├── hanlder-impl │ ├── cmd │ │ └── main.go │ ├── go.mod │ ├── go.sum │ └── handler.go ├── http-client-circut-breaker │ ├── go.mod │ ├── go.sum │ └── main.go ├── http-client-config │ └── main.go ├── http-client-ssl-cert │ ├── certs │ │ ├── ca │ │ │ ├── certsdb │ │ │ │ └── 01.pem │ │ │ ├── index.txt │ │ │ ├── index.txt.attr │ │ │ ├── index.txt.attr.old │ │ │ ├── index.txt.old │ │ │ ├── serial │ │ │ └── serial.old │ │ ├── myRoot.crt │ │ ├── myRoot.key │ │ ├── myRoot.srl │ │ ├── server-cert.conf │ │ ├── server-cert.request.conf │ │ ├── server-chain.crt │ │ ├── test.example.crt │ │ ├── test.example.csr │ │ ├── test.example.key │ │ ├── user-cert.conf │ │ ├── user-cert.request.conf │ │ ├── user-client.p12 │ │ ├── user.crt │ │ ├── user.key │ │ └── user.req │ └── main.go ├── http-client │ └── main.go ├── http-metod-matching │ └── main.go ├── http-ratelimit │ ├── go.mod │ ├── go.sum │ └── main.go ├── http-registration │ ├── go.mod │ └── main.go ├── http-retry │ ├── client │ │ ├── go.mod │ │ ├── go.sum │ │ └── main.go │ └── server │ │ ├── go.mod │ │ ├── go.sum │ │ └── main.go ├── httpsserver │ ├── README.md │ ├── certs │ │ ├── myRoot.crt │ │ ├── myRoot.key │ │ ├── myRoot.srl │ │ ├── server-cert.conf │ │ ├── server-cert.request.conf │ │ ├── test.example.crt │ │ ├── test.example.csr │ │ └── test.example.key │ ├── go.mod │ ├── go.sum │ └── main.go ├── httpsservercert │ ├── README.md │ ├── certs │ │ ├── ca │ │ │ ├── certsdb │ │ │ │ └── 01.pem │ │ │ ├── index.txt │ │ │ ├── index.txt.attr │ │ │ ├── index.txt.attr.old │ │ │ ├── index.txt.old │ │ │ ├── serial │ │ │ └── serial.old │ │ ├── myRoot.crt │ │ ├── myRoot.key │ │ ├── myRoot.srl │ │ ├── server-cert.conf │ │ ├── server-cert.request.conf │ │ ├── server-chain.crt │ │ ├── test.example.crt │ │ ├── test.example.csr │ │ ├── test.example.key │ │ ├── user-cert.conf │ │ ├── user-cert.request.conf │ │ ├── user-client.p12 │ │ ├── user.crt │ │ ├── user.key │ │ └── user.req │ ├── go.mod │ ├── main.go │ └── test.example.key ├── jsonhandling │ └── main.go ├── jsonservice │ ├── GET-request.http │ ├── POST-request.http │ ├── go.mod │ ├── go.sum │ └── main.go ├── jwt │ └── server │ │ ├── go.mod │ │ ├── go.sum │ │ ├── jwt │ │ └── token.go │ │ └── main.go ├── letscrypt │ ├── go.mod │ └── main.go ├── logging │ ├── go.mod │ ├── go.sum │ └── main.go ├── middleware │ ├── go.mod │ └── main.go ├── mongodb-update │ ├── docker-compose.yml │ ├── go.mod │ ├── go.sum │ └── main.go ├── mongodb │ ├── docker-compose.yml │ ├── go.mod │ ├── go.sum │ └── main.go ├── nats │ ├── receiver │ │ ├── cmd │ │ │ └── main.go │ │ ├── go.mod │ │ └── go.sum │ └── sender │ │ ├── cmd │ │ └── main.go │ │ ├── go.mod │ │ └── go.sum ├── oracle-db-connection-prepared-statement │ ├── .devcontainer │ │ ├── Dockerfile │ │ └── devcontainer.json │ ├── go.mod │ ├── go.sum │ └── main.go ├── oracle-db-connection-write-tx │ ├── .devcontainer │ │ ├── Dockerfile │ │ └── devcontainer.json │ ├── go.mod │ ├── go.sum │ └── main.go ├── oracle-db-connection-write │ ├── .devcontainer │ │ ├── Dockerfile │ │ └── devcontainer.json │ ├── go.mod │ ├── go.sum │ └── main.go ├── oracle-db-connection │ ├── .devcontainer │ │ ├── Dockerfile │ │ └── devcontainer.json │ ├── go.mod │ ├── go.sum │ └── main.go ├── prometheus-metrics │ ├── go.mod │ ├── go.sum │ └── main.go ├── prometheus │ ├── application │ │ ├── Dockerfile │ │ ├── go.mod │ │ ├── go.sum │ │ └── main.go │ ├── docker-compose.yml │ ├── grafana │ │ └── grafana.ini │ └── prometheus │ │ └── prometheus.yml ├── rfc7808 │ ├── client │ │ ├── go.mod │ │ ├── go.sum │ │ └── main.go │ └── server │ │ ├── go.mod │ │ ├── go.sum │ │ └── main.go └── simple-http │ ├── go.mod │ └── main.go ├── module-logrus ├── go.mod ├── go.sum └── main.go ├── quality ├── http-server-test.v1 │ ├── go.mod │ ├── main.go │ └── main_test.go ├── http-server-test.v2 │ ├── go.mod │ ├── main.go │ ├── main_test.go │ └── results.html ├── http-server-test.v3 │ ├── go.mod │ ├── main.go │ ├── main_test.go │ └── results.html ├── http-server-test │ ├── go.mod │ ├── main.go │ └── main_test.go └── unittest │ ├── cmd │ └── main.go │ ├── go.mod │ └── math │ ├── add.go │ └── add_test.go ├── somelib ├── LICENSE ├── README.md ├── go.mod ├── somelib.go └── v2 │ ├── go.mod │ └── somelib.go └── vscode-first ├── go.mod ├── go.sum └── main.go /best-practices/context-http-server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "time" 7 | ) 8 | 9 | func handleCall(res http.ResponseWriter, req *http.Request) { 10 | ctx := req.Context() 11 | select { 12 | case <-ctx.Done(): 13 | res.Write([]byte("end")) 14 | fmt.Println("clsoing connection to client") 15 | return 16 | case <-time.After(20 * time.Second): 17 | fmt.Fprintln(res, "Hello World") 18 | } 19 | } 20 | 21 | func main() { 22 | http.HandleFunc("/", handleCall) 23 | http.ListenAndServe(":8080", nil) 24 | } 25 | -------------------------------------------------------------------------------- /best-practices/context-loop/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "time" 7 | ) 8 | 9 | func loop(ctx context.Context) <-chan string { 10 | c := make(chan string) 11 | go func() { 12 | for { 13 | select { 14 | case <-ctx.Done(): 15 | close(c) 16 | return 17 | default: 18 | c <- "Hello World!" 19 | } 20 | time.Sleep(1 * time.Second) 21 | } 22 | }() 23 | return c 24 | } 25 | 26 | func main() { 27 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 28 | defer cancel() 29 | for s := range loop(ctx) { 30 | log.Println(s) 31 | } 32 | 33 | log.Println("nächste Variante") 34 | 35 | ctx, cancel = context.WithCancel(context.Background()) 36 | time.AfterFunc(5*time.Second, cancel) 37 | for s := range loop(ctx) { 38 | log.Println(s) 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /best-practices/context-value-nono/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | ) 7 | 8 | type contextKey int 9 | 10 | const usernameKey contextKey = iota 11 | 12 | func businessCall(username string) { 13 | //.. was wichtiges 14 | fmt.Println(username) 15 | } 16 | 17 | func main() { 18 | ctx := context.Background() 19 | vCtx := context.WithValue(ctx, usernameKey, "bob") 20 | //.. einiges anderes 21 | businessCall(vCtx.Value(usernameKey).(string)) 22 | 23 | //Variante2 24 | username := "bob" 25 | //.. einiges anderes 26 | businessCall(username) 27 | 28 | } 29 | -------------------------------------------------------------------------------- /best-practices/context-value/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | ) 7 | 8 | type contextKey string 9 | 10 | func main() { 11 | var key contextKey = "test" 12 | ctx := context.Background() 13 | ctx = context.WithValue(ctx, key, "world") 14 | fmt.Println(ctx.Value(key)) 15 | } 16 | -------------------------------------------------------------------------------- /best-practices/context/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "time" 7 | ) 8 | 9 | func main() { 10 | 11 | bg := context.Background() 12 | fmt.Println(bg.Deadline()) //0001-01-01 00:00:00 +0000 UTC false 13 | fmt.Println(bg.Err()) // 14 | //fmt.Println(<-td.Done()) //blockiert für immer! 15 | 16 | ctx, cancel := context.WithTimeout(bg, 5*time.Second) 17 | defer cancel() 18 | 19 | go func() { 20 | time.Sleep(500 * time.Millisecond) 21 | cancel() 22 | }() 23 | 24 | fmt.Println(ctx.Deadline()) 25 | <-ctx.Done() 26 | fmt.Println("Ende") 27 | 28 | } 29 | -------------------------------------------------------------------------------- /best-practices/empty-string-check/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | s := "" 8 | 9 | if len(s) == 0 { 10 | fmt.Println("string ist leer") 11 | } 12 | 13 | if s == "" { 14 | fmt.Println("string ist leer") 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /best-practices/error-behaviour/external_lib.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | ) 6 | 7 | func callRemoteService() error { 8 | 9 | var err net.UnknownNetworkError = "blub" 10 | return err 11 | 12 | } 13 | -------------------------------------------------------------------------------- /best-practices/error-behaviour/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net" 6 | ) 7 | 8 | func isTimeout(err error) bool { 9 | 10 | type timeout interface { 11 | Timeout() bool 12 | } 13 | 14 | v, ok := err.(timeout) 15 | if !ok { 16 | return false 17 | } 18 | 19 | return v.Timeout() 20 | 21 | } 22 | 23 | func main() { 24 | err := callRemoteService() 25 | 26 | if nerr, ok := err.(net.Error); ok && nerr.Timeout() { 27 | //nochmal versuchen 28 | log.Printf("it's a unknown network error: %v\n", nerr) 29 | } 30 | 31 | if err != nil && !isTimeout(err) { 32 | log.Fatal(err) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /best-practices/error-handling-2/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | ) 8 | 9 | var err error 10 | var f *os.File 11 | 12 | func write(text string) { 13 | if err != nil { 14 | return 15 | } 16 | _, err = fmt.Fprintln(f, text) 17 | } 18 | 19 | func main() { 20 | f, err = os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm) 21 | if err != nil { 22 | log.Fatal(err) 23 | } 24 | defer f.Close() 25 | write("Hello world 2") 26 | write("Hello world 2") 27 | if err != nil { 28 | log.Fatal(err) 29 | } 30 | fmt.Println(f) 31 | } 32 | -------------------------------------------------------------------------------- /best-practices/error-handling-2/test.txt: -------------------------------------------------------------------------------- 1 | Hello world 2 2 | Hello world 2 3 | -------------------------------------------------------------------------------- /best-practices/error-handling-3/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "log" 7 | "os" 8 | ) 9 | 10 | func NewWriter(w io.Writer) *writer { 11 | return &writer{w, nil} 12 | } 13 | 14 | type writer struct { 15 | w io.Writer 16 | err error 17 | } 18 | 19 | func (w *writer) write(text string) { 20 | if w.err != nil { 21 | return 22 | } 23 | _, w.err = fmt.Fprintln(w.w, text) 24 | } 25 | 26 | func (w *writer) Err() error { 27 | return w.err 28 | } 29 | 30 | func main() { 31 | 32 | f, err := os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm) 33 | if err != nil { 34 | log.Fatal(err) 35 | } 36 | defer f.Close() 37 | 38 | w := NewWriter(f) 39 | 40 | w.write("Hello world 3") 41 | w.write("Hello world 3") 42 | 43 | if err = w.Err(); err != nil { 44 | log.Fatal(err) 45 | } 46 | fmt.Println(f) 47 | 48 | } 49 | -------------------------------------------------------------------------------- /best-practices/error-handling-3/test.txt: -------------------------------------------------------------------------------- 1 | Hello world 3 2 | Hello world 3 3 | -------------------------------------------------------------------------------- /best-practices/error-handling/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | f, err := os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm) 11 | if err != nil { 12 | log.Fatal(err) 13 | } 14 | defer f.Close() 15 | _, err = fmt.Fprintln(f, "Hello World") 16 | if err != nil { 17 | log.Fatal(err) 18 | } 19 | _, err = fmt.Fprintln(f, "Hello World") 20 | if err != nil { 21 | log.Fatal(err) 22 | } 23 | fmt.Println(f) 24 | } 25 | -------------------------------------------------------------------------------- /best-practices/error-handling/test.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | Hello World 3 | Hello World 4 | Hello World 5 | -------------------------------------------------------------------------------- /best-practices/file-io-large/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "log" 7 | "os" 8 | ) 9 | 10 | func main() { 11 | 12 | f, err := os.Open("main.go") 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | defer f.Close() 17 | 18 | scanner := bufio.NewScanner(f) 19 | //scanner.Split(bufio.ScanWords) 20 | for scanner.Scan() { 21 | fmt.Println(scanner.Text()) 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /best-practices/file-io/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | ) 8 | 9 | func main() { 10 | 11 | bites, err := ioutil.ReadFile("test.txt") 12 | if err != nil { 13 | log.Fatal(err) 14 | } 15 | 16 | fmt.Printf("%s\n", bites) 17 | fmt.Println(string(bites)) 18 | 19 | toWrite := []byte("Hello World!") 20 | err = ioutil.WriteFile("test2.txt", toWrite, 0644) 21 | if err != nil { 22 | log.Fatal(err) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /best-practices/file-io/test.txt: -------------------------------------------------------------------------------- 1 | Hello World! -------------------------------------------------------------------------------- /best-practices/force-interface/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type UserService interface { 4 | Save() 5 | } 6 | 7 | var _ UserService = &MyUserService{} 8 | 9 | type MyUserService struct { 10 | } 11 | 12 | func (us *MyUserService) Save() { 13 | 14 | } 15 | 16 | func main() { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /best-practices/goroutine-pooling/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | "time" 7 | ) 8 | 9 | var ( 10 | concurrent = 5 11 | pool = make(chan interface{}, concurrent) 12 | ) 13 | 14 | func work(num int, w *sync.WaitGroup) { 15 | pool <- "starting" 16 | defer func() { 17 | w.Done() 18 | <-pool 19 | }() 20 | fmt.Printf("number %d\n", num) 21 | time.Sleep(1 * time.Second) 22 | } 23 | 24 | func main() { 25 | 26 | var wg sync.WaitGroup 27 | for i := 0; i < 1000; i++ { 28 | wg.Add(1) 29 | go work(i, &wg) 30 | } 31 | wg.Wait() 32 | 33 | } 34 | -------------------------------------------------------------------------------- /best-practices/logging-defer/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "log" 4 | 5 | func importantStuff() { 6 | log.Println("rein") 7 | defer log.Println("raus") 8 | 9 | log.Println("drin") 10 | 11 | } 12 | 13 | func main() { 14 | importantStuff() 15 | } 16 | -------------------------------------------------------------------------------- /best-practices/map-contains/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | m := make(map[string]string) 8 | m["key1"] = "value1" 9 | 10 | if v, ok := m["key2"]; ok { 11 | fmt.Printf("Wert %v enthalten\n", v) 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /best-practices/project-structure/cmd-sample/cmd/myapp/myapp.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("myapp") 7 | } 8 | -------------------------------------------------------------------------------- /best-practices/project-structure/cmd-sample/cmd/mycli/mycli.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("mycli") 7 | } 8 | -------------------------------------------------------------------------------- /best-practices/project-structure/cmd-sample/handler.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/cmd-sample/handler.go -------------------------------------------------------------------------------- /best-practices/project-structure/cmd-sample/myapp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/cmd-sample/myapp -------------------------------------------------------------------------------- /best-practices/project-structure/cmd-sample/registration.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/cmd-sample/registration.go -------------------------------------------------------------------------------- /best-practices/project-structure/cmd-sample/service.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/cmd-sample/service.go -------------------------------------------------------------------------------- /best-practices/project-structure/dependency-sample/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | 6 | "golang.source-fellows.com/samples/applicationx/http/rest" 7 | "golang.source-fellows.com/samples/applicationx/postgres" 8 | ) 9 | 10 | func main() { 11 | 12 | us := &postgres.UserService{} 13 | 14 | http.HandleFunc("/", rest.Handler(us)) 15 | http.ListenAndServe(":8080", nil) 16 | 17 | } 18 | -------------------------------------------------------------------------------- /best-practices/project-structure/dependency-sample/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/applicationx 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/golang/mock v1.4.4 7 | rsc.io/quote/v3 v3.1.0 // indirect 8 | ) 9 | -------------------------------------------------------------------------------- /best-practices/project-structure/dependency-sample/go.sum: -------------------------------------------------------------------------------- 1 | github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw= 2 | github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= 3 | github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= 4 | github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= 5 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 6 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 7 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 8 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 9 | golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 10 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 11 | golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg= 12 | golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 13 | rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= 14 | rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= 15 | -------------------------------------------------------------------------------- /best-practices/project-structure/dependency-sample/http/rest/handler.go: -------------------------------------------------------------------------------- 1 | package rest 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "time" 7 | 8 | "golang.source-fellows.com/samples/applicationx" 9 | ) 10 | 11 | func Handler(us applicationx.UserService) http.HandlerFunc { 12 | return func(w http.ResponseWriter, r *http.Request) { 13 | us.CreateUser(&applicationx.User{}) 14 | fmt.Fprintf(w, "Hello World! %s", time.Now()) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /best-practices/project-structure/dependency-sample/http/rest/handler_test.go: -------------------------------------------------------------------------------- 1 | package rest 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "testing" 7 | 8 | "github.com/golang/mock/gomock" 9 | "golang.source-fellows.com/samples/applicationx/mocks" 10 | ) 11 | 12 | func TestHandler(t *testing.T) { 13 | 14 | ctrl := gomock.NewController(t) 15 | defer ctrl.Finish() 16 | 17 | userService := mocks.NewMockUserService(ctrl) 18 | 19 | handlerFunc := Handler(userService) 20 | 21 | w := httptest.NewRecorder() 22 | r, _ := http.NewRequest("GET", "/egal", nil) 23 | 24 | //wird der Service überhaupt aufgerufen? 25 | userService.EXPECT().CreateUser(gomock.Any()).MinTimes(1) 26 | 27 | handlerFunc(w, r) 28 | 29 | } 30 | -------------------------------------------------------------------------------- /best-practices/project-structure/dependency-sample/postgres/user_service.go: -------------------------------------------------------------------------------- 1 | package postgres 2 | 3 | import ( 4 | "fmt" 5 | 6 | "golang.source-fellows.com/samples/applicationx" 7 | ) 8 | 9 | var _ applicationx.UserService = &UserService{} 10 | 11 | type UserService struct{} 12 | 13 | func (us *UserService) CreateUser(user *applicationx.User) error { 14 | fmt.Println("Create User in Postgres Service") 15 | return nil 16 | } 17 | 18 | func (us *UserService) DeleteUser(id int) error { 19 | return nil 20 | } 21 | 22 | func (us *UserService) ReadUser(id int) (*applicationx.User, error) { 23 | return nil, nil 24 | } 25 | -------------------------------------------------------------------------------- /best-practices/project-structure/dependency-sample/user.go: -------------------------------------------------------------------------------- 1 | //go:generate mockgen -source=user.go -package mocks -destination mocks/user_service.go 2 | 3 | package applicationx 4 | 5 | type User struct { 6 | ID int 7 | name string 8 | } 9 | 10 | type UserService interface { 11 | CreateUser(u *User) error 12 | ReadUser(id int) (*User, error) 13 | DeleteUser(id int) error 14 | } 15 | -------------------------------------------------------------------------------- /best-practices/project-structure/flat-sample/handler.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/flat-sample/handler.go -------------------------------------------------------------------------------- /best-practices/project-structure/flat-sample/main.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/flat-sample/main.go -------------------------------------------------------------------------------- /best-practices/project-structure/flat-sample/registration.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/flat-sample/registration.go -------------------------------------------------------------------------------- /best-practices/project-structure/flat-sample/service.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/flat-sample/service.go -------------------------------------------------------------------------------- /best-practices/project-structure/internal-sample/a/b/c/g/app.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/internal-sample/a/b/c/g/app.go -------------------------------------------------------------------------------- /best-practices/project-structure/internal-sample/a/b/c/internal/d/e/f/accessor.go: -------------------------------------------------------------------------------- 1 | package f 2 | 3 | import "fmt" 4 | 5 | func Access() { 6 | fmt.Println("Access it!") 7 | } 8 | -------------------------------------------------------------------------------- /best-practices/project-structure/internal-sample/a/b/c/runner.go: -------------------------------------------------------------------------------- 1 | package c 2 | 3 | import "golang.source-fellows.com/project/isample/a/b/c/internal/d/e/f" 4 | 5 | func Run() { 6 | f.Access() 7 | } 8 | -------------------------------------------------------------------------------- /best-practices/project-structure/internal-sample/a/b/g/helper.go: -------------------------------------------------------------------------------- 1 | package g 2 | 3 | import "fmt" 4 | 5 | func Help() { 6 | fmt.Println("Help me! cannot access") 7 | } 8 | -------------------------------------------------------------------------------- /best-practices/project-structure/internal-sample/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/project/isample 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "layeredsample/models" 7 | ) 8 | 9 | func main() { 10 | c := models.CustomerModel{} 11 | fmt.Println(c) 12 | } 13 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/go.mod: -------------------------------------------------------------------------------- 1 | module layeredsample 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/go.sum: -------------------------------------------------------------------------------- 1 | golang.source-fellows.com v0.0.0-20200619133407-79aa8981720c h1:c0BKYhs4PULAm3Hb5f+t9AcmPRScdSAkeuajndyOMLQ= 2 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/handlers/customer.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | type CustomerHandler struct{} 4 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/handlers/registration.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | type RegistrationHandler struct{} 4 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/models/customer.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import "layeredsample/storage" 4 | 5 | type CustomerModel struct { 6 | db storage.DB 7 | } 8 | 9 | func (c *CustomerModel) Save() { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/models/registration.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | type RegistrationModel struct{} 4 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/services/customer.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/layer-sample/services/customer.go -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/services/registration.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/layer-sample/services/registration.go -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/storage/db.go: -------------------------------------------------------------------------------- 1 | package storage 2 | 3 | import "layeredsample/models" 4 | 5 | type DBStorage struct{} 6 | 7 | func (d *DBStorage) Save(c models.CustomerModel) { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /best-practices/project-structure/layer-sample/storage/xml.go: -------------------------------------------------------------------------------- 1 | package storage 2 | 3 | type XMLStorage struct{} 4 | -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/cmd/main.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/cmd/main.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/registration/handler.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/registration/handler.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/registration/registration.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/registration/registration.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/registration/service.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/registration/service.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/storage/db.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/storage/db.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/storage/xml.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/storage/xml.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/user/handler.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/user/handler.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/user/service.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/user/service.go -------------------------------------------------------------------------------- /best-practices/project-structure/module-sample/user/user.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/best-practices/project-structure/module-sample/user/user.go -------------------------------------------------------------------------------- /best-practices/project-structure/packagename/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | 6 | "golang.source-fellows.com/samples/packagename/util" 7 | ) 8 | 9 | func main() { 10 | 11 | util.ParseValue("Hello World") 12 | json.Marshal("Hello World") 13 | 14 | } 15 | -------------------------------------------------------------------------------- /best-practices/project-structure/packagename/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/packagename 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /best-practices/project-structure/packagename/util/log.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import "fmt" 4 | 5 | func ParseValue(text string) { 6 | fmt.Println(text) 7 | } 8 | -------------------------------------------------------------------------------- /best-practices/single-method-interface/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type Logger interface { 8 | Log(message string) 9 | } 10 | 11 | type LoggerFunc func(message string) 12 | 13 | func (l LoggerFunc) Log(message string) { 14 | l(message) 15 | } 16 | 17 | func MyLogFunc(message string) { 18 | fmt.Printf("I log %s\n", message) 19 | } 20 | 21 | func myMethodTakesTheLog(l Logger) { 22 | l.Log("my log message") 23 | } 24 | 25 | func main() { 26 | logger := LoggerFunc(MyLogFunc) 27 | myMethodTakesTheLog(logger) 28 | } 29 | -------------------------------------------------------------------------------- /best-practices/struct-to-text/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Tester struct { 6 | Name string 7 | Alter int 8 | Sonstwas string 9 | } 10 | 11 | type StringTester struct { 12 | Name string 13 | Alter int 14 | Sonstwas string 15 | } 16 | 17 | func (st *StringTester) String() string { 18 | return fmt.Sprintf("StringTester %v, Alter: %v", st.Name, st.Alter) 19 | } 20 | 21 | func main() { 22 | 23 | t := &Tester{Name: "Dingo", Alter: 3, Sonstwas: "Wert"} 24 | fmt.Printf("%+v\n", t) 25 | 26 | st := &StringTester{Name: "Dingo", Alter: 3, Sonstwas: "Wert"} 27 | fmt.Printf("%+v\n", st) 28 | 29 | } 30 | -------------------------------------------------------------------------------- /best-practices/synchron-api/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type Reader struct { 4 | c chan string 5 | } 6 | 7 | 8 | 9 | func main() { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Elastic Beanstalk Files 3 | .elasticbeanstalk/* 4 | !.elasticbeanstalk/*.cfg.yml 5 | !.elasticbeanstalk/*.global.yml 6 | -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/Buildfile: -------------------------------------------------------------------------------- 1 | make: ./build.sh -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu 2 | 3 | RUN apt-get update && apt-get install \ 4 | build-essential zlib1g-dev libssl-dev libncurses-dev \ 5 | libffi-dev libsqlite3-dev libreadline-dev libbz2-dev git curl wget -y 6 | 7 | WORKDIR /opt/ 8 | RUN git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git 9 | 10 | RUN ./aws-elastic-beanstalk-cli-setup/scripts/bundled_installer 11 | 12 | ENV PATH="/root/.ebcli-virtual-env/executables:/root/.pyenv/versions/3.7.2/bin:$PATH" -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/Procfile: -------------------------------------------------------------------------------- 1 | web: application -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/README.txt: -------------------------------------------------------------------------------- 1 | Nutzer der EB-Cli über Docker-Container: 2 | 3 | Image erstellen und ausführen: 4 | ``` 5 | docker build -t elasticbeanstalk-cli . 6 | docker run -v `pwd`:/app -it elasticbeanstalk-cli /bin/bash 7 | ``` 8 | 9 | Innerhalb des Containers müssen folgende Kommandos ausgeführt werden: 10 | ``` 11 | eb init -p go go-beanstalk-sample --region eu-central-1 12 | eb create go-env 13 | ``` -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Build app 4 | go build -o application -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/cloud/beanstalk 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /cloud-provider/aws-beanstak-sample/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "time" 7 | ) 8 | 9 | func greet(w http.ResponseWriter, r *http.Request) { 10 | fmt.Fprintf(w, "Hello World! %s", time.Now()) 11 | } 12 | 13 | func main() { 14 | http.HandleFunc("/", greet) 15 | http.ListenAndServe(":5000", nil) 16 | } 17 | -------------------------------------------------------------------------------- /cloud-provider/aws-lambda-go/README.txt: -------------------------------------------------------------------------------- 1 | ``` 2 | GOOS=linux go build main.go 3 | zip function.zip main 4 | aws lambda create-function \ 5 | --function-name aws-lambda-sample \ 6 | --runtime go1.x \ 7 | --zip-file fileb://function.zip 8 | --handler main 9 | --role arn:aws:iam:::role/ 10 | ``` -------------------------------------------------------------------------------- /cloud-provider/aws-lambda-go/function.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/cloud-provider/aws-lambda-go/function.zip -------------------------------------------------------------------------------- /cloud-provider/aws-lambda-go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | "github.com/aws/aws-lambda-go/lambda" 8 | "github.com/aws/aws-lambda-go/lambdacontext" 9 | ) 10 | 11 | type MyEvent struct { 12 | Name string `json:"name"` 13 | } 14 | 15 | func HandleRequest(ctx context.Context, name MyEvent) (string, error) { 16 | 17 | lc, _ := lambdacontext.FromContext(ctx) 18 | 19 | return fmt.Sprintf("Hello %s! %s", name.Name, lc.AwsRequestID), nil 20 | } 21 | 22 | func main() { 23 | lambda.Start(HandleRequest) 24 | } 25 | -------------------------------------------------------------------------------- /cloud-provider/azure-kubernetes/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1 as builder 2 | 3 | WORKDIR /go/src/app 4 | COPY *.go . 5 | RUN CGO_ENABLED=0 go build . 6 | 7 | #################################### 8 | 9 | FROM alpine 10 | COPY --from=builder /go/src/app/app . 11 | EXPOSE 8080 12 | CMD [ "./app" ] 13 | -------------------------------------------------------------------------------- /cloud-provider/azure-kubernetes/README.txt: -------------------------------------------------------------------------------- 1 | 2 | ``` 3 | eval $(minikube docker-env) 4 | docker build -t minikube-golang-sample . 5 | 6 | kubectl create -f manifest.yml 7 | kubectl get pod 8 | kubectl expose deployment golang-sample --type=NodePort 9 | kubectl get service 10 | minikube service golang-sample --url 11 | ``` 12 | -------------------------------------------------------------------------------- /cloud-provider/azure-kubernetes/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "time" 7 | ) 8 | 9 | func greet(w http.ResponseWriter, r *http.Request) { 10 | fmt.Fprintf(w, "Hello World! %s", time.Now()) 11 | } 12 | 13 | func main() { 14 | http.HandleFunc("/", greet) 15 | http.ListenAndServe(":8080", nil) 16 | } 17 | -------------------------------------------------------------------------------- /cloud-provider/azure-kubernetes/manifest.yml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | creationTimestamp: null 5 | labels: 6 | app: go-azure-sample 7 | name: go-azure-sample 8 | spec: 9 | replicas: 2 10 | selector: 11 | matchLabels: 12 | app: go-azure-sample 13 | strategy: {} 14 | template: 15 | metadata: 16 | creationTimestamp: null 17 | labels: 18 | app: go-azure-sample 19 | spec: 20 | containers: 21 | - image: trainingfellow.azurecr.io/go-azure-sample 22 | name: go-azure-sample 23 | resources: {} 24 | ports: 25 | - containerPort: 8080 26 | status: {} -------------------------------------------------------------------------------- /cloud-provider/docker-compose/Dockerfile.receiver: -------------------------------------------------------------------------------- 1 | FROM golang:1 as builder 2 | 3 | WORKDIR /go/src/app 4 | 5 | COPY go.* ./ 6 | RUN go mod download 7 | 8 | COPY . . 9 | RUN CGO_ENABLED=0 go build -o app ./cmd/receiver 10 | 11 | FROM alpine:latest 12 | COPY --from=builder /go/src/app . 13 | CMD ["./app"] 14 | -------------------------------------------------------------------------------- /cloud-provider/docker-compose/Dockerfile.sender: -------------------------------------------------------------------------------- 1 | FROM golang:1 as builder 2 | 3 | WORKDIR /go/src/app 4 | 5 | COPY go.* ./ 6 | RUN go mod download 7 | 8 | COPY . . 9 | RUN CGO_ENABLED=0 go build -o app ./cmd/sender 10 | 11 | FROM alpine:latest 12 | COPY --from=builder /go/src/app . 13 | CMD ["./app"] 14 | -------------------------------------------------------------------------------- /cloud-provider/docker-compose/cmd/receiver/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "os" 7 | "sync" 8 | "time" 9 | 10 | "github.com/nats-io/nats.go" 11 | ) 12 | 13 | type Message struct { 14 | Text string 15 | } 16 | 17 | func connectNATS(ctx context.Context, url string) (*nats.Conn, error) { 18 | var err error 19 | for { 20 | select { 21 | case <-ctx.Done(): 22 | return nil, err 23 | default: 24 | var nc *nats.Conn 25 | nc, err = nats.Connect(url) 26 | if err == nil { 27 | return nc, err 28 | } 29 | } 30 | } 31 | 32 | } 33 | 34 | func main() { 35 | 36 | url := nats.DefaultURL 37 | if v, ok := os.LookupEnv("NATS_URL"); ok { 38 | url = v 39 | } 40 | 41 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 42 | defer cancel() 43 | nc, err := connectNATS(ctx, url) 44 | if err != nil { 45 | log.Fatalf("Could not connect to server because of %v", err) 46 | } 47 | defer nc.Close() 48 | log.Println("successfully connected to NATS server") 49 | 50 | c, _ := nats.NewEncodedConn(nc, nats.JSON_ENCODER) 51 | defer c.Close() 52 | 53 | wg := sync.WaitGroup{} 54 | wg.Add(1) 55 | 56 | sub, err := c.Subscribe("testing", func(message *Message) { 57 | log.Printf("neue Nachricht %+v", message) 58 | }) 59 | 60 | if err != nil { 61 | log.Fatalf("Could not subscribe because of %v", err) 62 | } 63 | 64 | log.Printf("Warte auf Nachrichten") 65 | wg.Wait() 66 | 67 | sub.Unsubscribe() 68 | sub.Drain() 69 | 70 | } 71 | -------------------------------------------------------------------------------- /cloud-provider/docker-compose/cmd/sender/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | "time" 7 | 8 | "github.com/nats-io/nats.go" 9 | ) 10 | 11 | type Message struct { 12 | Text string 13 | } 14 | 15 | func sendMessage(url string) error { 16 | nc, err := nats.Connect(url) 17 | if err != nil { 18 | return err 19 | } 20 | defer nc.Close() 21 | c, _ := nats.NewEncodedConn(nc, nats.JSON_ENCODER) 22 | defer c.Close() 23 | 24 | return c.Publish("testing", &Message{"Hello World"}) 25 | } 26 | 27 | func main() { 28 | 29 | url := nats.DefaultURL 30 | if v, ok := os.LookupEnv("NATS_URL"); ok { 31 | url = v 32 | } 33 | 34 | for { 35 | err := sendMessage(url) 36 | if err != nil { 37 | log.Println(err) 38 | } else { 39 | log.Println("Nachricht versendet") 40 | } 41 | time.Sleep(4 * time.Second) 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /cloud-provider/docker-compose/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | sender-application: 4 | build: 5 | context: . 6 | dockerfile: Dockerfile.sender 7 | environment: 8 | NATS_URL: "nats://nats-server:4222" 9 | receiver-application: 10 | build: 11 | context: . 12 | dockerfile: Dockerfile.receiver 13 | environment: 14 | NATS_URL: "nats://nats-server:4222" 15 | nats-server: 16 | image: nats -------------------------------------------------------------------------------- /cloud-provider/docker-compose/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/cloud/dockercompose 2 | 3 | go 1.14 4 | 5 | require github.com/nats-io/nats.go v1.10.0 // indirect 6 | -------------------------------------------------------------------------------- /cloud-provider/docker-compose/go.sum: -------------------------------------------------------------------------------- 1 | github.com/nats-io/jwt v0.3.2 h1:+RB5hMpXUUA2dfxuhBTEkMOrYmM+gKIZYS1KjSostMI= 2 | github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU= 3 | github.com/nats-io/nats.go v1.10.0 h1:L8qnKaofSfNFbXg0C5F71LdjPRnmQwSsA4ukmkt1TvY= 4 | github.com/nats-io/nats.go v1.10.0/go.mod h1:AjGArbfyR50+afOUotNX2Xs5SYHf+CoOa5HH1eEl2HE= 5 | github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= 6 | github.com/nats-io/nkeys v0.1.4 h1:aEsHIssIk6ETN5m2/MD8Y4B2X7FfXrBAUdkyRvbVYzA= 7 | github.com/nats-io/nkeys v0.1.4/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s= 8 | github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= 9 | github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= 10 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 11 | golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 12 | golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM= 13 | golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 14 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 15 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 16 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 17 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 18 | -------------------------------------------------------------------------------- /cloud-provider/docker-sample-multistage/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1 as builder 2 | 3 | WORKDIR /go/src/app 4 | COPY *.go . 5 | RUN CGO_ENABLED=0 go build . 6 | 7 | #################################### 8 | 9 | FROM alpine 10 | COPY --from=builder /go/src/app/app . 11 | CMD [ "./app" ] 12 | -------------------------------------------------------------------------------- /cloud-provider/docker-sample-multistage/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Hello World!") 9 | } 10 | -------------------------------------------------------------------------------- /cloud-provider/docker-sample/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang 2 | 3 | WORKDIR /go/src/app 4 | COPY *.go . 5 | RUN go install . 6 | 7 | CMD ["app"] -------------------------------------------------------------------------------- /cloud-provider/docker-sample/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Hello World!") 9 | } 10 | -------------------------------------------------------------------------------- /cloud-provider/go-cloud/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1 as builder 2 | 3 | WORKDIR /go/src/app 4 | COPY go.* ./ 5 | RUN go mod download 6 | COPY *.go ./ 7 | RUN CGO_ENABLED=0 go build . 8 | 9 | #################################### 10 | 11 | FROM alpine 12 | COPY --from=builder /go/src/app/gcloud . 13 | EXPOSE 8080 14 | CMD [ "./gcloud" ] 15 | -------------------------------------------------------------------------------- /cloud-provider/go-cloud/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | application: 4 | build: . 5 | ports: 6 | - 8080:8080 7 | minio-server: 8 | image: minio/minio 9 | command: server /data 10 | volumes: 11 | - ./minio:/data 12 | ports: 13 | - 9000:9000 -------------------------------------------------------------------------------- /cloud-provider/go-cloud/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/gobuch/gcloud 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/gin-gonic/gin v1.6.3 // indirect 7 | github.com/go-playground/validator/v10 v10.3.0 // indirect 8 | github.com/json-iterator/go v1.1.10 // indirect 9 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 10 | github.com/modern-go/reflect2 v1.0.1 // indirect 11 | gocloud.dev v0.20.0 // indirect 12 | golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 // indirect 13 | google.golang.org/protobuf v1.25.0 // indirect 14 | gopkg.in/yaml.v2 v2.3.0 // indirect 15 | ) 16 | -------------------------------------------------------------------------------- /cloud-provider/go-cloud/manifest.yml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | creationTimestamp: null 5 | labels: 6 | app: gcloud-sample 7 | name: gcloud-sample 8 | spec: 9 | replicas: 2 10 | selector: 11 | matchLabels: 12 | app: gcloud-sample 13 | strategy: {} 14 | template: 15 | metadata: 16 | creationTimestamp: null 17 | labels: 18 | app: gcloud-sample 19 | spec: 20 | containers: 21 | - image: minikube-gcloud-sample 22 | name: gcloud-sample-service 23 | imagePullPolicy: Never 24 | resources: {} 25 | ports: 26 | - containerPort: 8080 27 | - image: minio/minio 28 | name: minio 29 | args: ["server", "/data"] 30 | volumeMounts: 31 | - name: minio-storage 32 | mountPath: /data 33 | volumes: 34 | - name: minio-storage 35 | emptyDir: {} 36 | status: {} -------------------------------------------------------------------------------- /cloud-provider/go-cloud/minio/gocloud/gopher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/cloud-provider/go-cloud/minio/gocloud/gopher.png -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | # delve output 8 | debug 9 | 10 | # Test binary, built with `go test -c` 11 | *.test 12 | 13 | # Output of the go coverage tool, specifically when used with LiteIDE 14 | *.out 15 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.idea/externalDependencies.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.idea/go-hello-world.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "googlecloudtools.cloudcode", 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Run on Kubernetes", 6 | "type": "cloudcode.kubernetes", 7 | "request": "launch", 8 | "skaffoldConfig": "${workspaceFolder}/skaffold.yaml", 9 | "watch": true, 10 | "cleanUp": true, 11 | "portForward": true, 12 | "imageRegistry": "gcr.io/gobuch-72abe" 13 | }, 14 | { 15 | "type": "go", 16 | "request": "launch", 17 | "name": "Launch (local)", 18 | "mode": "auto", 19 | "program": "${workspaceFolder}/cmd/hello-world" 20 | } 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "presentation": { 6 | "panel": "shared", 7 | "showReuseMessage": true, 8 | }, 9 | "tasks": [ 10 | { 11 | "group": "build", 12 | "label": "Build container image using local Docker", 13 | "type": "process", 14 | "command": "skaffold", 15 | "args": [ 16 | "build", 17 | "-p=no-push" 18 | ], 19 | "problemMatcher": [], 20 | }, 21 | { 22 | "label": "Deploy to Kubernetes cluster", 23 | "type": "process", 24 | "command": "skaffold", 25 | "args": [ 26 | "run" 27 | ], 28 | "problemMatcher": [], 29 | }, 30 | { 31 | "label": "View application logs on Kubernetes", 32 | "type": "process", 33 | "command": "kubectl", 34 | "args": [ 35 | "logs", 36 | "--selector", 37 | "app=hello-world" 38 | ], 39 | "problemMatcher": [], 40 | } 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/Dockerfile: -------------------------------------------------------------------------------- 1 | # Use base golang image from Docker Hub 2 | FROM golang:1.14 3 | 4 | # Download the dlv (delve) debugger for go (you can comment this out if unused) 5 | RUN go get -u -v github.com/go-delve/delve/cmd/dlv 6 | 7 | WORKDIR /src/hello-world 8 | 9 | # Install dependencies in go.mod and go.sum 10 | COPY go.mod go.sum ./ 11 | RUN go mod download 12 | 13 | # Copy rest of the application source code 14 | COPY . ./ 15 | 16 | # Compile the application to /app. 17 | RUN go build -o /app -v ./cmd/hello-world 18 | 19 | # If you want to use the debugger, you need to modify the entrypoint to the 20 | # container and point it to the "dlv debug" command: 21 | # * UNCOMMENT the following ENTRYPOINT statement, 22 | # * COMMENT OUT the last ENTRYPOINT statement 23 | # Start the "dlv debug" server on port 3000 of the container. 24 | ENTRYPOINT ["dlv", "exec", "/app", "--continue", "--accept-multiclient", "--api-version=2", "--headless", "--listen=:3000", "--log"] 25 | 26 | # If you want to run WITHOUT the debugging server: 27 | # * COMMENT OUT the previous ENTRYPOINT statements, 28 | # * UNCOMMENT the following ENTRYPOINT statement. 29 | # ENTRYPOINT ["/app"] 30 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/cmd/hello-world/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | "os" 8 | ) 9 | 10 | const defaultAddr = ":8080" 11 | 12 | // main starts an http server on the $PORT environment variable. 13 | func main() { 14 | addr := defaultAddr 15 | // $PORT environment variable is provided in the Kubernetes deployment. 16 | if p := os.Getenv("PORT"); p != "" { 17 | addr = ":" + p 18 | } 19 | 20 | log.Printf("server starting to listen on %s", addr) 21 | http.HandleFunc("/", home) 22 | if err := http.ListenAndServe(addr, nil); err != nil { 23 | log.Fatalf("server listen error: %+v", err) 24 | } 25 | } 26 | 27 | // home logs the received request and returns a simple response. 28 | func home(w http.ResponseWriter, r *http.Request) { 29 | log.Printf("received request: %s %s", r.Method, r.URL.Path) 30 | fmt.Fprintf(w, "Hello, world!") 31 | } 32 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/go.mod: -------------------------------------------------------------------------------- 1 | module hello-world 2 | 3 | go 1.12 4 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/go.sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/cloud-provider/google-cloud-hello/go.sum -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/img/diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/cloud-provider/google-cloud-hello/img/diagram.png -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/kubernetes-manifests/hello.deployment.yaml: -------------------------------------------------------------------------------- 1 | # This Deployment manifest defines: 2 | # - single-replica deployment of the container image, with label "app: go-hello-world" 3 | # - Pod exposes port 8080 4 | # - specify PORT environment variable to the container process 5 | # Syntax reference https://kubernetes.io/docs/concepts/configuration/overview/ 6 | apiVersion: apps/v1 7 | kind: Deployment 8 | metadata: 9 | name: go-hello-world 10 | spec: 11 | replicas: 1 12 | selector: 13 | matchLabels: 14 | app: go-hello-world 15 | template: 16 | metadata: 17 | labels: 18 | app: go-hello-world 19 | spec: 20 | containers: 21 | - name: server 22 | image: go-hello-world 23 | ports: 24 | - containerPort: 8080 25 | env: 26 | - name: PORT 27 | value: "8080" 28 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/kubernetes-manifests/hello.service.yaml: -------------------------------------------------------------------------------- 1 | # This Service manifest defines: 2 | # - a load balancer for pods matching label "app: go-hello-world" 3 | # - exposing the application to the public Internet (type:LoadBalancer) 4 | # - routes port 80 of the load balancer to the port 8080 of the Pods. 5 | # Syntax reference https://kubernetes.io/docs/concepts/configuration/overview/ 6 | apiVersion: v1 7 | kind: Service 8 | metadata: 9 | name: go-hello-world-external 10 | spec: 11 | type: LoadBalancer 12 | selector: 13 | app: go-hello-world 14 | ports: 15 | - name: http 16 | port: 80 17 | targetPort: 8080 18 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-hello/skaffold.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: skaffold/v1beta15 2 | kind: Config 3 | build: 4 | tagPolicy: 5 | sha256: {} 6 | artifacts: 7 | - context: . 8 | image: go-hello-world 9 | deploy: 10 | kubectl: 11 | manifests: 12 | - kubernetes-manifests/** 13 | profiles: 14 | - name: cloudbuild 15 | build: 16 | googleCloudBuild: {} 17 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-sample/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Run/Debug on Kubernetes", 5 | "type": "cloudcode.kubernetes", 6 | "request": "launch", 7 | "skaffoldConfig": "${workspaceFolder}/skaffold.yaml", 8 | "watch": true, 9 | "cleanUp": true, 10 | "portForward": true, 11 | "imageRegistry": "gcr.io/gobuch-72abe/google-cloud-sample" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /cloud-provider/google-cloud-sample/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1 as builder 2 | 3 | WORKDIR /go/src/app 4 | COPY *.go . 5 | RUN CGO_ENABLED=0 go build . 6 | 7 | #################################### 8 | 9 | FROM alpine 10 | COPY --from=builder /go/src/app/app . 11 | EXPOSE 8080 12 | CMD [ "./app" ] 13 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-sample/README.txt: -------------------------------------------------------------------------------- 1 | 2 | ``` 3 | docker build -t minikube-golang-sample . 4 | 5 | kubectl create -f manifest.yml 6 | kubectl get pod 7 | kubectl expose deployment golang-sample --type=NodePort 8 | kubectl get service 9 | minikube service golang-sample --url 10 | ``` 11 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-sample/deployment-manifest.yml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | creationTimestamp: null 5 | labels: 6 | app: google-cloud-sample 7 | name: google-cloud-sample 8 | spec: 9 | replicas: 2 10 | selector: 11 | matchLabels: 12 | app: google-cloud-sample 13 | strategy: {} 14 | template: 15 | metadata: 16 | creationTimestamp: null 17 | labels: 18 | app: google-cloud-sample 19 | spec: 20 | containers: 21 | - image: google-cloud-sample 22 | name: google-cloud-sample 23 | resources: {} 24 | ports: 25 | - containerPort: 8080 26 | status: {} -------------------------------------------------------------------------------- /cloud-provider/google-cloud-sample/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "time" 7 | ) 8 | 9 | func greet(w http.ResponseWriter, r *http.Request) { 10 | fmt.Fprintf(w, "Hello World! %s", time.Now()) 11 | } 12 | 13 | func main() { 14 | http.HandleFunc("/", greet) 15 | http.ListenAndServe(":8080", nil) 16 | } 17 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-sample/service-manifest.yml: -------------------------------------------------------------------------------- 1 | # This Service manifest defines: 2 | # - a load balancer for pods matching label "app: go-hello-world" 3 | # - exposing the application to the public Internet (type:LoadBalancer) 4 | # - routes port 80 of the load balancer to the port 8080 of the Pods. 5 | # Syntax reference https://kubernetes.io/docs/concepts/configuration/overview/ 6 | apiVersion: v1 7 | kind: Service 8 | metadata: 9 | name: google-cloud-sample-external 10 | spec: 11 | type: LoadBalancer 12 | selector: 13 | app: google-cloud-sample 14 | ports: 15 | - name: http 16 | port: 80 17 | targetPort: 8080 18 | -------------------------------------------------------------------------------- /cloud-provider/google-cloud-sample/skaffold.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: skaffold/v2beta5 2 | kind: Config 3 | metadata: 4 | name: google-cloud-sample 5 | build: 6 | artifacts: 7 | - image: google-cloud-sample 8 | tagPolicy: 9 | sha256: {} 10 | deploy: 11 | kubectl: 12 | manifests: 13 | - deployment-manifest.yml 14 | - service-manifest.yml 15 | -------------------------------------------------------------------------------- /cloud-provider/minikube-sample/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1 as builder 2 | 3 | WORKDIR /go/src/app 4 | COPY *.go . 5 | RUN CGO_ENABLED=0 go build . 6 | 7 | #################################### 8 | 9 | FROM alpine 10 | COPY --from=builder /go/src/app/app . 11 | EXPOSE 8080 12 | CMD [ "./app" ] 13 | -------------------------------------------------------------------------------- /cloud-provider/minikube-sample/README.txt: -------------------------------------------------------------------------------- 1 | 2 | ``` 3 | eval $(minikube docker-env) 4 | docker build -t minikube-golang-sample . 5 | 6 | kubectl create -f manifest.yml 7 | kubectl get pod 8 | kubectl expose deployment golang-sample --type=NodePort 9 | kubectl get service 10 | minikube service golang-sample --url 11 | ``` 12 | -------------------------------------------------------------------------------- /cloud-provider/minikube-sample/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "time" 7 | ) 8 | 9 | func greet(w http.ResponseWriter, r *http.Request) { 10 | fmt.Fprintf(w, "Hello World! %s", time.Now()) 11 | } 12 | 13 | func main() { 14 | http.HandleFunc("/", greet) 15 | http.ListenAndServe(":8080", nil) 16 | } 17 | -------------------------------------------------------------------------------- /cloud-provider/minikube-sample/manifest.yml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | creationTimestamp: null 5 | labels: 6 | app: golang-sample 7 | name: golang-sample 8 | spec: 9 | replicas: 2 10 | selector: 11 | matchLabels: 12 | app: golang-sample 13 | strategy: {} 14 | template: 15 | metadata: 16 | creationTimestamp: null 17 | labels: 18 | app: golang-sample 19 | spec: 20 | containers: 21 | - image: minikube-golang-sample 22 | name: golang-sample 23 | imagePullPolicy: Never 24 | resources: {} 25 | ports: 26 | - containerPort: 8080 27 | status: {} -------------------------------------------------------------------------------- /concurrency/best-practices-generator-function/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func printOut() <-chan string { 10 | c := make(chan string) 11 | go func() { 12 | for i := 1; ; i++ { 13 | c <- fmt.Sprintf("Print %v", i) 14 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 15 | } 16 | }() 17 | return c 18 | } 19 | 20 | func main() { 21 | c := printOut() 22 | for i := 1; i < 10; i++ { 23 | fmt.Println(<-c) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /concurrency/best-practices-multiplexer-select/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func printOut() <-chan string { 10 | c := make(chan string) 11 | go func() { 12 | for i := 1; ; i++ { 13 | c <- fmt.Sprintf("Print %v", i) 14 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 15 | } 16 | }() 17 | return c 18 | } 19 | 20 | func join(in1, in2 <-chan string) <-chan string { 21 | out := make(chan string) 22 | go func() { 23 | for { 24 | select { 25 | case t := <-in1: 26 | out <- t 27 | case t := <-in2: 28 | out <- t 29 | } 30 | } 31 | }() 32 | return out 33 | } 34 | 35 | func main() { 36 | c1 := printOut() 37 | c2 := printOut() 38 | c3 := join(c1, c2) 39 | for i := 1; i < 10; i++ { 40 | fmt.Println(<-c3) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /concurrency/best-practices-multiplexer/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func printOut() <-chan string { 10 | c := make(chan string) 11 | go func() { 12 | for i := 1; ; i++ { 13 | c <- fmt.Sprintf("Print %v", i) 14 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 15 | } 16 | }() 17 | return c 18 | } 19 | 20 | func join(in1, in2 <-chan string) <-chan string { 21 | out := make(chan string) 22 | go func() { 23 | for { 24 | out <- <-in1 25 | } 26 | }() 27 | go func() { 28 | for { 29 | out <- <-in2 30 | } 31 | }() 32 | return out 33 | } 34 | 35 | func main() { 36 | c1 := printOut() 37 | c2 := printOut() 38 | c3 := join(c1, c2) 39 | for i := 1; i < 10; i++ { 40 | fmt.Println(<-c3) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /concurrency/best-practices-sync/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | type Message struct { 10 | text string 11 | wait chan interface{} 12 | } 13 | 14 | func printOut() <-chan Message { 15 | finish := make(chan interface{}) 16 | c := make(chan Message) 17 | go func() { 18 | for i := 1; ; i++ { 19 | c <- Message{fmt.Sprintf("Print %v", i), finish} 20 | time.Sleep(time.Duration(rand.Intn(9000)) * time.Millisecond) 21 | <-finish 22 | } 23 | }() 24 | return c 25 | } 26 | 27 | func join(in1, in2 <-chan Message) <-chan Message { 28 | out := make(chan Message) 29 | go func() { 30 | for { 31 | out <- <-in1 32 | } 33 | }() 34 | go func() { 35 | for { 36 | out <- <-in2 37 | } 38 | }() 39 | return out 40 | } 41 | 42 | func main() { 43 | c1 := printOut() 44 | c2 := printOut() 45 | c3 := join(c1, c2) 46 | for i := 1; i < 20; i++ { 47 | message1 := <-c3 48 | message2 := <-c3 49 | fmt.Println(message1.text) 50 | fmt.Println(message2.text) 51 | message1.wait <- "" 52 | message2.wait <- "" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /concurrency/best-practices-timeout/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func printOut() <-chan string { 10 | c := make(chan string) 11 | go func() { 12 | for i := 1; ; i++ { 13 | c <- fmt.Sprintf("Print %v", i) 14 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 15 | } 16 | }() 17 | return c 18 | } 19 | 20 | func join(in1, in2 <-chan string) <-chan string { 21 | out := make(chan string) 22 | go func() { 23 | for { 24 | select { 25 | case t := <-in1: 26 | out <- t 27 | case t := <-in2: 28 | out <- t 29 | } 30 | } 31 | }() 32 | return out 33 | } 34 | 35 | func main() { 36 | c1 := printOut() 37 | c2 := printOut() 38 | c3 := join(c1, c2) 39 | 40 | timeout := time.After(3 * time.Second) 41 | for { 42 | select { 43 | case t := <-c3: 44 | fmt.Println(t) 45 | case <-timeout: 46 | return 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /concurrency/channel-mit-close/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | var c chan string 10 | 11 | func printOut() { 12 | for i := 1; i < 10; i++ { 13 | c <- fmt.Sprintf("Print %v", i) 14 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 15 | } 16 | close(c) 17 | } 18 | 19 | func main() { 20 | c = make(chan string) 21 | go printOut() 22 | 23 | for { 24 | t, open := <-c 25 | if open { 26 | fmt.Println(t) 27 | } else { 28 | break 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /concurrency/channel-range/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | var c chan string 10 | 11 | func printOut() { 12 | for i := 1; i < 10; i++ { 13 | c <- fmt.Sprintf("Print %v", i) 14 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 15 | } 16 | close(c) 17 | } 18 | 19 | func main() { 20 | c = make(chan string) 21 | go printOut() 22 | for t := range c { 23 | fmt.Println(t) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /concurrency/channels/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | c := make(chan string) 8 | 9 | go func() { 10 | fmt.Println(<-c) 11 | }() 12 | 13 | c <- "Hello World!" 14 | 15 | close(c) 16 | 17 | } 18 | -------------------------------------------------------------------------------- /concurrency/first-go-routine-mit-channel/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | var c chan string 10 | 11 | func printOut() { 12 | for i := 1; ; i++ { 13 | c <- fmt.Sprintf("Print %v", i) 14 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 15 | } 16 | } 17 | 18 | func main() { 19 | c = make(chan string) 20 | go printOut() 21 | for i := 0; i < 10; i++ { 22 | fmt.Println(<-c) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /concurrency/first-go-routine/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func printOut() { 10 | for i := 0; i < 10; i++ { 11 | fmt.Printf("Print %v\n", i) 12 | time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) 13 | } 14 | } 15 | 16 | func main() { 17 | go printOut() 18 | time.Sleep(5 * time.Second) 19 | } 20 | -------------------------------------------------------------------------------- /concurrency/graceful-shutdown/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "go.formatTool": "goimports" 3 | } -------------------------------------------------------------------------------- /concurrency/graceful-shutdown/go.mod: -------------------------------------------------------------------------------- 1 | module graceful-shutdown 2 | 3 | go 1.12 4 | -------------------------------------------------------------------------------- /concurrency/graceful-shutdown/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "net/http" 8 | "os" 9 | "os/signal" 10 | "time" 11 | ) 12 | 13 | func handle(w http.ResponseWriter, r *http.Request) { 14 | 15 | select { 16 | case <-time.After(60 * time.Second): 17 | // If we receive a message after 2 seconds 18 | // that means the request has been processed 19 | // We then write this as the response 20 | w.Write([]byte("request processed")) 21 | case <-r.Context().Done(): 22 | // If the request gets cancelled, log it 23 | // to STDERR 24 | fmt.Fprint(os.Stderr, "request cancelled\n") 25 | } 26 | 27 | } 28 | 29 | func main() { 30 | 31 | srv := http.Server{Addr: ":8081"} 32 | 33 | idleConnsClosed := make(chan struct{}) 34 | 35 | ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second) 36 | defer cancel() 37 | 38 | go func() { 39 | sigint := make(chan os.Signal, 1) 40 | signal.Notify(sigint, os.Interrupt) 41 | <-sigint 42 | // We received an interrupt signal, shut down. 43 | if err := srv.Shutdown(ctx); err != nil { 44 | // Error from closing listeners, or context timeout: 45 | log.Printf("HTTP server Shutdown: %v", err) 46 | } 47 | log.Print("HTTP server Shutdown successful") 48 | close(idleConnsClosed) 49 | }() 50 | 51 | http.HandleFunc("/", handle) 52 | 53 | if err := srv.ListenAndServe(); err != http.ErrServerClosed { 54 | // Error starting or closing listener: 55 | log.Printf("HTTP server ListenAndServe: %v", err) 56 | } 57 | 58 | <-idleConnsClosed 59 | 60 | } 61 | -------------------------------------------------------------------------------- /concurrency/java-threads-beispiel/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.Semaphore; 2 | import java.util.concurrent.atomic.AtomicBoolean; 3 | 4 | public class Main { 5 | 6 | static int count = 0; 7 | 8 | public static void main(String[] args) { 9 | 10 | new Thread(new Runnable() { 11 | @Override 12 | public void run() { 13 | while (true) { 14 | count++; 15 | try { 16 | new Thread(new Runnable() { 17 | @Override 18 | public void run() { 19 | while (true) { 20 | try { 21 | Thread.sleep(120000); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | System.out.println("Thread Hello " + count); 26 | } 27 | } 28 | }).start(); 29 | } catch (Throwable e) { 30 | e.printStackTrace(); 31 | System.exit(1); 32 | } 33 | System.out.println("create native thread "+ count); 34 | } 35 | } 36 | }).start(); 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /configuration/application-configuration-os/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | configValue := os.Getenv("HELLO") 10 | fmt.Printf("Hello %v\n", configValue) 11 | } 12 | -------------------------------------------------------------------------------- /configuration/application-configuration-viper/config.yml: -------------------------------------------------------------------------------- 1 | server: 2 | host: localhost -------------------------------------------------------------------------------- /configuration/application-configuration-viper/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/applicationconfiguration 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/kelseyhightower/envconfig v1.4.0 // indirect 7 | github.com/spf13/viper v1.7.0 8 | ) 9 | -------------------------------------------------------------------------------- /configuration/application-configuration-viper/main.go: -------------------------------------------------------------------------------- 1 | /*Package main show how to use viper. 2 | 3 | Start the app in the root folder with: 4 | 5 | SERVER_HOST=testing go run cmd/main.go 6 | 7 | */ 8 | package main 9 | 10 | import ( 11 | "fmt" 12 | "strings" 13 | 14 | "github.com/spf13/viper" 15 | ) 16 | 17 | func main() { 18 | viper.SetConfigName("config") 19 | viper.AddConfigPath(".") 20 | err := viper.ReadInConfig() 21 | if err != nil { 22 | panic(fmt.Errorf("fatal error config file: %s", err)) 23 | } 24 | replacer := strings.NewReplacer(".", "_") 25 | viper.SetEnvKeyReplacer(replacer) 26 | viper.AutomaticEnv() 27 | fmt.Println(viper.Get("server.host")) 28 | } 29 | -------------------------------------------------------------------------------- /configuration/application-configuration/cmd/main.go: -------------------------------------------------------------------------------- 1 | /*Package main show how to use envconfig. 2 | 3 | Start the app in the root folder with: 4 | 5 | SERVER_PORT=8080 SERVER_HOST=localhost go run cmd/main.go 6 | 7 | */ 8 | package main 9 | 10 | import ( 11 | "fmt" 12 | 13 | "github.com/kelseyhightower/envconfig" 14 | "golang.source-fellows.com/samples/applicationconfiguration" 15 | ) 16 | 17 | func main() { 18 | 19 | cfg := applicationconfiguration.Config{} 20 | err := envconfig.Process("", &cfg) 21 | 22 | if err != nil { 23 | panic(err) 24 | } 25 | 26 | fmt.Println(cfg.Server.Port) 27 | 28 | } 29 | -------------------------------------------------------------------------------- /configuration/application-configuration/configuration.go: -------------------------------------------------------------------------------- 1 | package applicationconfiguration 2 | 3 | type Config struct { 4 | Server struct { 5 | Port string `envconfig:"SERVER_PORT" required:"true"` 6 | Host string `envconfig:"SERVER_HOST" required:"true"` 7 | } 8 | Database struct { 9 | Username string `envconfig:"DB_USERNAME"` 10 | Password string `envconfig:"DB_PASSWORD"` 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /configuration/application-configuration/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/applicationconfiguration 2 | 3 | go 1.14 4 | 5 | require github.com/kelseyhightower/envconfig v1.4.0 // indirect 6 | -------------------------------------------------------------------------------- /configuration/application-configuration/go.sum: -------------------------------------------------------------------------------- 1 | github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= 2 | github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= 3 | -------------------------------------------------------------------------------- /first-module-dependency/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "golang.source-fellows.com/training/sample" 5 | ) 6 | 7 | func main() { 8 | sample.Hello() 9 | } 10 | -------------------------------------------------------------------------------- /first-module-dependency/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/training/sample 2 | 3 | go 1.13 4 | 5 | require github.com/sirupsen/logrus v1.5.0 6 | -------------------------------------------------------------------------------- /first-module-dependency/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= 3 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q= 6 | github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= 7 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 8 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= 9 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 10 | -------------------------------------------------------------------------------- /first-module-dependency/main.go: -------------------------------------------------------------------------------- 1 | package sample 2 | 3 | import log "github.com/sirupsen/logrus" 4 | 5 | func Hello() { 6 | log.Println("Hello World") 7 | } 8 | -------------------------------------------------------------------------------- /golang-language-samples/collection-array-slice/collection.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | //Array mit fester Größe 8 | var months = [12]string{"Januar", 9 | "Februar", "März", "April", 10 | "Mai", "Juni", "Juli", "August", 11 | "September", "Oktober", "November", 12 | "Dezember"} 13 | 14 | //Slices mit immer 3 Elementen des Arrays 15 | q1 := months[:3] 16 | q2 := months[3:6] 17 | q3 := months[6:9] 18 | q4 := months[9:12] 19 | //Liefert "[Januar februar März]" 20 | fmt.Println(q1) 21 | fmt.Println(q2) 22 | fmt.Println(q3) 23 | fmt.Println(q4) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /golang-language-samples/collection-array/collection.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | //Initialisierung eines leeren Arrays 7 | var str1 [3]string 8 | //Befüllung durch Zuweisungen 9 | str1[0] = "Hello" 10 | str1[1] = "World" 11 | str1[2] = "!" 12 | fmt.Println(str1[0]) 13 | fmt.Println(len(str1)) 14 | 15 | //Initialsierung mit direkter Befüllung 16 | var str2 = [3]string{"Hello", "World", "!"} 17 | fmt.Println(str2[0]) 18 | fmt.Println(len(str2)) 19 | } 20 | -------------------------------------------------------------------------------- /golang-language-samples/collection-capa/collection.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | //Array mit fester Größe 8 | var months = [12]string{"Januar", 9 | "Februar", "März", "April", 10 | "Mai", "Juni", "Juli", "August", 11 | "September", "Oktober", "November", 12 | "Dezember"} 13 | 14 | //Slices mit immer 3 Elementen des Arrays 15 | q1 := months[:3] 16 | q2 := months[3:6] 17 | q3 := months[6:9] 18 | q4 := months[9:12] 19 | 20 | fmt.Printf("Length %v, Capa %v, Values: %v\n", len(q1), cap(q1), q1) 21 | fmt.Printf("Length %v, Capa %v, Values: %v\n", len(q2), cap(q2), q2) 22 | fmt.Printf("Length %v, Capa %v, Values: %v\n", len(q3), cap(q3), q3) 23 | fmt.Printf("Length %v, Capa %v, Values: %v\n", len(q4), cap(q4), q4) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /golang-language-samples/collection-for-range/collection.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | //Array mit fester Größe 8 | var months = [12]string{"Januar", 9 | "Februar", "März", "April", 10 | "Mai", "Juni", "Juli", "August", 11 | "September", "Oktober", "November", 12 | "Dezember"} 13 | for i, month := range months { 14 | fmt.Printf("%v: %v\n", i, month) 15 | } 16 | 17 | q2 := months[3:6] 18 | for i, month := range q2 { 19 | fmt.Printf("%v: %v\n", i, month) 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /golang-language-samples/defer-file-open/defer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | 10 | f, err := os.Open("testfile") 11 | if err != nil { 12 | log.Fatalf("Konnte die Datei nicht öffnen: %v", err) 13 | } 14 | defer f.Close() 15 | 16 | f2, err := os.Open("nichtVorhanden") 17 | if err != nil { 18 | log.Fatalf("Konnte die Datei nicht öffnen: %v", err) 19 | } 20 | defer f2.Close() 21 | //Arbeit mit den Dateien 22 | } 23 | -------------------------------------------------------------------------------- /golang-language-samples/defer-file-open/testfile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/golang-language-samples/defer-file-open/testfile -------------------------------------------------------------------------------- /golang-language-samples/defer-lock/defer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | ) 7 | 8 | var lock sync.Mutex 9 | 10 | func wasWichtiges() { 11 | lock.Lock() 12 | defer lock.Unlock() 13 | //auf ressourcen zugreifen 14 | fmt.Println("Arbeit an gesperrter Ressource") 15 | } 16 | 17 | func main() { 18 | 19 | wasWichtiges() 20 | lock.Lock() 21 | fmt.Println("am Ende") 22 | 23 | } 24 | -------------------------------------------------------------------------------- /golang-language-samples/defer/defer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func sagA() { 6 | fmt.Println("A") 7 | } 8 | 9 | func sagB() { 10 | fmt.Println("B") 11 | } 12 | 13 | func main() { 14 | defer sagA() 15 | sagB() 16 | } 17 | -------------------------------------------------------------------------------- /golang-language-samples/errors-new/errors.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | ) 7 | 8 | func erzeugeEinenNeuenFehler(i int) error { 9 | if i != 42 { 10 | return fmt.Errorf("Mit %v lösen Sie das Problem nicht", i) 11 | } 12 | return nil 13 | } 14 | 15 | func erzeugeNochEinenFehler() error { 16 | return errors.New("Da ist noch ein Problem") 17 | } 18 | 19 | func main() { 20 | err := erzeugeEinenNeuenFehler(13) 21 | if err != nil { 22 | fmt.Printf("Es ist ein Fehler aufgetreten: %v\n", err) 23 | } 24 | err = erzeugeNochEinenFehler() 25 | if err != nil { 26 | fmt.Printf("Es ist ein Fehler aufgetreten: %v\n", err) 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /golang-language-samples/errors-owntyp-wrap/errors.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "log" 7 | ) 8 | 9 | var PermissionError = errors.New("keine Berechtigung") 10 | 11 | //StatementExecutionError ist ein eigener Fehlertyp 12 | //mit zusätzlicher Information 13 | type StatementExecutionError struct { 14 | Statement string 15 | Message string 16 | Cause error 17 | } 18 | 19 | func (wse *StatementExecutionError) Unwrap() error { 20 | return wse.Cause 21 | } 22 | 23 | //Implementierung der Error Methode des error Interface 24 | func (wse *StatementExecutionError) Error() string { 25 | return wse.Message 26 | } 27 | 28 | func fuehreStatementAus(statement string) error { 29 | if statement != "1=1" { 30 | return &StatementExecutionError{ 31 | Statement: statement, 32 | Message: "Statement kann nicht ausgeführt werden", 33 | Cause: PermissionError, 34 | } 35 | } 36 | return nil 37 | } 38 | 39 | func main() { 40 | err := fuehreStatementAus("v:=b") 41 | //Prüfung über errors Package 42 | if errors.Is(err, PermissionError) { 43 | log.Fatal("Das Statement konnte nicht ausgeführt werden.") 44 | } else { 45 | fmt.Println("alles ok") 46 | } 47 | 48 | panic("") 49 | } 50 | -------------------------------------------------------------------------------- /golang-language-samples/errors-owntyp/errors.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | ) 7 | 8 | //StatementExecutionError ist ein eigener Fehlertyp 9 | //mit zusätzlicher Information 10 | type StatementExecutionError struct { 11 | Statement string 12 | Message string 13 | } 14 | 15 | //Implementierung der Error Methode des error Interface 16 | func (wse *StatementExecutionError) Error() string { 17 | return wse.Message 18 | } 19 | 20 | func fuehreStatementAus(statement string) error { 21 | if statement != "1=1" { 22 | return &StatementExecutionError{ 23 | Statement: statement, 24 | Message: "Statement kann nicht ausgeführt werden", 25 | } 26 | } 27 | return nil 28 | } 29 | 30 | func main() { 31 | err := fuehreStatementAus("v:=b") 32 | //Prüfung über Simple-Statement mit Type-Assertion 33 | if e, ok := err.(*StatementExecutionError); ok { 34 | log.Fatalf("Das Statement '%v' konnte nicht ausgeführt werden.", e.Statement) 35 | } else { 36 | fmt.Println("alles ok") 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /golang-language-samples/errors/errors.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | f, err := os.Open("datei-ist-nicht-da.txt") 10 | if err != nil { 11 | //Durch den Aufruf von log.Fatalf wird die weitere 12 | // Verarbeitung abgebrochen und die Anwendung beendet 13 | log.Fatalf("Konnte die Datei nicht öffnen, da %v", err) 14 | } 15 | f.Close() 16 | } 17 | -------------------------------------------------------------------------------- /golang-language-samples/fmt-package/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Printf("Hello %q", "hello") 7 | } 8 | -------------------------------------------------------------------------------- /golang-language-samples/for-range/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | text := "Hello" 8 | 9 | //Nur Zuweisung des Index 10 | for v := range text { 11 | fmt.Print(v) 12 | } 13 | 14 | //Zuweisung von Index und Wert 15 | for i, v := range text { 16 | fmt.Printf("index %d value %c\n", i, v) 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /golang-language-samples/pointer-parameter/pointer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Person struct { 6 | Name string 7 | } 8 | 9 | func sayHello(p interface{}) { 10 | t, ok := p.(*Person) 11 | fmt.Println(t) 12 | fmt.Println(ok) 13 | } 14 | 15 | func main() { 16 | 17 | p := &Person{"Kristian"} 18 | sayHello(p) 19 | 20 | } 21 | -------------------------------------------------------------------------------- /golang-language-samples/pointer-receiver/pointer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type Person struct { 8 | alter int 9 | } 10 | 11 | func (p *Person) Alter() int { 12 | return p.alter 13 | } 14 | 15 | func (p *Person) GeburtstagFeiern() { 16 | p.alter = p.alter + 1 17 | } 18 | 19 | func main() { 20 | p := Person{5} 21 | p.GeburtstagFeiern() 22 | fmt.Println(p.Alter()) 23 | } 24 | -------------------------------------------------------------------------------- /golang-language-samples/pointer-struct/pointer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Person struct { 6 | Name string 7 | } 8 | 9 | func main() { 10 | p := &Person{} 11 | p.Name = "Kristian" 12 | (*p).Name = "Kristian" //beide Zeilen sind gleichbedeutend 13 | fmt.Println(p) 14 | } 15 | -------------------------------------------------------------------------------- /golang-language-samples/pointer/pointer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | var month = "Januar" 8 | //Erstellt einen Pointer auf die Variable month 9 | var monthPtr = &month 10 | //Ändern den Wert von month über den Pointer monthPtr 11 | *monthPtr = "Februar" 12 | 13 | fmt.Println(month) 14 | 15 | } 16 | -------------------------------------------------------------------------------- /golang-language-samples/sampledoc/first.go: -------------------------------------------------------------------------------- 1 | //Package sampledoc zeigt wie man Integer Werte addiert. 2 | package sampledoc 3 | 4 | //Add addiert zwei Integer Werte und liefert das Ergebnis. 5 | func Add(first int, second int) int { 6 | return first + second 7 | } 8 | -------------------------------------------------------------------------------- /golang-language-samples/sampledoc/first_example_test.go: -------------------------------------------------------------------------------- 1 | package sampledoc 2 | 3 | import "fmt" 4 | 5 | func ExampleAdd() { 6 | 7 | res := Add(1, 2) 8 | fmt.Println(res) 9 | 10 | //Output: 3 11 | 12 | } 13 | -------------------------------------------------------------------------------- /golang-language-samples/switch-case-falltrough/switch-case.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | val := 2 8 | var erg int 9 | 10 | switch { 11 | case val == 2: 12 | erg = val * 2 13 | fallthrough 14 | case val < 10: 15 | erg = erg * 2 16 | case val < 100: 17 | erg = erg * 3 18 | default: 19 | erg = 1 20 | } 21 | 22 | fmt.Println(erg) 23 | 24 | } 25 | -------------------------------------------------------------------------------- /golang-language-samples/switch-case-no-expression/switch-case.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | val := 2 * 5 8 | 9 | switch { 10 | case val == 10: 11 | fmt.Println("10!") 12 | case val < 10: 13 | fmt.Println("kleiner 10") 14 | default: 15 | fmt.Println("größer 10") 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /golang-language-samples/switch-case/switch-case.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | val := 2 * 6 8 | 9 | switch val { 10 | case 10: 11 | fmt.Println("10!") 12 | case 1, 2, 3, 4, 5, 6, 7, 8, 9: 13 | fmt.Println("kleiner 10") 14 | default: 15 | fmt.Println("größer 10") 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /golang-language-samples/type-alias-definition/type-definition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type ( 6 | A1 = string // A1 und string bezeichen identische Typen 7 | A2 = A1 // A2 und A1 bezeichen identische Typen 8 | ) 9 | 10 | func main() { 11 | var text A2 12 | fmt.Println(text) 13 | } 14 | -------------------------------------------------------------------------------- /golang-language-samples/type-definition-assertion/type-definition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | //Variable muss ein Interfacetyp sein 7 | var i interface{} = "hello" 8 | 9 | //direkte Zuweisung ohne Prüfung 10 | s := i.(string) 11 | fmt.Println(s) 12 | 13 | //Zuweisung mit Prüfung ob möglich 14 | s, ok := i.(string) 15 | fmt.Println(s, ok) 16 | 17 | //Zuweisung mit Prüfung ob möglich 18 | f, ok := i.(int) 19 | fmt.Println(f, ok) 20 | 21 | //Zuweisung ohne Prüfung führt zu Panic 22 | f = i.(int) 23 | fmt.Println(f) 24 | } 25 | -------------------------------------------------------------------------------- /golang-language-samples/type-definition-interface-receiver/type-definition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Dog interface { 6 | Bark() 7 | } 8 | 9 | type Dackel struct { 10 | name string 11 | } 12 | 13 | func (d Dackel) Bark() { 14 | fmt.Printf("Wuff %v\n", d.name) 15 | } 16 | 17 | func main() { 18 | var d Dog 19 | d = Dackel{"Heino"} 20 | d.Bark() 21 | } 22 | -------------------------------------------------------------------------------- /golang-language-samples/type-definition-interface/type-definition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type File interface { 6 | Read([]byte) (int, error) 7 | Write([]byte) (int, error) 8 | Close() error 9 | } 10 | 11 | func main() { 12 | 13 | var f File 14 | fmt.Println(f) 15 | 16 | } 17 | -------------------------------------------------------------------------------- /golang-language-samples/type-definition-struct/type-definition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Person struct { 6 | vorname string 7 | nachname string 8 | } 9 | 10 | func main() { 11 | p := Person{"Kristian", "Köhler"} 12 | p2 := Person{nachname: "Köhler"} 13 | fmt.Println(p.vorname) 14 | fmt.Println(p2.nachname) 15 | } 16 | -------------------------------------------------------------------------------- /golang-language-samples/type-definition-switch/type-definition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func checkMyType(i interface{}) { 6 | switch i.(type) { 7 | case string: 8 | fmt.Println("Es ist ein string") 9 | case int: 10 | fmt.Println("Es ist ein int") 11 | default: 12 | fmt.Println("Es ist weder string noch int") 13 | } 14 | } 15 | 16 | func main() { 17 | checkMyType("Hello") 18 | checkMyType(1) 19 | checkMyType(3.4) 20 | } 21 | -------------------------------------------------------------------------------- /golang-language-samples/type-definition/type-definition.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type ( 6 | Point struct{ x, y float64 } 7 | polar Point 8 | ) 9 | 10 | func main() { 11 | var p Point 12 | fmt.Println(p) 13 | } 14 | -------------------------------------------------------------------------------- /golang-language-samples/type-failure/type-failure.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | var i int = "Hello" 8 | 9 | fmt.Println(i) 10 | 11 | } 12 | -------------------------------------------------------------------------------- /golang-language-samples/varadic-parameter/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func something(text string, i ...int) int { 6 | fmt.Println(i[0]) 7 | return i[1] 8 | } 9 | 10 | func main() { 11 | fmt.Println(something("hello", 1, 2)) 12 | } 13 | -------------------------------------------------------------------------------- /hello-channel-post/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func f(left, right chan int) { 6 | left <- 1 + <-right 7 | } 8 | 9 | func main() { 10 | leftmost := make(chan int) 11 | var left, right chan int = nil, leftmost 12 | for i := 0; i < 100000; i++ { 13 | left, right = right, make(chan int) 14 | go f(left, right) 15 | } 16 | right <- 0 17 | x := <-leftmost 18 | fmt.Println(x) 19 | } 20 | -------------------------------------------------------------------------------- /hello-channel/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | c := make(chan string) 7 | //Wert in Channel schreiben 8 | c <- "Hello World" 9 | //Wert aus Channel lesen 10 | fmt.Println(<-c) 11 | } 12 | -------------------------------------------------------------------------------- /hello-go-channel/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func sayHello(c chan string) { 8 | fmt.Println("vor") 9 | c <- "Hello World" 10 | fmt.Println("nach") 11 | } 12 | 13 | func main() { 14 | c := make(chan string) 15 | go sayHello(c) 16 | fmt.Println("vor2") 17 | text := <-c 18 | fmt.Println("nach2") 19 | fmt.Println(text) 20 | } 21 | -------------------------------------------------------------------------------- /hello-interface/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type QuasselStrippe interface { 6 | Quassel() 7 | } 8 | 9 | type Person struct { 10 | name string 11 | } 12 | 13 | func (p *Person) Quassel() { 14 | fmt.Printf("Hi. Meine Name ist %s\n", p.name) 15 | } 16 | 17 | func VielQuasseln(qs QuasselStrippe) { 18 | for i := 0; i < 3; i++ { 19 | qs.Quassel() 20 | } 21 | } 22 | 23 | func main() { 24 | s := &Person{name: "Quassel-Philip"} 25 | s.Quassel() 26 | VielQuasseln(s) 27 | } 28 | -------------------------------------------------------------------------------- /hello-world/cplusplus/README.md: -------------------------------------------------------------------------------- 1 | Starten des docker Containers 2 | ----------------------------- 3 | 4 | ``` 5 | docker run -v `pwd`:/tmp -it gcc:latest /bin/bash 6 | ``` 7 | 8 | Danach im Container 9 | ``` 10 | cd /tmp 11 | time g++ hello.cpp 12 | ``` 13 | -------------------------------------------------------------------------------- /hello-world/cplusplus/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/hello-world/cplusplus/a.out -------------------------------------------------------------------------------- /hello-world/cplusplus/hello.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | std::cout << "Hello World!"; 5 | return 0; 6 | } -------------------------------------------------------------------------------- /hello-world/go/README.md: -------------------------------------------------------------------------------- 1 | Starten des docker Containers 2 | ----------------------------- 3 | 4 | ``` 5 | docker run -v `pwd`:/tmp -it golang:latest /bin/bash 6 | ``` 7 | 8 | Danach im Container 9 | ``` 10 | cd /tmp 11 | time go build main.go 12 | ``` 13 | -------------------------------------------------------------------------------- /hello-world/go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello, 世界") 7 | } 8 | -------------------------------------------------------------------------------- /hello-world/java/Main.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /hello-world/java/README.md: -------------------------------------------------------------------------------- 1 | Starten des docker Containers 2 | ----------------------------- 3 | 4 | ``` 5 | docker run -v `pwd`:/tmp -it openjdk:latest /bin/bash 6 | ``` 7 | 8 | Danach im Container 9 | ``` 10 | cd /tmp 11 | time javac Main.java 12 | ``` -------------------------------------------------------------------------------- /hello-world/javascript/README.md: -------------------------------------------------------------------------------- 1 | Starten des docker Containers 2 | ----------------------------- 3 | 4 | ``` 5 | docker run -v `pwd`:/tmp -it node:latest /bin/bash 6 | ``` 7 | 8 | Danach im Containers 9 | ``` 10 | cd /tmp 11 | node index.js 12 | ``` -------------------------------------------------------------------------------- /hello-world/javascript/index.js: -------------------------------------------------------------------------------- 1 | console.log("Hello World") -------------------------------------------------------------------------------- /hello-world/python/README.md: -------------------------------------------------------------------------------- 1 | Starten des docker Containers 2 | ----------------------------- 3 | 4 | ``` 5 | docker run -v `pwd`:/tmp -it python:latest /bin/bash 6 | ``` 7 | 8 | Danach im Containers 9 | ``` 10 | cd /tmp 11 | time python hello.py 12 | ``` -------------------------------------------------------------------------------- /hello-world/python/hello.py: -------------------------------------------------------------------------------- 1 | print("Hello World.") -------------------------------------------------------------------------------- /hello-world/typescript/README.md: -------------------------------------------------------------------------------- 1 | Starten des docker Containers 2 | ----------------------------- 3 | 4 | ``` 5 | docker run -v `pwd`:/tmp -it node:latest /bin/bash 6 | ``` 7 | 8 | Danach im Container 9 | ``` 10 | npm install -g tsc 11 | cd /tmp 12 | time tsc hello.ts 13 | ``` 14 | -------------------------------------------------------------------------------- /hello-world/typescript/hello.js: -------------------------------------------------------------------------------- 1 | function greeter(person) { 2 | return "Hello, " + person; 3 | } 4 | var user = "Jane User"; 5 | console.log(greeter(user)); 6 | -------------------------------------------------------------------------------- /hello-world/typescript/hello.ts: -------------------------------------------------------------------------------- 1 | function greeter(person) { 2 | return "Hello, " + person; 3 | } 4 | 5 | let user = "Jane User"; 6 | 7 | console.log(greeter(user)); -------------------------------------------------------------------------------- /library-dependency/go.mod: -------------------------------------------------------------------------------- 1 | module library-dependency 2 | 3 | go 1.13 4 | 5 | require github.com/SourceFellows/somelib v0.0.2 6 | -------------------------------------------------------------------------------- /library-dependency/go.sum: -------------------------------------------------------------------------------- 1 | github.com/SourceFellows/somelib v0.0.2 h1:mRbh02CVY098DPnLaTGeBeeCfYkoBY8U2v3ZJk/Zyvo= 2 | github.com/SourceFellows/somelib v0.0.2/go.mod h1:acrwWUtWlfzG22eWIsriyO+Qu6it2dIHQZO21HFpRwY= 3 | -------------------------------------------------------------------------------- /library-dependency/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/SourceFellows/somelib" 4 | 5 | func main() { 6 | 7 | somelib.CallLib() 8 | 9 | } 10 | -------------------------------------------------------------------------------- /microservices/container/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:latest 2 | 3 | COPY main.go /app/main.go 4 | WORKDIR /app 5 | RUN go build main.go 6 | 7 | CMD [ "/app/main"] -------------------------------------------------------------------------------- /microservices/container/README.md: -------------------------------------------------------------------------------- 1 | # Ausführen 2 | 3 | ``` 4 | docker build -t microservice-buch . 5 | docker run microservice-buch 6 | ``` -------------------------------------------------------------------------------- /microservices/container/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello World!") 7 | } 8 | -------------------------------------------------------------------------------- /microservices/default-mux-demo/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "net/http" 4 | 5 | type myHandler struct { 6 | Name string 7 | } 8 | 9 | func (h *myHandler) ServeHTTP(r http.ResponseWriter, _ *http.Request) { 10 | r.Write([]byte(h.Name)) 11 | } 12 | 13 | func main() { 14 | handler1 := &myHandler{Name: "handler1"} 15 | handler2 := &myHandler{Name: "handler2"} 16 | handler3 := &myHandler{Name: "handler3"} 17 | handler4 := &myHandler{Name: "handler4"} 18 | handler5 := &myHandler{Name: "handler5"} 19 | handler6 := &myHandler{Name: "handler6"} 20 | 21 | http.Handle("/", handler1) 22 | http.Handle("/book", handler2) 23 | http.Handle("/books/", handler3) 24 | http.Handle("/books/Hobbit", handler4) 25 | http.Handle("/theme", handler5) 26 | http.Handle("/theme/", handler6) 27 | http.ListenAndServeTLS(":8080", nil) 28 | } 29 | -------------------------------------------------------------------------------- /microservices/formrequest/Form-request.http: -------------------------------------------------------------------------------- 1 | POST http://localhost:8080/ HTTP/1.1 2 | Content-Type: application/x-www-form-urlencoded 3 | 4 | name=Peter -------------------------------------------------------------------------------- /microservices/formrequest/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/formrequest 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /microservices/formrequest/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "net/http" 4 | 5 | func handleForm(rw http.ResponseWriter, rq *http.Request) { 6 | err := rq.ParseForm() 7 | if err != nil { 8 | rw.WriteHeader(http.StatusNotAcceptable) 9 | return 10 | } 11 | name := rq.Form.Get("name") 12 | rw.Write([]byte("Hello " + name)) 13 | } 14 | 15 | func main() { 16 | http.HandleFunc("/", handleForm) 17 | http.ListenAndServe(":8080", nil) 18 | } 19 | -------------------------------------------------------------------------------- /microservices/go-channel/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func main() { 10 | 11 | tc := make(chan time.Time) 12 | 13 | var c int64 = 0 14 | 15 | go func() { 16 | for { 17 | go func() { 18 | for { 19 | c++ 20 | i := rand.Int31n(10) 21 | time.Sleep(time.Duration(i) * time.Millisecond) 22 | tc <- time.Now() 23 | } 24 | }() 25 | } 26 | }() 27 | 28 | for t := range tc { 29 | fmt.Println(t) 30 | fmt.Println(c) 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /microservices/gorilla-mux/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/gorillamux 2 | 3 | go 1.13 4 | 5 | require github.com/gorilla/mux v1.7.4 6 | -------------------------------------------------------------------------------- /microservices/gorilla-mux/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= 2 | github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /microservices/gorilla-mux/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/gorilla/mux" 7 | ) 8 | 9 | func myFunc(rw http.ResponseWriter, rq *http.Request) { 10 | rw.Write([]byte("Hello World")) 11 | } 12 | 13 | func main() { 14 | r := mux.NewRouter() 15 | r.PathPrefix("/").HandlerFunc(myFunc) 16 | r.Methods("GET") 17 | http.ListenAndServe(":8080", r) 18 | } 19 | -------------------------------------------------------------------------------- /microservices/gorm-hooks/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/gorm 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/Unknwon/log v0.0.0-20200308114134-929b1006e34a 7 | github.com/jinzhu/gorm v1.9.14 8 | ) 9 | -------------------------------------------------------------------------------- /microservices/gorm-hooks/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | "github.com/jinzhu/gorm" 8 | _ "github.com/jinzhu/gorm/dialects/sqlite" 9 | ) 10 | 11 | type Customer struct { 12 | gorm.Model 13 | FirstName string 14 | LastName string 15 | } 16 | 17 | func (u *Customer) BeforeUpdate() (err error) { 18 | fmt.Println("before update") 19 | return nil 20 | } 21 | 22 | func main() { 23 | db, err := gorm.Open("sqlite3", "test.db") 24 | if err != nil { 25 | panic("failed to connect database") 26 | } 27 | defer db.Close() 28 | 29 | // LogMode enable 30 | db.LogMode(true) 31 | 32 | // Migrate the schema 33 | err = db.AutoMigrate(&Customer{}).Error 34 | if err != nil { 35 | log.Fatalf("error migrating ", err) 36 | } 37 | 38 | // Create 39 | customer := Customer{FirstName: "Hans", LastName: "wurst"} 40 | db.Create(&customer) 41 | 42 | var foundCustomer Customer 43 | // Plain SQL 44 | db.First(&foundCustomer, "First_Name = ?", "Hans") 45 | // Template 46 | db.Where(&Customer{FirstName: "Hans"}).First(&foundCustomer) 47 | 48 | fmt.Println("Gefunden wurde:", foundCustomer.FirstName) 49 | 50 | // Update - update product's price to 2000 51 | db.Model(&foundCustomer).Update("LastName", "Meiser") 52 | 53 | err = db.Delete(&foundCustomer).Error 54 | if err != nil { 55 | panic(err) 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /microservices/gorm-hooks/test.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/gorm-hooks/test.db -------------------------------------------------------------------------------- /microservices/gorm-relation/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/gormrelation 2 | 3 | go 1.14 4 | 5 | require github.com/jinzhu/gorm v1.9.14 6 | -------------------------------------------------------------------------------- /microservices/gorm-relation/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/jinzhu/gorm" 7 | _ "github.com/jinzhu/gorm/dialects/sqlite" 8 | ) 9 | 10 | type Customer struct { 11 | gorm.Model 12 | FirstName string 13 | LastName string 14 | CreditCard CreditCard 15 | CreditCardId uint 16 | } 17 | 18 | type CreditCard struct { 19 | gorm.Model 20 | Number string 21 | } 22 | 23 | func main() { 24 | db, err := gorm.Open("sqlite3", "test.db") 25 | if err != nil { 26 | panic("failed to connect database") 27 | } 28 | defer db.Close() 29 | 30 | // LogMode enable 31 | db.LogMode(true) 32 | 33 | // Migrate the schema 34 | db.AutoMigrate(&CreditCard{}, &Customer{}) 35 | 36 | // Create 37 | customer := Customer{FirstName: "Hans", LastName: "wurst"} 38 | customer.CreditCard = CreditCard{Number: "123-123-123"} 39 | db.Set("gorm:association_autoupdate", false).Set("gorm:association_autoupdate", true).Create(&customer) 40 | 41 | var foundCustomer Customer 42 | var foundCreditCard CreditCard 43 | 44 | db.Where(&Customer{FirstName: "Hans"}).First(&foundCustomer) 45 | db.Model(&foundCustomer).Related(&foundCreditCard) 46 | 47 | fmt.Println("Gefunden wurde:", foundCreditCard.Number) 48 | 49 | db.Delete(&foundCustomer) 50 | 51 | } 52 | -------------------------------------------------------------------------------- /microservices/gorm/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/gorm 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/Unknwon/log v0.0.0-20200308114134-929b1006e34a 7 | github.com/jinzhu/gorm v1.9.14 8 | ) 9 | -------------------------------------------------------------------------------- /microservices/gorm/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | "github.com/jinzhu/gorm" 8 | _ "github.com/jinzhu/gorm/dialects/sqlite" 9 | ) 10 | 11 | type Customer struct { 12 | gorm.Model 13 | FirstName string 14 | LastName string 15 | } 16 | 17 | func main() { 18 | db, err := gorm.Open("sqlite3", "test.db") 19 | if err != nil { 20 | panic("failed to connect database") 21 | } 22 | defer db.Close() 23 | 24 | // LogMode enable 25 | db.LogMode(true) 26 | 27 | // Migrate the schema 28 | err = db.AutoMigrate(&Customer{}).Error 29 | if err != nil { 30 | log.Fatalf("error migrating ", err) 31 | } 32 | 33 | // Create 34 | customer := Customer{FirstName: "Hans", LastName: "wurst"} 35 | db.Create(&customer) 36 | 37 | var foundCustomer Customer 38 | // Plain SQL 39 | db.First(&foundCustomer, "First_Name = ?", "Hans") 40 | // Template 41 | db.Where(&Customer{FirstName: "Hans"}).First(&foundCustomer) 42 | 43 | fmt.Println("Gefunden wurde:", foundCustomer.FirstName) 44 | 45 | // Update - update product's price to 2000 46 | db.Model(&foundCustomer).Update("LastName", "Meiser") 47 | 48 | err = db.Delete(&foundCustomer).Error 49 | if err != nil { 50 | panic(err) 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /microservices/grpc/client/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | 7 | "golang.source-fellows.com/samples/grpc/hello" 8 | "google.golang.org/grpc" 9 | "google.golang.org/protobuf/types/known/emptypb" 10 | ) 11 | 12 | func main() { 13 | con, _ := grpc.Dial(":8080", grpc.WithInsecure()) 14 | client := hello.NewHelloWorldServiceClient(con) 15 | 16 | ctx := context.Background() 17 | answer, err := client.SayHello(ctx, &emptypb.Empty{}) 18 | if err != nil { 19 | log.Fatal(err) 20 | } 21 | log.Println(answer.GetMessageText()) 22 | } 23 | -------------------------------------------------------------------------------- /microservices/grpc/client/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/grpc/client 2 | 3 | go 1.13 4 | 5 | require ( 6 | golang.source-fellows.com/samples/grpc/hello v0.0.0 7 | google.golang.org/grpc v1.30.0 8 | google.golang.org/protobuf v1.25.0 9 | ) 10 | 11 | replace golang.source-fellows.com/samples/grpc/hello => ./../hello 12 | -------------------------------------------------------------------------------- /microservices/grpc/hello/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/grpc/hello 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/golang/protobuf v1.4.2 7 | google.golang.org/grpc v1.30.0 8 | google.golang.org/protobuf v1.25.0 9 | ) 10 | -------------------------------------------------------------------------------- /microservices/grpc/hello/hello-message.proto: -------------------------------------------------------------------------------- 1 | syntax="proto3"; 2 | 3 | package hello; 4 | 5 | option go_package = "golang.source-fellows.com/samples/grpc/hello"; 6 | 7 | import "google/protobuf/empty.proto"; 8 | 9 | message HelloMessage { 10 | string messageText = 1; 11 | } 12 | 13 | service HelloWorldService { 14 | rpc sayHello(google.protobuf.Empty) returns (HelloMessage); 15 | } -------------------------------------------------------------------------------- /microservices/grpc/server/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net" 6 | 7 | "golang.source-fellows.com/samples/grpc/hello" 8 | "golang.source-fellows.com/samples/grpc/server" 9 | "google.golang.org/grpc" 10 | ) 11 | 12 | func main() { 13 | srv := grpc.NewServer() 14 | hello.RegisterHelloWorldServiceServer(srv, &server.HelloService{}) 15 | listener, _ := net.Listen("tcp", ":8080") 16 | log.Println("Starting Server ...") 17 | log.Fatal(srv.Serve(listener)) 18 | } 19 | -------------------------------------------------------------------------------- /microservices/grpc/server/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/grpc/server 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/golang/protobuf v1.4.2 7 | golang.source-fellows.com/samples/grpc/hello v0.0.0 8 | google.golang.org/grpc v1.30.0 9 | google.golang.org/grpc/cmd/protoc-gen-go-grpc v0.0.0-20200630190442-3de8449f8555 // indirect 10 | google.golang.org/protobuf v1.25.0 11 | ) 12 | 13 | replace golang.source-fellows.com/samples/grpc/hello => ./../hello 14 | -------------------------------------------------------------------------------- /microservices/grpc/server/helloService.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | context "context" 5 | 6 | empty "github.com/golang/protobuf/ptypes/empty" 7 | "golang.source-fellows.com/samples/grpc/hello" 8 | ) 9 | 10 | type HelloService struct { 11 | hello.UnimplementedHelloWorldServiceServer 12 | } 13 | 14 | func (hs *HelloService) SayHello(context.Context, *empty.Empty) (*hello.HelloMessage, error) { 15 | msg := &hello.HelloMessage{MessageText: "Hello World"} 16 | return msg, nil 17 | } 18 | -------------------------------------------------------------------------------- /microservices/hanlder-impl/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | 6 | "golang.source-fellows.com/samples/httphandler" 7 | ) 8 | 9 | func main() { 10 | http.ListenAndServe(":8080", &httphandler.MyHandler{}) 11 | } 12 | -------------------------------------------------------------------------------- /microservices/hanlder-impl/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/httphandler 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /microservices/hanlder-impl/go.sum: -------------------------------------------------------------------------------- 1 | golang.source-fellows.com v0.0.0-20200619133407-79aa8981720c h1:c0BKYhs4PULAm3Hb5f+t9AcmPRScdSAkeuajndyOMLQ= 2 | -------------------------------------------------------------------------------- /microservices/hanlder-impl/handler.go: -------------------------------------------------------------------------------- 1 | package httphandler 2 | 3 | import "net/http" 4 | 5 | type MyHandler struct{} 6 | 7 | func (mh *MyHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) { 8 | writer.Write([]byte("Hello World!")) 9 | } 10 | -------------------------------------------------------------------------------- /microservices/http-client-circut-breaker/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/httpcircuitbreaker 2 | 3 | go 1.13 4 | 5 | require github.com/sony/gobreaker v0.4.1 6 | -------------------------------------------------------------------------------- /microservices/http-client-circut-breaker/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 3 | github.com/sony/gobreaker v0.4.1 h1:oMnRNZXX5j85zso6xCPRNPtmAycat+WcoKbklScLDgQ= 4 | github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= 5 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 6 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 7 | -------------------------------------------------------------------------------- /microservices/http-client-circut-breaker/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | "net/http" 8 | "time" 9 | 10 | breaker "github.com/sony/gobreaker" 11 | ) 12 | 13 | var cb *breaker.CircuitBreaker 14 | 15 | func main() { 16 | 17 | settings := breaker.Settings{Interval: 1 * time.Second, Timeout: 5 * time.Second} 18 | cb = breaker.NewCircuitBreaker(settings) 19 | 20 | for { 21 | bites, err := cb.Execute(func() (interface{}, error) { 22 | res, err := http.Get("http://localhost:8080/a") 23 | if err != nil { 24 | return nil, err 25 | } 26 | if res.StatusCode == http.StatusOK { 27 | 28 | } 29 | defer res.Body.Close() 30 | bites, _ := ioutil.ReadAll(res.Body) 31 | return bites, nil 32 | }) 33 | 34 | if err != nil { 35 | log.Printf("error %v\n", err) 36 | 37 | } else { 38 | fmt.Println(string(bites.([]byte))) 39 | } 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /microservices/http-client-config/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | client := http.Client{Transport: &http.Transport{ 10 | ResponseHeaderTimeout: 1 * time.Millisecond, 11 | }} 12 | res, err := client.Get("https://google.com") 13 | if err != nil { 14 | panic(err) 15 | } 16 | defer res.Body.Close() 17 | } 18 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/ca/index.txt: -------------------------------------------------------------------------------- 1 | V 210709041547Z 01 unknown /C=DE/ST=BW/L=Reutlingen/O=Source Fellows GmbH/CN=test.example.private/emailAddress=user@example.private 2 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/ca/index.txt.attr: -------------------------------------------------------------------------------- 1 | unique_subject = yes 2 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/ca/index.txt.attr.old: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/http-client-ssl-cert/certs/ca/index.txt.attr.old -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/ca/index.txt.old: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/http-client-ssl-cert/certs/ca/index.txt.old -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/ca/serial: -------------------------------------------------------------------------------- 1 | 02 2 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/ca/serial.old: -------------------------------------------------------------------------------- 1 | 01 2 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/myRoot.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDhTCCAm2gAwIBAgIUS7TqIYrkdb2kEmtuawe8h/54VVIwDQYJKoZIhvcNAQEL 3 | BQAwUjELMAkGA1UEBhMCREUxEDAOBgNVBAgMB0JhV8ODwrwxEzARBgNVBAcMClJl 4 | dXRsaW5nZW4xHDAaBgNVBAoME1NvdXJjZSBGZWxsb3dzIEdtYkgwHhcNMjAwNzA4 5 | MDM1ODE4WhcNMzAwNzA2MDM1ODE4WjBSMQswCQYDVQQGEwJERTEQMA4GA1UECAwH 6 | QmFXw4PCvDETMBEGA1UEBwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZl 7 | bGxvd3MgR21iSDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANY+x2Kr 8 | U49506heEh2s0Q5vTjXUbP5814pHhlJ43X3p7QYMbJ/BBunxLA3a8GvuukgD1Wxr 9 | 0T0uHo5/MLxRh6r1TEy80IApUsyCcAO3jdTGtIr4iebnAOL/Dw0TGWDRyl5SZCwo 10 | BsVSOfCoJyMs+ygB+BLE+05Dq5FwA3CpxP5r67efBpY2Y9bu1iXAC179rGaVvtLM 11 | 3Zy5G+tQ0aMJYOqVR6tvSrrYg44gJiM270u6Os80V723fPtEUvKbHQOpZoleHWsb 12 | uZ1FbdQBcnJ2u3bI93ewQAvqQ3tjOqgell5n7kJ1B3Vjk8bVzda1zPKH9vcvF0fc 13 | gXdPrVOzBCa5Su8CAwEAAaNTMFEwHQYDVR0OBBYEFO3om6AZ9q/+r6kXo7hAhOBD 14 | SeTbMB8GA1UdIwQYMBaAFO3om6AZ9q/+r6kXo7hAhOBDSeTbMA8GA1UdEwEB/wQF 15 | MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAMxKFUWYTQz/ACVY8lV18QCGxcvNj3Uz 16 | ZdtNUEDkxu6yKsl+1otREbuxqnfNjrGLmlLQfU/GlY+PCF85SYm1Ep4yKdZ7J+1c 17 | MG+PB5NS3cV7BnoJ6WnXk7KJRX+dYpCr+jQmQbtTshk6dM2a6THjyPzKkyDflcfM 18 | tXQROGDG9O/5DvF8Eqz13Gel5Wukept9mHtDHapU0BooFTXNqh+tPmFU76tKCQZV 19 | h0Qt9BC8A6bDEaULVlXpvNTuDCrCMGhiYEo2++PD/e5YT1UDVNSokanyFIUoCJAn 20 | X5u+VjRfaVibiWu8E63qoKv+oXBf9pwlNeE8KBlydzf5OQgx8FEkbfc= 21 | -----END CERTIFICATE----- 22 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/myRoot.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEowIBAAKCAQEA1j7HYqtTj3nTqF4SHazRDm9ONdRs/nzXikeGUnjdfentBgxs 3 | n8EG6fEsDdrwa+66SAPVbGvRPS4ejn8wvFGHqvVMTLzQgClSzIJwA7eN1Ma0iviJ 4 | 5ucA4v8PDRMZYNHKXlJkLCgGxVI58KgnIyz7KAH4EsT7TkOrkXADcKnE/mvrt58G 5 | ljZj1u7WJcALXv2sZpW+0szdnLkb61DRowlg6pVHq29KutiDjiAmIzbvS7o6zzRX 6 | vbd8+0RS8psdA6lmiV4daxu5nUVt1AFycna7dsj3d7BAC+pDe2M6qB6WXmfuQnUH 7 | dWOTxtXN1rXM8of29y8XR9yBd0+tU7MEJrlK7wIDAQABAoIBAQC0GzaWthybAvly 8 | ExL7q2pbzipbimbOUbkI4DQFZOIelebSLOOsMSNUy5ATXbi7o4cqPz7N1J2j5v99 9 | H2OjDI8WovwvjpNhClw23Hi6RzaOZzUjluinGg9exLisbx/oirSrMmxPyp5Wh1d5 10 | /oJYSdohufYIIVx6YNz8eMQLlEys7BcAs9uDbfrEh2lhSCRd+eVER/p2U+EE3YFe 11 | OPhE7S7/5fDHidA/vU5YL0zyDcTiqec8lF3b63L39zYsckUjvbJ9gXru+RUFsnYT 12 | FCz/8axvSyiRj9M162BtrwV1lANgbck+unmk9j63bMaNUFHdlFxovxZDA4C6napJ 13 | +5dnwduBAoGBAO3UqDRxSWvelTv5vSFWaiJoFKU23PGX46Fsy+b8ttj+8Jz94333 14 | u+1t5fEnpS46QhQtiVRB42pezBncJ+CdjQ92ikcJZL7o8NYttlmTVg2i/v+dnxmL 15 | T+Tgl89VUOgfKdlRwpgG64dMT1HFQJlMKkKMzdS6FxQtSclYSq0yYVxhAoGBAOac 16 | 2ijcy0xlhDinsmikxBveaxl8pTeF0jmLlXmNRTuSXkk9tXi5yurV+e4M9oG7pvYU 17 | 4LUthIvtbmPgDhANN5+45bJqNWw3ovaLb0/60XsET4fiR8W0muXMHcBySpFQsvRm 18 | /daw41S+f4rv2XqyaLLbwmgPiusGlJMDENQQCmlPAoGASPA2Idc4gDeEJx+hTE8X 19 | P7QR2NVpj0JY0lHuesm4PfZ5znviv+gxH1Db+0iAVRwS1eNdQZTvbNjj2W52YGGR 20 | OFPPdpe/6PjBm73rNa+E7au2bqhlec0K1JO4myJ8LKQaDocPmdZNd3IExXwsIpeE 21 | QmKyYXIsz8hP38sPI+zWhSECgYBzJz2Ui/QEGOj2NV7T+/plBCIsnt4NJIh97aJd 22 | CNraTVNBdUvplbSqoOBlKxnoXCCZ3oD6V1lJWez2eEntX/w3iGdCOOCmQf3g6G1T 23 | 3aW+rL9d1fMK2Q1D1DHDCA8OEuLZQZhdHQTXKyya3vuU1gOL1Ep2FdTDWQzVgAFm 24 | MwBP5wKBgC7jFtvny07N3NQN/KX758LxN043Awx7DstraYj93jDQ8xT5naiXC/8m 25 | YoGB9aE6v+uxllBN5QdTSdzL0vpmkLYfnLbKSq54WVOflSAvL6VjxI9o9HMFqGF2 26 | wcEp3pm+ub20bZBdFoxP15X3IisUGiQcrg/q4ZGHxOvJuzI9q4b0 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/myRoot.srl: -------------------------------------------------------------------------------- 1 | 73771BE66A4800A9EBDEF9018EBEDCDB7701BCC8 2 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/server-cert.conf: -------------------------------------------------------------------------------- 1 | authorityKeyIdentifier=keyid,issuer 2 | basicConstraints=CA:FALSE 3 | keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyCertSign 4 | subjectAltName = @alt_names 5 | [alt_names] 6 | DNS.1 = test.example.private -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/server-cert.request.conf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = dn 3 | default_bits = 2048 4 | prompt = no 5 | default_md = sha256 6 | [dn] 7 | C=DE 8 | ST=BW 9 | L=Reutlingen 10 | O=Source Fellows GmbH 11 | emailAddress=webmaster@example.private 12 | CN = test.example.private -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/test.example.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDcDCCAlgCFHN3G+ZqSACp6975AY6+3Nt3AbzIMA0GCSqGSIb3DQEBCwUAMFIx 3 | CzAJBgNVBAYTAkRFMRAwDgYDVQQIDAdCYVfDg8K8MRMwEQYDVQQHDApSZXV0bGlu 4 | Z2VuMRwwGgYDVQQKDBNTb3VyY2UgRmVsbG93cyBHbWJIMB4XDTIwMDcwOTA0MTM0 5 | NloXDTMwMDcwNzA0MTM0NlowgZYxCzAJBgNVBAYTAkRFMQswCQYDVQQIDAJCVzET 6 | MBEGA1UEBwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZlbGxvd3MgR21i 7 | SDEoMCYGCSqGSIb3DQEJARYZd2VibWFzdGVyQGV4YW1wbGUucHJpdmF0ZTEdMBsG 8 | A1UEAwwUdGVzdC5leGFtcGxlLnByaXZhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IB 9 | DwAwggEKAoIBAQDEff9ZynNlYbqjTSl77iLejkEwkZuByBwW90CSNcU4GvYnBkiJ 10 | phUGCV0ifZzEbs+a+AFi9n4AUeA6MUHukEd9ccVSjhg3CQVuPgShyWW1m7XFuiFw 11 | ms0cePzkNviwDwQvHknS1DaEVDwhjciE0PmWn2CcRVf5ZoNi39EmrYotZBvovxQv 12 | RoOoP8LXRj4sZDY+5hrwheOoYbAiq0pZcNY7ajHH5CXt/GXKwNucZmBiJSCqusj8 13 | BQgJ1ob788pu1mqojs8QAhgcRf2jkM7MKTuO5tjOrHfdN2dlcDV9k8O+dg2FwIAF 14 | db0Nrmy71qHrdz1o7nsx0ktyuQmcHC8GfltNAgMBAAEwDQYJKoZIhvcNAQELBQAD 15 | ggEBAFC2bJh1AVWBdHA6Ig6GPW0pta14foNIQTPgjZfxRZZBaHS3cTr5Shl5Dhpi 16 | nU48hoAZNmNaSf7Y47+UhjlQHaRWs1kGyIdYFuyujn9CxGQXmzCG/VvOHh+elwws 17 | HV7cR6KRMozzOo4JVc61wKgnXOlaLOkY2T26aFhMfpeZhSmZ+vxRIe7uo2UEWLNg 18 | nRf7DOAcQCUEtyWB8jF+Du5nKg6FnQYn9S9qzwJSCDKvNaw+TaXK1kumcrxuF9s9 19 | HLxYDAnKfLIqsekoeELrbMU+r0Jhpbr3oHeUa4Mm+v2I2SleWNcYLfQmCyxklJ5r 20 | LrQLEceRlUveswrFdPYZYVQq2gc= 21 | -----END CERTIFICATE----- 22 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/test.example.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIIC3DCCAcQCAQAwgZYxCzAJBgNVBAYTAkRFMQswCQYDVQQIDAJCVzETMBEGA1UE 3 | BwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZlbGxvd3MgR21iSDEoMCYG 4 | CSqGSIb3DQEJARYZd2VibWFzdGVyQGV4YW1wbGUucHJpdmF0ZTEdMBsGA1UEAwwU 5 | dGVzdC5leGFtcGxlLnByaXZhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK 6 | AoIBAQDEff9ZynNlYbqjTSl77iLejkEwkZuByBwW90CSNcU4GvYnBkiJphUGCV0i 7 | fZzEbs+a+AFi9n4AUeA6MUHukEd9ccVSjhg3CQVuPgShyWW1m7XFuiFwms0cePzk 8 | NviwDwQvHknS1DaEVDwhjciE0PmWn2CcRVf5ZoNi39EmrYotZBvovxQvRoOoP8LX 9 | Rj4sZDY+5hrwheOoYbAiq0pZcNY7ajHH5CXt/GXKwNucZmBiJSCqusj8BQgJ1ob7 10 | 88pu1mqojs8QAhgcRf2jkM7MKTuO5tjOrHfdN2dlcDV9k8O+dg2FwIAFdb0Nrmy7 11 | 1qHrdz1o7nsx0ktyuQmcHC8GfltNAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEA 12 | l+IOqCs8V602w/GoWdxmLagVGUhSO7DOj6y3mPJirYCWwXCWWePkI9EFnW6ZW3O5 13 | uNCS2EkmzCOn/Py2M4R6h0+4a+eT+uZVY3gLhD4oN3uYPDoycoMHfkCYXXCNTJG1 14 | y3rWrvqrg7PjzEk7NOdG5dpGbdok9GVmkaXVjMn64J0PVzzyYZbM+1bxs9sq7Juj 15 | xzW/ZYioL/R+XBsUFeiwIiro1AlSUO1zClEMR5ePPGoEkbaMu6z1aojF51jEW+3M 16 | 208+uZnRgycuwpcLPdThagXGblAdypYzSB6vTKN9jndsy+/gMgp/K/Ie926trJEZ 17 | Bmogwc3Y5xduKsqkSxWNEw== 18 | -----END CERTIFICATE REQUEST----- 19 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/user-cert.conf: -------------------------------------------------------------------------------- 1 | authorityKeyIdentifier=keyid,issuer 2 | basicConstraints=CA:FALSE 3 | keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment 4 | default_days = 365 5 | [ca] 6 | default_ca = CA_default 7 | [ CA_default ] 8 | dir = ca 9 | certs = $dir/certsdb 10 | new_certs_dir = $certs 11 | database = $dir/index.txt 12 | certificate = $dir/cacert.pem 13 | private_key = $dir/private/cakey.pem 14 | serial = $dir/serial 15 | crldir = $dir/crl 16 | crlnumber = $dir/crlnumber 17 | crl = $crldir/crl.pem 18 | RANDFILE = $dir/private/.rand 19 | default_md = sha1 20 | policy = policy_match 21 | [ policy_match ] 22 | countryName = match 23 | stateOrProvinceName = match 24 | localityName = supplied 25 | organizationName = match 26 | organizationalUnitName = optional 27 | commonName = supplied 28 | emailAddress = optional -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/user-cert.request.conf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = dn 3 | default_bits = 2048 4 | prompt = no 5 | default_md = sha256 6 | [dn] 7 | C=DE 8 | ST=BW 9 | L=Reutlingen 10 | O=Source Fellows GmbH 11 | emailAddress=user@example.private 12 | CN = test.example.private -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/user-client.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/http-client-ssl-cert/certs/user-client.p12 -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/certs/user.req: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIIC1zCCAb8CAQAwgZExCzAJBgNVBAYTAkRFMQswCQYDVQQIDAJCVzETMBEGA1UE 3 | BwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZlbGxvd3MgR21iSDEjMCEG 4 | CSqGSIb3DQEJARYUdXNlckBleGFtcGxlLnByaXZhdGUxHTAbBgNVBAMMFHRlc3Qu 5 | ZXhhbXBsZS5wcml2YXRlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA 6 | 46m2xTUGbRLu5+5giUkqrLK+PffFTVUI/IBDqv0FbEOUpQLJH/xwxESFWrj5lfiK 7 | XC4SVUMj8tA0w6RBiRSp0RnUDdBYhzifbB2JLSNgj56v9Frw0KT9SkmbGd+XINuA 8 | e+7yuueatDxIdJxXYQtO7utTXco9nvPS1Rr3ejQdCXNEHK89MXKQ6UMLs8kwWCHf 9 | J1s5QoXx7NkciThkktba+SLB6bUivzoyEPnj5rNzHdJ+qaYnhoOMfp1POdBTRQv3 10 | iYvyjENEQqlKgYNgux9j9XX6czuFyWJ9HYphTDz5I6M9l3hvbRPbiHn7iQs+plC4 11 | cprI03vS7GwVPav3i8hQmwIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAJ2ZHvrq 12 | QE8yoiOSb+itUtWLo86T+qbSwHLy9Ned0wtu1XiL7ltJATyRwMbv1RduGcfYWBcx 13 | ZcUAcqYhsuSTM3MPg0MQvAFrLRwonq+TN6zq0K7RvZ5FSCizzcyThJIRcCtUNd0y 14 | heqknhsvnkIwSQCb4RPF3/57FXct1FNEHEKMtldWEHCkKbJnUtt9Z/4WEkmTCvzi 15 | EYwcwoK6QsmJOIwQXzOYniaPf2k8oeBLu81sq5GG5vRZvs9dFX6HQmfQ28oKeMCy 16 | 8n6XGHPudNybKBykvYzRKbChVl6LiFdz1f95x5JSh+aJx8Ca8mq5NpFK0TJEzQWv 17 | NUvFSnG7X4w6JJg= 18 | -----END CERTIFICATE REQUEST----- 19 | -------------------------------------------------------------------------------- /microservices/http-client-ssl-cert/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "crypto/tls" 5 | "crypto/x509" 6 | "fmt" 7 | "io/ioutil" 8 | "net/http" 9 | ) 10 | 11 | func main() { 12 | certPool := x509.NewCertPool() 13 | crt, _ := ioutil.ReadFile("./certs/myRoot.crt") 14 | certPool.AppendCertsFromPEM(crt) 15 | keyPair, _ := tls.LoadX509KeyPair("./certs/user.crt", "./certs/user.key") 16 | clientCerts := []tls.Certificate{keyPair} 17 | tlsConfig := &tls.Config{ 18 | RootCAs: certPool, 19 | Certificates: clientCerts, 20 | } 21 | transport := &http.Transport{ 22 | TLSClientConfig: tlsConfig, 23 | } 24 | client := http.Client{Transport: transport} 25 | res, err := client.Get("https://test.example.private:8443") 26 | if err != nil { 27 | panic(err) 28 | } 29 | bites, err := ioutil.ReadAll(res.Body) 30 | fmt.Printf("response is: %v", string(bites)) 31 | } 32 | -------------------------------------------------------------------------------- /microservices/http-client/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "net/http" 7 | ) 8 | 9 | func main() { 10 | res, err := http.Get("https://google.com") 11 | if err != nil { 12 | panic(err) 13 | } 14 | defer res.Body.Close() 15 | fmt.Println(res.Status) 16 | bites, _ := ioutil.ReadAll(res.Body) 17 | fmt.Println(string(bites)) 18 | } 19 | -------------------------------------------------------------------------------- /microservices/http-metod-matching/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "net/http" 4 | 5 | func myHandler(res http.ResponseWriter, req *http.Request) { 6 | if req.Method == http.MethodGet { 7 | res.Write([]byte("Hello World")) 8 | } else { 9 | res.WriteHeader(http.StatusMethodNotAllowed) 10 | } 11 | } 12 | 13 | func main() { 14 | http.HandleFunc("/", myHandler) 15 | http.ListenAndServe(":8080", nil) 16 | } 17 | -------------------------------------------------------------------------------- /microservices/http-ratelimit/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/ratelimit 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/go-redis/redis/v7 v7.4.0 7 | github.com/ulule/limiter/v3 v3.5.0 8 | ) 9 | -------------------------------------------------------------------------------- /microservices/http-ratelimit/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | 6 | libredis "github.com/go-redis/redis/v7" 7 | "github.com/ulule/limiter/v3" 8 | "github.com/ulule/limiter/v3/drivers/middleware/stdlib" 9 | "github.com/ulule/limiter/v3/drivers/store/redis" 10 | ) 11 | 12 | func handleRequest(res http.ResponseWriter, req *http.Request) { 13 | res.Write([]byte("Hello World!")) 14 | } 15 | 16 | func rateLimitMiddleWare(handler http.HandlerFunc) http.Handler { 17 | rate, err := limiter.NewRateFromFormatted("4-S") 18 | if err != nil { 19 | panic(err) 20 | } 21 | // Redis Client anlegen 22 | option, err := libredis.ParseURL("redis://localhost:6379/0") 23 | if err != nil { 24 | panic(err) 25 | } 26 | client := libredis.NewClient(option) 27 | // Store mit Redis konfigurieren 28 | store, err := redis.NewStoreWithOptions(client, limiter.StoreOptions{ 29 | Prefix: "limiter_http_example", 30 | MaxRetry: 3, 31 | }) 32 | if err != nil { 33 | panic(err) 34 | } 35 | middleware := stdlib.NewMiddleware(limiter.New(store, rate, limiter.WithTrustForwardHeader(true))) 36 | return middleware.Handler(handler) 37 | } 38 | 39 | func main() { 40 | http.Handle("/", rateLimitMiddleWare(http.HandlerFunc(handleRequest))) 41 | http.ListenAndServe(":8080", nil) 42 | } 43 | -------------------------------------------------------------------------------- /microservices/http-registration/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/httpregistration 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /microservices/http-registration/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "net/http" 4 | 5 | type MyHandler struct{} 6 | 7 | func (mh *MyHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) { 8 | writer.Write([]byte("Hello World (from ServeHTTP)")) 9 | } 10 | 11 | func handleHttp(res http.ResponseWriter, req *http.Request) { 12 | res.Write([]byte("Hello World (from handleHttp)")) 13 | } 14 | 15 | func main() { 16 | //Anmeldung der HandlerFunc Implementierung 17 | http.HandleFunc("/func", handleHttp) 18 | //Anmeldung der Handler Interface Implementierung 19 | http.Handle("/handler", &MyHandler{}) 20 | http.ListenAndServe(":8080", &MyHandler{}) 21 | } 22 | -------------------------------------------------------------------------------- /microservices/http-retry/client/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/rfc7808/server 2 | 3 | go 1.13 4 | 5 | require github.com/hashicorp/go-retryablehttp v0.6.6 6 | -------------------------------------------------------------------------------- /microservices/http-retry/client/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM= 3 | github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= 4 | github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= 5 | github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM= 6 | github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= 7 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 8 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 9 | -------------------------------------------------------------------------------- /microservices/http-retry/client/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/hashicorp/go-retryablehttp" 7 | ) 8 | 9 | func main() { 10 | retryClient := retryablehttp.NewClient() 11 | retryClient.RetryMax = 10 12 | 13 | standardClient := retryClient.StandardClient() 14 | 15 | res, err := standardClient.Get("http://localhost:8080/") 16 | if err != nil { 17 | log.Fatal("could not request") 18 | } 19 | defer res.Body.Close() 20 | log.Println(res.Status) 21 | } 22 | -------------------------------------------------------------------------------- /microservices/http-retry/server/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/httpretry/server 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /microservices/http-retry/server/go.sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/http-retry/server/go.sum -------------------------------------------------------------------------------- /microservices/http-retry/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | ) 7 | 8 | var count int = 0 9 | 10 | func handleHttp(res http.ResponseWriter, req *http.Request) { 11 | 12 | responseStatus := http.StatusInternalServerError 13 | if count%4 == 0 { 14 | responseStatus = http.StatusOK 15 | } 16 | fmt.Printf("will return %v\n", responseStatus) 17 | res.WriteHeader(responseStatus) 18 | 19 | count++ 20 | 21 | } 22 | 23 | func main() { 24 | http.HandleFunc("/", handleHttp) 25 | http.ListenAndServe(":8080", nil) 26 | } 27 | -------------------------------------------------------------------------------- /microservices/httpsserver/README.md: -------------------------------------------------------------------------------- 1 | Zertifikate erstellen 2 | ===================== 3 | 4 | - Root Zertifikat erstellen: 5 | ``` 6 | openssl genrsa -out myRoot.key 2048 7 | openssl req -x509 -new -nodes -key myRoot.key -sha256 -days 3650 -out myRoot.crt 8 | ``` 9 | 10 | - Zertifikat für Server erstellen: 11 | ``` 12 | #CSR erstellen 13 | openssl req -new -sha256 -nodes -out test.example.csr -newkey rsa:2048 -keyout test.example.key -config server-cert.request.conf 14 | 15 | openssl x509 -req -in test.example.csr -CA myRoot.crt -CAkey myRoot.key -CAcreateserial -extfile test.example.ext.cnf -out test.example.crt -days 3650 -sha256 16 | ``` -------------------------------------------------------------------------------- /microservices/httpsserver/certs/myRoot.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDhTCCAm2gAwIBAgIUS7TqIYrkdb2kEmtuawe8h/54VVIwDQYJKoZIhvcNAQEL 3 | BQAwUjELMAkGA1UEBhMCREUxEDAOBgNVBAgMB0JhV8ODwrwxEzARBgNVBAcMClJl 4 | dXRsaW5nZW4xHDAaBgNVBAoME1NvdXJjZSBGZWxsb3dzIEdtYkgwHhcNMjAwNzA4 5 | MDM1ODE4WhcNMzAwNzA2MDM1ODE4WjBSMQswCQYDVQQGEwJERTEQMA4GA1UECAwH 6 | QmFXw4PCvDETMBEGA1UEBwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZl 7 | bGxvd3MgR21iSDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANY+x2Kr 8 | U49506heEh2s0Q5vTjXUbP5814pHhlJ43X3p7QYMbJ/BBunxLA3a8GvuukgD1Wxr 9 | 0T0uHo5/MLxRh6r1TEy80IApUsyCcAO3jdTGtIr4iebnAOL/Dw0TGWDRyl5SZCwo 10 | BsVSOfCoJyMs+ygB+BLE+05Dq5FwA3CpxP5r67efBpY2Y9bu1iXAC179rGaVvtLM 11 | 3Zy5G+tQ0aMJYOqVR6tvSrrYg44gJiM270u6Os80V723fPtEUvKbHQOpZoleHWsb 12 | uZ1FbdQBcnJ2u3bI93ewQAvqQ3tjOqgell5n7kJ1B3Vjk8bVzda1zPKH9vcvF0fc 13 | gXdPrVOzBCa5Su8CAwEAAaNTMFEwHQYDVR0OBBYEFO3om6AZ9q/+r6kXo7hAhOBD 14 | SeTbMB8GA1UdIwQYMBaAFO3om6AZ9q/+r6kXo7hAhOBDSeTbMA8GA1UdEwEB/wQF 15 | MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAMxKFUWYTQz/ACVY8lV18QCGxcvNj3Uz 16 | ZdtNUEDkxu6yKsl+1otREbuxqnfNjrGLmlLQfU/GlY+PCF85SYm1Ep4yKdZ7J+1c 17 | MG+PB5NS3cV7BnoJ6WnXk7KJRX+dYpCr+jQmQbtTshk6dM2a6THjyPzKkyDflcfM 18 | tXQROGDG9O/5DvF8Eqz13Gel5Wukept9mHtDHapU0BooFTXNqh+tPmFU76tKCQZV 19 | h0Qt9BC8A6bDEaULVlXpvNTuDCrCMGhiYEo2++PD/e5YT1UDVNSokanyFIUoCJAn 20 | X5u+VjRfaVibiWu8E63qoKv+oXBf9pwlNeE8KBlydzf5OQgx8FEkbfc= 21 | -----END CERTIFICATE----- 22 | -------------------------------------------------------------------------------- /microservices/httpsserver/certs/myRoot.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEowIBAAKCAQEA1j7HYqtTj3nTqF4SHazRDm9ONdRs/nzXikeGUnjdfentBgxs 3 | n8EG6fEsDdrwa+66SAPVbGvRPS4ejn8wvFGHqvVMTLzQgClSzIJwA7eN1Ma0iviJ 4 | 5ucA4v8PDRMZYNHKXlJkLCgGxVI58KgnIyz7KAH4EsT7TkOrkXADcKnE/mvrt58G 5 | ljZj1u7WJcALXv2sZpW+0szdnLkb61DRowlg6pVHq29KutiDjiAmIzbvS7o6zzRX 6 | vbd8+0RS8psdA6lmiV4daxu5nUVt1AFycna7dsj3d7BAC+pDe2M6qB6WXmfuQnUH 7 | dWOTxtXN1rXM8of29y8XR9yBd0+tU7MEJrlK7wIDAQABAoIBAQC0GzaWthybAvly 8 | ExL7q2pbzipbimbOUbkI4DQFZOIelebSLOOsMSNUy5ATXbi7o4cqPz7N1J2j5v99 9 | H2OjDI8WovwvjpNhClw23Hi6RzaOZzUjluinGg9exLisbx/oirSrMmxPyp5Wh1d5 10 | /oJYSdohufYIIVx6YNz8eMQLlEys7BcAs9uDbfrEh2lhSCRd+eVER/p2U+EE3YFe 11 | OPhE7S7/5fDHidA/vU5YL0zyDcTiqec8lF3b63L39zYsckUjvbJ9gXru+RUFsnYT 12 | FCz/8axvSyiRj9M162BtrwV1lANgbck+unmk9j63bMaNUFHdlFxovxZDA4C6napJ 13 | +5dnwduBAoGBAO3UqDRxSWvelTv5vSFWaiJoFKU23PGX46Fsy+b8ttj+8Jz94333 14 | u+1t5fEnpS46QhQtiVRB42pezBncJ+CdjQ92ikcJZL7o8NYttlmTVg2i/v+dnxmL 15 | T+Tgl89VUOgfKdlRwpgG64dMT1HFQJlMKkKMzdS6FxQtSclYSq0yYVxhAoGBAOac 16 | 2ijcy0xlhDinsmikxBveaxl8pTeF0jmLlXmNRTuSXkk9tXi5yurV+e4M9oG7pvYU 17 | 4LUthIvtbmPgDhANN5+45bJqNWw3ovaLb0/60XsET4fiR8W0muXMHcBySpFQsvRm 18 | /daw41S+f4rv2XqyaLLbwmgPiusGlJMDENQQCmlPAoGASPA2Idc4gDeEJx+hTE8X 19 | P7QR2NVpj0JY0lHuesm4PfZ5znviv+gxH1Db+0iAVRwS1eNdQZTvbNjj2W52YGGR 20 | OFPPdpe/6PjBm73rNa+E7au2bqhlec0K1JO4myJ8LKQaDocPmdZNd3IExXwsIpeE 21 | QmKyYXIsz8hP38sPI+zWhSECgYBzJz2Ui/QEGOj2NV7T+/plBCIsnt4NJIh97aJd 22 | CNraTVNBdUvplbSqoOBlKxnoXCCZ3oD6V1lJWez2eEntX/w3iGdCOOCmQf3g6G1T 23 | 3aW+rL9d1fMK2Q1D1DHDCA8OEuLZQZhdHQTXKyya3vuU1gOL1Ep2FdTDWQzVgAFm 24 | MwBP5wKBgC7jFtvny07N3NQN/KX758LxN043Awx7DstraYj93jDQ8xT5naiXC/8m 25 | YoGB9aE6v+uxllBN5QdTSdzL0vpmkLYfnLbKSq54WVOflSAvL6VjxI9o9HMFqGF2 26 | wcEp3pm+ub20bZBdFoxP15X3IisUGiQcrg/q4ZGHxOvJuzI9q4b0 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /microservices/httpsserver/certs/myRoot.srl: -------------------------------------------------------------------------------- 1 | 73771BE66A4800A9EBDEF9018EBEDCDB7701BCC7 2 | -------------------------------------------------------------------------------- /microservices/httpsserver/certs/server-cert.conf: -------------------------------------------------------------------------------- 1 | authorityKeyIdentifier=keyid,issuer 2 | basicConstraints=CA:FALSE 3 | keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment 4 | subjectAltName = @alt_names 5 | [alt_names] 6 | DNS.1 = test.example.private -------------------------------------------------------------------------------- /microservices/httpsserver/certs/server-cert.request.conf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = dn 3 | default_bits = 2048 4 | prompt = no 5 | default_md = sha256 6 | [dn] 7 | C=DE 8 | ST=BW 9 | L=Reutlingen 10 | O=Source Fellows GmbH 11 | emailAddress=webmaster@example.private 12 | CN = test.example.private -------------------------------------------------------------------------------- /microservices/httpsserver/certs/test.example.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIID0zCCArugAwIBAgIUc3cb5mpIAKnr3vkBjr7c23cBvMcwDQYJKoZIhvcNAQEL 3 | BQAwUjELMAkGA1UEBhMCREUxEDAOBgNVBAgMB0JhV8ODwrwxEzARBgNVBAcMClJl 4 | dXRsaW5nZW4xHDAaBgNVBAoME1NvdXJjZSBGZWxsb3dzIEdtYkgwHhcNMjAwNzA4 5 | MDQwNzA0WhcNMzAwNzA2MDQwNzA0WjCBljELMAkGA1UEBhMCREUxCzAJBgNVBAgM 6 | AkJXMRMwEQYDVQQHDApSZXV0bGluZ2VuMRwwGgYDVQQKDBNTb3VyY2UgRmVsbG93 7 | cyBHbWJIMSgwJgYJKoZIhvcNAQkBFhl3ZWJtYXN0ZXJAZXhhbXBsZS5wcml2YXRl 8 | MR0wGwYDVQQDDBR0ZXN0LmV4YW1wbGUucHJpdmF0ZTCCASIwDQYJKoZIhvcNAQEB 9 | BQADggEPADCCAQoCggEBAK46AzTKQy+/SFeseHbRZbuQZnYznIjiWW57A2dGCSg2 10 | tuI0811lV5Fx7MMlPEdvHOycwg678hMDvwLv5GvZQg06Pz/rvRxSls6g+S0dPAGE 11 | NPgNaWDqVa8G58FVdzfe1NFRYLvf/oaFXkE6WJvdgVHuZoNj1IrjqBQMoFRNBeub 12 | JJHNEJZhX5KdMWqFxJBM4jDzXt1PUIFgeFbtOHnnK6/1ga6yXvtvusS/1tZYZpNp 13 | 1czr5l2cw0VXAcgVDu3qQIOtvYtCkUZO57436UNHKE3uT3gLzzAKRxfrS8LbVG2H 14 | V82dT5or77bPT2N/GyPJwWgzi50yIfX7vpRmTPHipt0CAwEAAaNcMFowHwYDVR0j 15 | BBgwFoAU7eiboBn2r/6vqRejuECE4ENJ5NswCQYDVR0TBAIwADALBgNVHQ8EBAMC 16 | BPAwHwYDVR0RBBgwFoIUdGVzdC5leGFtcGxlLnByaXZhdGUwDQYJKoZIhvcNAQEL 17 | BQADggEBAFYzh53rfQmEw//rCCeZCjWnyyHuW52DdEYLOJVmT3rkogmIkuFuw7n4 18 | KNEqw1eDwNCYMFV0uemiWpI80qvtQO8RvaSoWlJs/hTYVI/BIL0xlR8/tOUhshep 19 | qwAv9LJISd5RcmTJ6Ag3ytR1f6skBuzHdUbODTZBh/L+tM/ypHF+kRnqqjDkEC3r 20 | kMb81hoU6xALpjwqCzRawbEC3OIw2QQUqVB+zsW45hnhhHKVrLkkyO9djSoReHOy 21 | lrz7liWcZBlTUjuH+M3WCb8Qr9FD1Cz1f2Ejq0pR6WLTL3c/tcWpZiNUpFYGnscF 22 | fLyZekegMNG1PqawmEgxBfTAxrONwbo= 23 | -----END CERTIFICATE----- 24 | -------------------------------------------------------------------------------- /microservices/httpsserver/certs/test.example.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIIC3DCCAcQCAQAwgZYxCzAJBgNVBAYTAkRFMQswCQYDVQQIDAJCVzETMBEGA1UE 3 | BwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZlbGxvd3MgR21iSDEoMCYG 4 | CSqGSIb3DQEJARYZd2VibWFzdGVyQGV4YW1wbGUucHJpdmF0ZTEdMBsGA1UEAwwU 5 | dGVzdC5leGFtcGxlLnByaXZhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK 6 | AoIBAQCuOgM0ykMvv0hXrHh20WW7kGZ2M5yI4lluewNnRgkoNrbiNPNdZVeRcezD 7 | JTxHbxzsnMIOu/ITA78C7+Rr2UINOj8/670cUpbOoPktHTwBhDT4DWlg6lWvBufB 8 | VXc33tTRUWC73/6GhV5BOlib3YFR7maDY9SK46gUDKBUTQXrmySRzRCWYV+SnTFq 9 | hcSQTOIw817dT1CBYHhW7Th55yuv9YGusl77b7rEv9bWWGaTadXM6+ZdnMNFVwHI 10 | FQ7t6kCDrb2LQpFGTue+N+lDRyhN7k94C88wCkcX60vC21Rth1fNnU+aK++2z09j 11 | fxsjycFoM4udMiH1+76UZkzx4qbdAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEA 12 | Uz43p2rJGBNxao94QiKV+AsGoHJeogT8ghJqAsyMrpLTs0ETmVEdtmwO+cujpYOd 13 | bWzJ4WPhemyUbyV4/Igx7U43A0OkAN1xmLHXkuDAZa3Ox43lP1027A193Ik/eAdk 14 | IhZixL/MJUaub7nTC6uSOq73wo4TcQku9cZUvs9+9t6yOjeMAfNXsyfshXq3hlxd 15 | aRqbydeSuAn1TYBB8fNWZmLCRaAQ88SHTa3WA3Sapc+JAxbLlz7UWpMcn5Ua4jsD 16 | vgnhXHhUcaGSB2bGUPxolSj1al7I3q8HrzgCSSHnoBBl3LJ0jpkd56+0VHJ351+4 17 | cziMZngB/7t5LDDKr7q/Yg== 18 | -----END CERTIFICATE REQUEST----- 19 | -------------------------------------------------------------------------------- /microservices/httpsserver/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/https/server 2 | 3 | go 1.14 4 | 5 | require golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 6 | -------------------------------------------------------------------------------- /microservices/httpsserver/go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 2 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= 3 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 4 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= 5 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 6 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 7 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 8 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 9 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 10 | -------------------------------------------------------------------------------- /microservices/httpsserver/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | ) 7 | 8 | func main() { 9 | 10 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 11 | fmt.Fprintln(w, "Hello World") 12 | }) 13 | 14 | certFile := "./certs/test.example.crt" 15 | keyFile := "./certs/test.example.key" 16 | 17 | http.ListenAndServeTLS(":8443", certFile, keyFile, nil) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /microservices/httpsservercert/README.md: -------------------------------------------------------------------------------- 1 | Zertifikate erstellen 2 | ===================== 3 | 4 | AUSFÜHRE IM cert VERZEICHNIS 5 | 6 | - Root Zertifikat erstellen: 7 | ``` 8 | openssl genrsa -out myRoot.key 2048 9 | openssl req -x509 -new -nodes -key myRoot.key -sha256 -days 3650 -out myRoot.crt 10 | ``` 11 | 12 | - Zertifikat für Server erstellen: 13 | ``` 14 | #CSR erstellen 15 | openssl req -new -sha256 -nodes -out test.example.csr -newkey rsa:2048 -keyout test.example.key -config server-cert.request.conf 16 | 17 | openssl x509 -req -in test.example.csr -CA myRoot.crt -CAkey myRoot.key -CAcreateserial -out test.example.crt -days 3650 -sha256 18 | ``` 19 | 20 | -CA anlegen 21 | ``` 22 | mkdir -p ca/certsdb 23 | touch ca/index.txt 24 | touch ca/index.txt.attr 25 | echo '01' > ca/serial 26 | ``` 27 | 28 | - Client-Zertifikat erstellen 29 | 30 | ``` 31 | openssl genrsa -des3 -out user.key # key is 1234 32 | openssl req -new -key user.key -out user.req -config user-cert.request.conf 33 | openssl ca -cert test.example.crt -keyfile test.example.key -out user.crt -in user.req -config user-cert.conf 34 | openssl pkcs12 -export -in user.crt -inkey user.key -out user-client.p12 35 | ``` 36 | 37 | - Chain 38 | 39 | ``` 40 | cat 41 | ``` 42 | 43 | - Curl 44 | ``` 45 | curl --cacert ./certs/myRoot.crt --cert ./certs/user.crt --key ./certs/user.key -v https://test.example.private:8443 46 | ``` -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/ca/index.txt: -------------------------------------------------------------------------------- 1 | V 210709041547Z 01 unknown /C=DE/ST=BW/L=Reutlingen/O=Source Fellows GmbH/CN=test.example.private/emailAddress=user@example.private 2 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/ca/index.txt.attr: -------------------------------------------------------------------------------- 1 | unique_subject = yes 2 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/ca/index.txt.attr.old: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/httpsservercert/certs/ca/index.txt.attr.old -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/ca/index.txt.old: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/httpsservercert/certs/ca/index.txt.old -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/ca/serial: -------------------------------------------------------------------------------- 1 | 02 2 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/ca/serial.old: -------------------------------------------------------------------------------- 1 | 01 2 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/myRoot.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDhTCCAm2gAwIBAgIUS7TqIYrkdb2kEmtuawe8h/54VVIwDQYJKoZIhvcNAQEL 3 | BQAwUjELMAkGA1UEBhMCREUxEDAOBgNVBAgMB0JhV8ODwrwxEzARBgNVBAcMClJl 4 | dXRsaW5nZW4xHDAaBgNVBAoME1NvdXJjZSBGZWxsb3dzIEdtYkgwHhcNMjAwNzA4 5 | MDM1ODE4WhcNMzAwNzA2MDM1ODE4WjBSMQswCQYDVQQGEwJERTEQMA4GA1UECAwH 6 | QmFXw4PCvDETMBEGA1UEBwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZl 7 | bGxvd3MgR21iSDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANY+x2Kr 8 | U49506heEh2s0Q5vTjXUbP5814pHhlJ43X3p7QYMbJ/BBunxLA3a8GvuukgD1Wxr 9 | 0T0uHo5/MLxRh6r1TEy80IApUsyCcAO3jdTGtIr4iebnAOL/Dw0TGWDRyl5SZCwo 10 | BsVSOfCoJyMs+ygB+BLE+05Dq5FwA3CpxP5r67efBpY2Y9bu1iXAC179rGaVvtLM 11 | 3Zy5G+tQ0aMJYOqVR6tvSrrYg44gJiM270u6Os80V723fPtEUvKbHQOpZoleHWsb 12 | uZ1FbdQBcnJ2u3bI93ewQAvqQ3tjOqgell5n7kJ1B3Vjk8bVzda1zPKH9vcvF0fc 13 | gXdPrVOzBCa5Su8CAwEAAaNTMFEwHQYDVR0OBBYEFO3om6AZ9q/+r6kXo7hAhOBD 14 | SeTbMB8GA1UdIwQYMBaAFO3om6AZ9q/+r6kXo7hAhOBDSeTbMA8GA1UdEwEB/wQF 15 | MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAMxKFUWYTQz/ACVY8lV18QCGxcvNj3Uz 16 | ZdtNUEDkxu6yKsl+1otREbuxqnfNjrGLmlLQfU/GlY+PCF85SYm1Ep4yKdZ7J+1c 17 | MG+PB5NS3cV7BnoJ6WnXk7KJRX+dYpCr+jQmQbtTshk6dM2a6THjyPzKkyDflcfM 18 | tXQROGDG9O/5DvF8Eqz13Gel5Wukept9mHtDHapU0BooFTXNqh+tPmFU76tKCQZV 19 | h0Qt9BC8A6bDEaULVlXpvNTuDCrCMGhiYEo2++PD/e5YT1UDVNSokanyFIUoCJAn 20 | X5u+VjRfaVibiWu8E63qoKv+oXBf9pwlNeE8KBlydzf5OQgx8FEkbfc= 21 | -----END CERTIFICATE----- 22 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/myRoot.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEowIBAAKCAQEA1j7HYqtTj3nTqF4SHazRDm9ONdRs/nzXikeGUnjdfentBgxs 3 | n8EG6fEsDdrwa+66SAPVbGvRPS4ejn8wvFGHqvVMTLzQgClSzIJwA7eN1Ma0iviJ 4 | 5ucA4v8PDRMZYNHKXlJkLCgGxVI58KgnIyz7KAH4EsT7TkOrkXADcKnE/mvrt58G 5 | ljZj1u7WJcALXv2sZpW+0szdnLkb61DRowlg6pVHq29KutiDjiAmIzbvS7o6zzRX 6 | vbd8+0RS8psdA6lmiV4daxu5nUVt1AFycna7dsj3d7BAC+pDe2M6qB6WXmfuQnUH 7 | dWOTxtXN1rXM8of29y8XR9yBd0+tU7MEJrlK7wIDAQABAoIBAQC0GzaWthybAvly 8 | ExL7q2pbzipbimbOUbkI4DQFZOIelebSLOOsMSNUy5ATXbi7o4cqPz7N1J2j5v99 9 | H2OjDI8WovwvjpNhClw23Hi6RzaOZzUjluinGg9exLisbx/oirSrMmxPyp5Wh1d5 10 | /oJYSdohufYIIVx6YNz8eMQLlEys7BcAs9uDbfrEh2lhSCRd+eVER/p2U+EE3YFe 11 | OPhE7S7/5fDHidA/vU5YL0zyDcTiqec8lF3b63L39zYsckUjvbJ9gXru+RUFsnYT 12 | FCz/8axvSyiRj9M162BtrwV1lANgbck+unmk9j63bMaNUFHdlFxovxZDA4C6napJ 13 | +5dnwduBAoGBAO3UqDRxSWvelTv5vSFWaiJoFKU23PGX46Fsy+b8ttj+8Jz94333 14 | u+1t5fEnpS46QhQtiVRB42pezBncJ+CdjQ92ikcJZL7o8NYttlmTVg2i/v+dnxmL 15 | T+Tgl89VUOgfKdlRwpgG64dMT1HFQJlMKkKMzdS6FxQtSclYSq0yYVxhAoGBAOac 16 | 2ijcy0xlhDinsmikxBveaxl8pTeF0jmLlXmNRTuSXkk9tXi5yurV+e4M9oG7pvYU 17 | 4LUthIvtbmPgDhANN5+45bJqNWw3ovaLb0/60XsET4fiR8W0muXMHcBySpFQsvRm 18 | /daw41S+f4rv2XqyaLLbwmgPiusGlJMDENQQCmlPAoGASPA2Idc4gDeEJx+hTE8X 19 | P7QR2NVpj0JY0lHuesm4PfZ5znviv+gxH1Db+0iAVRwS1eNdQZTvbNjj2W52YGGR 20 | OFPPdpe/6PjBm73rNa+E7au2bqhlec0K1JO4myJ8LKQaDocPmdZNd3IExXwsIpeE 21 | QmKyYXIsz8hP38sPI+zWhSECgYBzJz2Ui/QEGOj2NV7T+/plBCIsnt4NJIh97aJd 22 | CNraTVNBdUvplbSqoOBlKxnoXCCZ3oD6V1lJWez2eEntX/w3iGdCOOCmQf3g6G1T 23 | 3aW+rL9d1fMK2Q1D1DHDCA8OEuLZQZhdHQTXKyya3vuU1gOL1Ep2FdTDWQzVgAFm 24 | MwBP5wKBgC7jFtvny07N3NQN/KX758LxN043Awx7DstraYj93jDQ8xT5naiXC/8m 25 | YoGB9aE6v+uxllBN5QdTSdzL0vpmkLYfnLbKSq54WVOflSAvL6VjxI9o9HMFqGF2 26 | wcEp3pm+ub20bZBdFoxP15X3IisUGiQcrg/q4ZGHxOvJuzI9q4b0 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/myRoot.srl: -------------------------------------------------------------------------------- 1 | 73771BE66A4800A9EBDEF9018EBEDCDB7701BCC8 2 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/server-cert.conf: -------------------------------------------------------------------------------- 1 | authorityKeyIdentifier=keyid,issuer 2 | basicConstraints=CA:FALSE 3 | keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyCertSign 4 | subjectAltName = @alt_names 5 | [alt_names] 6 | DNS.1 = test.example.private -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/server-cert.request.conf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = dn 3 | default_bits = 2048 4 | prompt = no 5 | default_md = sha256 6 | [dn] 7 | C=DE 8 | ST=BW 9 | L=Reutlingen 10 | O=Source Fellows GmbH 11 | emailAddress=webmaster@example.private 12 | CN = test.example.private -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/test.example.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDcDCCAlgCFHN3G+ZqSACp6975AY6+3Nt3AbzIMA0GCSqGSIb3DQEBCwUAMFIx 3 | CzAJBgNVBAYTAkRFMRAwDgYDVQQIDAdCYVfDg8K8MRMwEQYDVQQHDApSZXV0bGlu 4 | Z2VuMRwwGgYDVQQKDBNTb3VyY2UgRmVsbG93cyBHbWJIMB4XDTIwMDcwOTA0MTM0 5 | NloXDTMwMDcwNzA0MTM0NlowgZYxCzAJBgNVBAYTAkRFMQswCQYDVQQIDAJCVzET 6 | MBEGA1UEBwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZlbGxvd3MgR21i 7 | SDEoMCYGCSqGSIb3DQEJARYZd2VibWFzdGVyQGV4YW1wbGUucHJpdmF0ZTEdMBsG 8 | A1UEAwwUdGVzdC5leGFtcGxlLnByaXZhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IB 9 | DwAwggEKAoIBAQDEff9ZynNlYbqjTSl77iLejkEwkZuByBwW90CSNcU4GvYnBkiJ 10 | phUGCV0ifZzEbs+a+AFi9n4AUeA6MUHukEd9ccVSjhg3CQVuPgShyWW1m7XFuiFw 11 | ms0cePzkNviwDwQvHknS1DaEVDwhjciE0PmWn2CcRVf5ZoNi39EmrYotZBvovxQv 12 | RoOoP8LXRj4sZDY+5hrwheOoYbAiq0pZcNY7ajHH5CXt/GXKwNucZmBiJSCqusj8 13 | BQgJ1ob788pu1mqojs8QAhgcRf2jkM7MKTuO5tjOrHfdN2dlcDV9k8O+dg2FwIAF 14 | db0Nrmy71qHrdz1o7nsx0ktyuQmcHC8GfltNAgMBAAEwDQYJKoZIhvcNAQELBQAD 15 | ggEBAFC2bJh1AVWBdHA6Ig6GPW0pta14foNIQTPgjZfxRZZBaHS3cTr5Shl5Dhpi 16 | nU48hoAZNmNaSf7Y47+UhjlQHaRWs1kGyIdYFuyujn9CxGQXmzCG/VvOHh+elwws 17 | HV7cR6KRMozzOo4JVc61wKgnXOlaLOkY2T26aFhMfpeZhSmZ+vxRIe7uo2UEWLNg 18 | nRf7DOAcQCUEtyWB8jF+Du5nKg6FnQYn9S9qzwJSCDKvNaw+TaXK1kumcrxuF9s9 19 | HLxYDAnKfLIqsekoeELrbMU+r0Jhpbr3oHeUa4Mm+v2I2SleWNcYLfQmCyxklJ5r 20 | LrQLEceRlUveswrFdPYZYVQq2gc= 21 | -----END CERTIFICATE----- 22 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/test.example.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIIC3DCCAcQCAQAwgZYxCzAJBgNVBAYTAkRFMQswCQYDVQQIDAJCVzETMBEGA1UE 3 | BwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZlbGxvd3MgR21iSDEoMCYG 4 | CSqGSIb3DQEJARYZd2VibWFzdGVyQGV4YW1wbGUucHJpdmF0ZTEdMBsGA1UEAwwU 5 | dGVzdC5leGFtcGxlLnByaXZhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK 6 | AoIBAQDEff9ZynNlYbqjTSl77iLejkEwkZuByBwW90CSNcU4GvYnBkiJphUGCV0i 7 | fZzEbs+a+AFi9n4AUeA6MUHukEd9ccVSjhg3CQVuPgShyWW1m7XFuiFwms0cePzk 8 | NviwDwQvHknS1DaEVDwhjciE0PmWn2CcRVf5ZoNi39EmrYotZBvovxQvRoOoP8LX 9 | Rj4sZDY+5hrwheOoYbAiq0pZcNY7ajHH5CXt/GXKwNucZmBiJSCqusj8BQgJ1ob7 10 | 88pu1mqojs8QAhgcRf2jkM7MKTuO5tjOrHfdN2dlcDV9k8O+dg2FwIAFdb0Nrmy7 11 | 1qHrdz1o7nsx0ktyuQmcHC8GfltNAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAQEA 12 | l+IOqCs8V602w/GoWdxmLagVGUhSO7DOj6y3mPJirYCWwXCWWePkI9EFnW6ZW3O5 13 | uNCS2EkmzCOn/Py2M4R6h0+4a+eT+uZVY3gLhD4oN3uYPDoycoMHfkCYXXCNTJG1 14 | y3rWrvqrg7PjzEk7NOdG5dpGbdok9GVmkaXVjMn64J0PVzzyYZbM+1bxs9sq7Juj 15 | xzW/ZYioL/R+XBsUFeiwIiro1AlSUO1zClEMR5ePPGoEkbaMu6z1aojF51jEW+3M 16 | 208+uZnRgycuwpcLPdThagXGblAdypYzSB6vTKN9jndsy+/gMgp/K/Ie926trJEZ 17 | Bmogwc3Y5xduKsqkSxWNEw== 18 | -----END CERTIFICATE REQUEST----- 19 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/user-cert.conf: -------------------------------------------------------------------------------- 1 | authorityKeyIdentifier=keyid,issuer 2 | basicConstraints=CA:FALSE 3 | keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment 4 | default_days = 365 5 | [ca] 6 | default_ca = CA_default 7 | [ CA_default ] 8 | dir = ca 9 | certs = $dir/certsdb 10 | new_certs_dir = $certs 11 | database = $dir/index.txt 12 | certificate = $dir/cacert.pem 13 | private_key = $dir/private/cakey.pem 14 | serial = $dir/serial 15 | crldir = $dir/crl 16 | crlnumber = $dir/crlnumber 17 | crl = $crldir/crl.pem 18 | RANDFILE = $dir/private/.rand 19 | default_md = sha1 20 | policy = policy_match 21 | [ policy_match ] 22 | countryName = match 23 | stateOrProvinceName = match 24 | localityName = supplied 25 | organizationName = match 26 | organizationalUnitName = optional 27 | commonName = supplied 28 | emailAddress = optional -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/user-cert.request.conf: -------------------------------------------------------------------------------- 1 | [req] 2 | distinguished_name = dn 3 | default_bits = 2048 4 | prompt = no 5 | default_md = sha256 6 | [dn] 7 | C=DE 8 | ST=BW 9 | L=Reutlingen 10 | O=Source Fellows GmbH 11 | emailAddress=user@example.private 12 | CN = test.example.private -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/user-client.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceFellows/gobuch/f0712cdc6475798160e23cf3ab4986dbeb51baaf/microservices/httpsservercert/certs/user-client.p12 -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/user.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpAIBAAKCAQEA46m2xTUGbRLu5+5giUkqrLK+PffFTVUI/IBDqv0FbEOUpQLJ 3 | H/xwxESFWrj5lfiKXC4SVUMj8tA0w6RBiRSp0RnUDdBYhzifbB2JLSNgj56v9Frw 4 | 0KT9SkmbGd+XINuAe+7yuueatDxIdJxXYQtO7utTXco9nvPS1Rr3ejQdCXNEHK89 5 | MXKQ6UMLs8kwWCHfJ1s5QoXx7NkciThkktba+SLB6bUivzoyEPnj5rNzHdJ+qaYn 6 | hoOMfp1POdBTRQv3iYvyjENEQqlKgYNgux9j9XX6czuFyWJ9HYphTDz5I6M9l3hv 7 | bRPbiHn7iQs+plC4cprI03vS7GwVPav3i8hQmwIDAQABAoIBAQDjJvzT+7ozroZt 8 | J61ujXBk69thnQxhwoy604ew3dBBW5S+p+7hWVFyoyOjh5iQCHKU0us740fFB/T9 9 | l7BEBDgECI9KUS1jjh2TFE1AmmHIKv9JAkvxAg4vxw1YXX0/LYQfJCVQU7JKTzPE 10 | FhIhI9VYEbkWhanCJWmhc8ylQ8YNG/FU9DYl2qR4GpgEEmyX3EErG/30PBEKRVBl 11 | k51/cz8vT3fN/bzXjX6+wkG41/+71o879Jh47wI9WY38ZUtqIGWZZaenCNubhHQx 12 | /5dvFvidwkeueXmwgssBKLdsP1cBxpu/7eN4PsiY+n7RDPVgcnE/PQvTindDOA5w 13 | vFJ2gZQxAoGBAPgMK2ZV23zMb0JxSfk+pxT2FJZP1MKI0IkdgB6c+rJWEnjV857q 14 | KeHf37uSEx/4EeAX+E9LFaNgpv48XmJXZtZ9hjOvhvwyDdCEwXsFStH6yulHcxlH 15 | Ri7iVeEOeBPhRXEhM4jOebw8Kqqp2so170Cc/kDZOx/383/e02FxEAg9AoGBAOr2 16 | PU+IWNBHZP/1BpekQRZBSpwmlsML5/YnIyXWeYO+d0Kwp3Qi0lKKL/ZI+/bVRGyq 17 | tEl6fGQzzPoT7XuTS8pey7K1HSyunYdx+vbS6L/ID07mO5oIFjtAbcJQfM7kfqWr 18 | m662lYvYdCYVPYF0ZfCw20ayb3LQNJTtzVp/5fG3AoGAapyQasOsOs+DYoNe0Anl 19 | tlSuNIzLXXGAb+6GCqJd/hiQh+8tbrgmmDao6u212p+8zxx+VVZoTnhUTiGUT6BR 20 | Sw9Yhw6CbhL1bRIiNsXniqjIXcuf5NWFjNErKDgX1DOWrTEf/UUBfa4+iGvI5fCY 21 | jKTtguHbT7o6plMXd2IpCeECgYEAjqWoW1y/7tMpcmvj8LR7/3LsRRj4droHvd3M 22 | 7O/lHiLIbOirAUsSvaG/tJpivlyECiUpB2MhmgQI8dqfV2L5pIcgzAJBEBuC7/Q3 23 | FRq+y5A+GvyUOtOmC5r2B/6thz4Z1aL2BYA8Eob7HRPQH4P3T8VPmwBBwUa+ZZxq 24 | DAOSKskCgYAWCldfjkOgynFHjqQ3HXDtSRLMrgVEj/+Muy6yCu7Lk+lhOsrflK82 25 | 1rMH+O8Z2RxFIA6KuzHZbUXexNzmibsEIWBZU5X14QrLkX1Gq9mCwLv8cSMDqKz+ 26 | qNLcMrEDqSmdPkPppVsJbz44QvFItVWYsPAWS4cyN62X6IJ2lDPjNw== 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /microservices/httpsservercert/certs/user.req: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIIC1zCCAb8CAQAwgZExCzAJBgNVBAYTAkRFMQswCQYDVQQIDAJCVzETMBEGA1UE 3 | BwwKUmV1dGxpbmdlbjEcMBoGA1UECgwTU291cmNlIEZlbGxvd3MgR21iSDEjMCEG 4 | CSqGSIb3DQEJARYUdXNlckBleGFtcGxlLnByaXZhdGUxHTAbBgNVBAMMFHRlc3Qu 5 | ZXhhbXBsZS5wcml2YXRlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA 6 | 46m2xTUGbRLu5+5giUkqrLK+PffFTVUI/IBDqv0FbEOUpQLJH/xwxESFWrj5lfiK 7 | XC4SVUMj8tA0w6RBiRSp0RnUDdBYhzifbB2JLSNgj56v9Frw0KT9SkmbGd+XINuA 8 | e+7yuueatDxIdJxXYQtO7utTXco9nvPS1Rr3ejQdCXNEHK89MXKQ6UMLs8kwWCHf 9 | J1s5QoXx7NkciThkktba+SLB6bUivzoyEPnj5rNzHdJ+qaYnhoOMfp1POdBTRQv3 10 | iYvyjENEQqlKgYNgux9j9XX6czuFyWJ9HYphTDz5I6M9l3hvbRPbiHn7iQs+plC4 11 | cprI03vS7GwVPav3i8hQmwIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAJ2ZHvrq 12 | QE8yoiOSb+itUtWLo86T+qbSwHLy9Ned0wtu1XiL7ltJATyRwMbv1RduGcfYWBcx 13 | ZcUAcqYhsuSTM3MPg0MQvAFrLRwonq+TN6zq0K7RvZ5FSCizzcyThJIRcCtUNd0y 14 | heqknhsvnkIwSQCb4RPF3/57FXct1FNEHEKMtldWEHCkKbJnUtt9Z/4WEkmTCvzi 15 | EYwcwoK6QsmJOIwQXzOYniaPf2k8oeBLu81sq5GG5vRZvs9dFX6HQmfQ28oKeMCy 16 | 8n6XGHPudNybKBykvYzRKbChVl6LiFdz1f95x5JSh+aJx8Ca8mq5NpFK0TJEzQWv 17 | NUvFSnG7X4w6JJg= 18 | -----END CERTIFICATE REQUEST----- 19 | -------------------------------------------------------------------------------- /microservices/httpsservercert/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/https/servercert 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /microservices/httpsservercert/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "crypto/tls" 5 | "crypto/x509" 6 | "fmt" 7 | "io/ioutil" 8 | "log" 9 | "net/http" 10 | ) 11 | 12 | func main() { 13 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 14 | fmt.Fprintln(w, "Hello World") 15 | }) 16 | 17 | certPool := x509.NewCertPool() 18 | pem, err := ioutil.ReadFile("certs/server-chain.crt") 19 | if err != nil { 20 | panic(err) 21 | } 22 | certPool.AppendCertsFromPEM(pem) 23 | 24 | tlsConfig := &tls.Config{ 25 | ClientAuth: tls.RequireAndVerifyClientCert, 26 | ClientCAs: certPool, 27 | } 28 | 29 | srv := http.Server{ 30 | Addr: ":8443", 31 | TLSConfig: tlsConfig, 32 | } 33 | 34 | log.Fatal(srv.ListenAndServeTLS("certs/test.example.crt", "certs/test.example.key")) 35 | } 36 | -------------------------------------------------------------------------------- /microservices/jsonhandling/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | ) 7 | 8 | type Customer struct { 9 | ID int `json:"id,omitempty"` 10 | Firstname string `json:"first,omitempty"` 11 | Lastname string `json:"last,omitempty"` 12 | } 13 | 14 | func main() { 15 | customer := &Customer{ID: 1, 16 | Firstname: "Hans", Lastname: "Wurst"} 17 | bites, _ := json.Marshal(customer) 18 | fmt.Println(string(bites)) 19 | 20 | customer2 := &Customer{} 21 | json.Unmarshal(bites, customer2) 22 | fmt.Println(customer2.Firstname) 23 | } 24 | -------------------------------------------------------------------------------- /microservices/jsonservice/GET-request.http: -------------------------------------------------------------------------------- 1 | GET http://localhost:8080/customer HTTP/1.1 -------------------------------------------------------------------------------- /microservices/jsonservice/POST-request.http: -------------------------------------------------------------------------------- 1 | POST http://localhost:8080/customer HTTP/1.1 2 | content-type: application/json 3 | 4 | { 5 | "ID": 2, 6 | "Lastname": "Wurst" 7 | } 8 | 9 | -------------------------------------------------------------------------------- /microservices/jsonservice/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/jsonservice 2 | 3 | go 1.13 4 | 5 | require github.com/gorilla/mux v1.7.4 6 | -------------------------------------------------------------------------------- /microservices/jsonservice/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= 2 | github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /microservices/jsonservice/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "net/http" 7 | 8 | "github.com/gorilla/mux" 9 | ) 10 | 11 | type Customer struct { 12 | ID int `json:"id,omitempty"` 13 | Firstname string `json:"first,omitempty"` 14 | Lastname string `json:"last,omitempty"` 15 | } 16 | 17 | var customer *Customer 18 | 19 | func myGetFunc(rw http.ResponseWriter, rq *http.Request) { 20 | bites, _ := json.Marshal(customer) 21 | rw.Header().Add("Content-Type", "application/json") 22 | rw.Write(bites) 23 | } 24 | 25 | func myPostFunc(rw http.ResponseWriter, rq *http.Request) { 26 | body, _ := ioutil.ReadAll(rq.Body) 27 | json.Unmarshal(body, customer) 28 | rw.WriteHeader(204) 29 | } 30 | 31 | func main() { 32 | customer = &Customer{ID: 1, Firstname: "Peter"} 33 | r := mux.NewRouter() 34 | r.HandleFunc("/customer", myGetFunc).Methods("GET") 35 | r.HandleFunc("/customer", myPostFunc).Methods("POST") 36 | http.ListenAndServe(":8080", r) 37 | } 38 | -------------------------------------------------------------------------------- /microservices/jwt/server/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/jwt/server 2 | 3 | go 1.14 4 | 5 | require github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 6 | -------------------------------------------------------------------------------- /microservices/jwt/server/go.sum: -------------------------------------------------------------------------------- 1 | github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 h1:koK7z0nSsRiRiBWwa+E714Puh+DO+ZRdIyAXiXzL+lg= 2 | github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2/go.mod h1:ARgCUhI1MHQH+ONky/PAtmVHQrP5JlGY0F3poXOp/fA= 3 | -------------------------------------------------------------------------------- /microservices/jwt/server/jwt/token.go: -------------------------------------------------------------------------------- 1 | package jwt 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/SermoDigital/jose/crypto" 7 | "github.com/SermoDigital/jose/jws" 8 | ) 9 | 10 | var key = []byte("meinGeheimerSchlüssel") 11 | 12 | func CreateToken(user string) ([]byte, error) { 13 | claims := jws.Claims{} 14 | claims.SetAudience("source-fellows.com") 15 | claims.SetIssuer(user) 16 | claims.SetExpiration(time.Now().Add(time.Minute * 10)) 17 | token := jws.NewJWT(claims, crypto.SigningMethodHS256) 18 | return token.Serialize(key) 19 | } 20 | 21 | func ValidateToken(token []byte) error { 22 | newToken, err := jws.ParseJWT(token) 23 | if err != nil { 24 | return err 25 | } 26 | return newToken.Validate(key, crypto.SigningMethodHS256) 27 | } 28 | -------------------------------------------------------------------------------- /microservices/jwt/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net/http" 6 | 7 | "golang.source-fellows.com/samples/jwt/server/jwt" 8 | ) 9 | 10 | func handle(res http.ResponseWriter, req *http.Request) { 11 | token, err := jwt.CreateToken() 12 | if err != nil { 13 | log.Fatal(err) 14 | res.WriteHeader(http.StatusInternalServerError) 15 | return 16 | } 17 | 18 | err = jwt.ValidateToken(token) 19 | if err != nil { 20 | log.Fatal(err) 21 | res.WriteHeader(http.StatusInternalServerError) 22 | return 23 | } 24 | 25 | res.Write(token) 26 | } 27 | 28 | func main() { 29 | http.HandleFunc("/", handle) 30 | http.ListenAndServe(":8080", nil) 31 | } 32 | -------------------------------------------------------------------------------- /microservices/letscrypt/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/https/letscrypt 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /microservices/letscrypt/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | 8 | "golang.org/x/crypto/acme/autocert" 9 | ) 10 | 11 | func main() { 12 | 13 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 14 | fmt.Fprintln(w, "Hello World") 15 | }) 16 | 17 | log.Fatal(http.Serve(autocert.NewListener("x.source-fellows.com"), nil)) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /microservices/logging/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/logging 2 | 3 | go 1.13 4 | 5 | require github.com/sirupsen/logrus v1.6.0 6 | -------------------------------------------------------------------------------- /microservices/logging/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= 4 | github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 5 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 6 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 7 | github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= 8 | github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= 9 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 10 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 11 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= 12 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 13 | -------------------------------------------------------------------------------- /microservices/logging/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "time" 5 | 6 | log "github.com/sirupsen/logrus" 7 | ) 8 | 9 | type Testing struct { 10 | Val1 bool 11 | Val2 string 12 | Val3 time.Time 13 | } 14 | 15 | func main() { 16 | 17 | log.SetFormatter(&log.JSONFormatter{}) 18 | 19 | // zusätzliche Felder an log.Entry anhängen 20 | log.WithFields(log.Fields{ 21 | "importId": "ka18s", 22 | "size": 10, 23 | }).Info("Starte den Import") 24 | // Wiederverwenden von log.Entry damit alle weiteren 25 | // Aufrufe ebenfalls Felder bekommen 26 | contextLogger := log.WithFields(log.Fields{ 27 | "importId": "0815s", 28 | "other": "Ich werde geloggt", 29 | "test": Testing{Val3: time.Now()}, 30 | }) 31 | contextLogger.Info("I'll be logged with common and other field") 32 | contextLogger.Info("Me too") 33 | } 34 | -------------------------------------------------------------------------------- /microservices/middleware/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/middleware 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /microservices/middleware/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net/http" 6 | ) 7 | 8 | func businessLogic(text string) http.Handler { 9 | myFunc := func(res http.ResponseWriter, req *http.Request) { 10 | res.Write([]byte(text)) 11 | } 12 | return http.HandlerFunc(myFunc) 13 | } 14 | 15 | func middleWare(wrapped http.Handler) http.Handler { 16 | return http.HandlerFunc(func(re http.ResponseWriter, rq *http.Request) { 17 | log.Println("before request") 18 | wrapped.ServeHTTP(re, rq) 19 | log.Println("after request") 20 | }) 21 | } 22 | 23 | func main() { 24 | http.Handle("/", middleWare(businessLogic("Hello World"))) 25 | if err := http.ListenAndServe(":8080", nil); err != nil { 26 | log.Printf("error with server %v", err) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /microservices/mongodb-update/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | mongodb: 4 | image: 'mongo' 5 | container_name: 'mongo-container' 6 | environment: 7 | - MONGO_INITDB_DATABASE=mongodb 8 | - MONGO_INITDB_ROOT_USERNAME=mongorootuser 9 | - MONGO_INITDB_ROOT_PASSWORD=mongorootpw 10 | ports: 11 | - '27017-27019:27017-27019' 12 | mongo-express: 13 | image: mongo-express 14 | environment: 15 | - ME_CONFIG_MONGODB_SERVER=mongodb 16 | - ME_CONFIG_MONGODB_ADMINUSERNAME=mongorootuser 17 | - ME_CONFIG_MONGODB_ADMINPASSWORD=mongorootpw 18 | ports: 19 | - 8081:8081 -------------------------------------------------------------------------------- /microservices/mongodb-update/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/mongodb 2 | 3 | go 1.14 4 | 5 | require go.mongodb.org/mongo-driver v1.3.5 // indirect 6 | -------------------------------------------------------------------------------- /microservices/mongodb/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | mongodb: 4 | image: 'mongo' 5 | container_name: 'mongo-container' 6 | environment: 7 | - MONGO_INITDB_DATABASE=mongodb 8 | - MONGO_INITDB_ROOT_USERNAME=mongorootuser 9 | - MONGO_INITDB_ROOT_PASSWORD=mongorootpw 10 | ports: 11 | - '27017-27019:27017-27019' 12 | mongo-express: 13 | image: mongo-express 14 | environment: 15 | - ME_CONFIG_MONGODB_SERVER=mongodb 16 | - ME_CONFIG_MONGODB_ADMINUSERNAME=mongorootuser 17 | - ME_CONFIG_MONGODB_ADMINPASSWORD=mongorootpw 18 | ports: 19 | - 8081:8081 -------------------------------------------------------------------------------- /microservices/mongodb/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/mongodb 2 | 3 | go 1.14 4 | 5 | require go.mongodb.org/mongo-driver v1.3.5 // indirect 6 | -------------------------------------------------------------------------------- /microservices/mongodb/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "time" 7 | 8 | "go.mongodb.org/mongo-driver/bson" 9 | "go.mongodb.org/mongo-driver/mongo" 10 | "go.mongodb.org/mongo-driver/mongo/options" 11 | "go.mongodb.org/mongo-driver/mongo/readpref" 12 | ) 13 | 14 | type Customer struct { 15 | FirstName string 16 | LastName string 17 | CreditCard CreditCard 18 | } 19 | 20 | type CreditCard struct { 21 | Number string 22 | } 23 | 24 | func main() { 25 | client, err := mongo.NewClient( 26 | options.Client(). 27 | ApplyURI("mongodb://mongorootuser:mongorootpw@localhost:27017")) 28 | if err != nil { 29 | panic(err) 30 | } 31 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) 32 | defer cancel() 33 | err = client.Connect(ctx) 34 | if err != nil { 35 | panic(err) 36 | } 37 | defer func() { 38 | if err = client.Disconnect(ctx); err != nil { 39 | panic(err) 40 | } 41 | }() 42 | err = client.Ping(ctx, readpref.Primary()) 43 | if err != nil { 44 | panic(err) 45 | } 46 | 47 | collection := client.Database("shop").Collection("customer") 48 | customer := Customer{FirstName: "Hans", LastName: "wurst"} 49 | customer.CreditCard = CreditCard{Number: "123-123-123"} 50 | collection.InsertOne(ctx, customer) 51 | 52 | cursor, err := collection.Find(ctx, bson.D{{"firstname", "Hans"}}) 53 | if err != nil { 54 | panic(err) 55 | } 56 | defer cursor.Close(ctx) 57 | 58 | for cursor.Next(ctx) { 59 | var result bson.M 60 | err := cursor.Decode(&result) 61 | if err != nil { 62 | panic(err) 63 | } 64 | fmt.Printf("result is %v\n", result) 65 | } 66 | if err := cursor.Err(); err != nil { 67 | panic(err) 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /microservices/nats/receiver/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "time" 6 | 7 | "github.com/nats-io/nats.go" 8 | ) 9 | 10 | func main() { 11 | nc, err := nats.Connect(nats.DefaultURL) 12 | if err != nil { 13 | log.Fatalf("Could not connect to server because of %v", err) 14 | } 15 | defer nc.Close() 16 | sub, err := nc.SubscribeSync("foo") 17 | if err != nil { 18 | log.Fatalf("Could not subscribe because of %v", err) 19 | } 20 | m, err := sub.NextMsg(20 * time.Second) 21 | if m == nil { 22 | log.Println("No messages available") 23 | return 24 | } 25 | log.Println(m.Subject) 26 | log.Println(string(m.Data)) 27 | sub.Unsubscribe() 28 | sub.Drain() 29 | } 30 | -------------------------------------------------------------------------------- /microservices/nats/receiver/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/nats/receiver 2 | 3 | go 1.14 4 | 5 | require github.com/nats-io/nats.go v1.10.0 6 | -------------------------------------------------------------------------------- /microservices/nats/receiver/go.sum: -------------------------------------------------------------------------------- 1 | github.com/nats-io/jwt v0.3.2 h1:+RB5hMpXUUA2dfxuhBTEkMOrYmM+gKIZYS1KjSostMI= 2 | github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU= 3 | github.com/nats-io/nats.go v1.10.0 h1:L8qnKaofSfNFbXg0C5F71LdjPRnmQwSsA4ukmkt1TvY= 4 | github.com/nats-io/nats.go v1.10.0/go.mod h1:AjGArbfyR50+afOUotNX2Xs5SYHf+CoOa5HH1eEl2HE= 5 | github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= 6 | github.com/nats-io/nkeys v0.1.4 h1:aEsHIssIk6ETN5m2/MD8Y4B2X7FfXrBAUdkyRvbVYzA= 7 | github.com/nats-io/nkeys v0.1.4/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s= 8 | github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= 9 | github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= 10 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 11 | golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 12 | golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM= 13 | golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 14 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 15 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 16 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 17 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 18 | -------------------------------------------------------------------------------- /microservices/nats/sender/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/nats-io/nats.go" 7 | ) 8 | 9 | func main() { 10 | nc, err := nats.Connect(nats.DefaultURL) 11 | 12 | if err != nil { 13 | log.Fatalf("Could not connect to server because of %v", err) 14 | } 15 | defer nc.Close() 16 | 17 | nc.Publish("foo", []byte("Hello World")) 18 | 19 | } 20 | -------------------------------------------------------------------------------- /microservices/nats/sender/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/nats/sender 2 | 3 | go 1.14 4 | 5 | require github.com/nats-io/nats.go v1.10.0 // indirect 6 | -------------------------------------------------------------------------------- /microservices/nats/sender/go.sum: -------------------------------------------------------------------------------- 1 | github.com/nats-io/jwt v0.3.2 h1:+RB5hMpXUUA2dfxuhBTEkMOrYmM+gKIZYS1KjSostMI= 2 | github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU= 3 | github.com/nats-io/nats.go v1.10.0 h1:L8qnKaofSfNFbXg0C5F71LdjPRnmQwSsA4ukmkt1TvY= 4 | github.com/nats-io/nats.go v1.10.0/go.mod h1:AjGArbfyR50+afOUotNX2Xs5SYHf+CoOa5HH1eEl2HE= 5 | github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= 6 | github.com/nats-io/nkeys v0.1.4 h1:aEsHIssIk6ETN5m2/MD8Y4B2X7FfXrBAUdkyRvbVYzA= 7 | github.com/nats-io/nkeys v0.1.4/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s= 8 | github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= 9 | github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= 10 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 11 | golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 12 | golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM= 13 | golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 14 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 15 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 16 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 17 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 18 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-prepared-statement/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/go 3 | { 4 | "name": "Go", 5 | "dockerFile": "Dockerfile", 6 | "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], 7 | 8 | // Set *default* container specific settings.json values on container create. 9 | "settings": { 10 | "terminal.integrated.shell.linux": "/bin/bash", 11 | "go.gopath": "/go" 12 | }, 13 | 14 | // Add the IDs of extensions you want installed when the container is created. 15 | "extensions": [ 16 | "golang.Go" 17 | ] 18 | 19 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 20 | // "forwardPorts": [], 21 | 22 | // Use 'postCreateCommand' to run commands after the container is created. 23 | ,"postCreateCommand": "go get", 24 | 25 | // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. 26 | // "remoteUser": "vscode" 27 | } -------------------------------------------------------------------------------- /microservices/oracle-db-connection-prepared-statement/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/oracle 2 | 3 | go 1.14 4 | 5 | require github.com/godror/godror v0.17.0 6 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-prepared-statement/go.sum: -------------------------------------------------------------------------------- 1 | github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= 2 | github.com/godror/godror v0.17.0 h1:ClVuKG0qCj6f182BruhwgHS8xCgfpQp35pTrefSq4NE= 3 | github.com/godror/godror v0.17.0/go.mod h1:DE94Br7LXn4dQGCexePriVrKotR9GkVzPPT5nnm8dj0= 4 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 5 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 6 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 7 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 8 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-prepared-statement/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "log" 6 | "time" 7 | 8 | _ "github.com/godror/godror" 9 | ) 10 | 11 | type Angestellter struct { 12 | id int 13 | Name string 14 | Einstellungsdatum time.Time 15 | } 16 | 17 | func main() { 18 | db, err := sql.Open("godror", "app/app@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=172.17.0.1)(PORT=1521)))(CONNECT_DATA=(SID=ORCLCDB)))") 19 | if err != nil { 20 | log.Fatal(err) 21 | } 22 | defer db.Close() 23 | db.SetMaxIdleConns(0) 24 | db.SetMaxOpenConns(100) 25 | db.SetConnMaxLifetime(2 * time.Hour) 26 | 27 | stmt, err := db.Prepare("select EMPNO, ENAME, HIREDATE from EMP where EMPNO = :p") 28 | if err != nil { 29 | log.Fatalf("Fehler bei Query: %v", err) 30 | } 31 | defer stmt.Close() 32 | rows, err := stmt.Query(sql.Named("p", 7499)) 33 | if err != nil { 34 | log.Fatal(err) 35 | } 36 | defer rows.Close() 37 | 38 | log.Println("retri") 39 | 40 | for rows.Next() { 41 | angestellter := &Angestellter{} 42 | err = rows.Scan(&angestellter.id, 43 | &angestellter.Name, 44 | &angestellter.Einstellungsdatum) 45 | if err != nil { 46 | log.Fatalf("Fehler beim Laden: %v", err) 47 | } 48 | log.Println(angestellter) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write-tx/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/go 3 | { 4 | "name": "Go", 5 | "dockerFile": "Dockerfile", 6 | "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], 7 | 8 | // Set *default* container specific settings.json values on container create. 9 | "settings": { 10 | "terminal.integrated.shell.linux": "/bin/bash", 11 | "go.gopath": "/go" 12 | }, 13 | 14 | // Add the IDs of extensions you want installed when the container is created. 15 | "extensions": [ 16 | "golang.Go" 17 | ] 18 | 19 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 20 | // "forwardPorts": [], 21 | 22 | // Use 'postCreateCommand' to run commands after the container is created. 23 | ,"postCreateCommand": "go get", 24 | 25 | // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. 26 | // "remoteUser": "vscode" 27 | } -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write-tx/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/oracle 2 | 3 | go 1.14 4 | 5 | require github.com/godror/godror v0.17.0 6 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write-tx/go.sum: -------------------------------------------------------------------------------- 1 | github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= 2 | github.com/godror/godror v0.17.0 h1:ClVuKG0qCj6f182BruhwgHS8xCgfpQp35pTrefSq4NE= 3 | github.com/godror/godror v0.17.0/go.mod h1:DE94Br7LXn4dQGCexePriVrKotR9GkVzPPT5nnm8dj0= 4 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 5 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 6 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 7 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 8 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write-tx/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "database/sql" 6 | "log" 7 | "time" 8 | 9 | _ "github.com/godror/godror" 10 | ) 11 | 12 | type Angestellter struct { 13 | id int 14 | Name string 15 | Einstellungsdatum time.Time 16 | } 17 | 18 | func main() { 19 | db, err := sql.Open("godror", "app/app@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=172.17.0.1)(PORT=1521)))(CONNECT_DATA=(SID=ORCLCDB)))") 20 | if err != nil { 21 | log.Fatal(err) 22 | } 23 | db.SetMaxOpenConns(0) 24 | db.SetMaxIdleConns(0) 25 | defer db.Close() 26 | 27 | tx, err := db.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelReadCommitted, ReadOnly: true}) 28 | stmt, err := tx.Prepare("INSERT INTO EMP (EMPNO, ENAME, HIREDATE) VALUES(:id1, :name2, :date3)") 29 | if err != nil { 30 | log.Fatalf("Fehler bei Statement: %v", err) 31 | } 32 | for i := 10; i < 100; i++ { 33 | _, err = stmt.Exec( 34 | sql.Named("id1", i), 35 | sql.Named("name2", "JOE"), 36 | sql.Named("date3", time.Now())) 37 | 38 | if err != nil { 39 | log.Fatalf("Fehler bei Exec: %v", err) 40 | } 41 | } 42 | defer stmt.Close() 43 | tx.Commit() 44 | 45 | } 46 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/go 3 | { 4 | "name": "Go", 5 | "dockerFile": "Dockerfile", 6 | "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], 7 | 8 | // Set *default* container specific settings.json values on container create. 9 | "settings": { 10 | "terminal.integrated.shell.linux": "/bin/bash", 11 | "go.gopath": "/go" 12 | }, 13 | 14 | // Add the IDs of extensions you want installed when the container is created. 15 | "extensions": [ 16 | "golang.Go" 17 | ] 18 | 19 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 20 | // "forwardPorts": [], 21 | 22 | // Use 'postCreateCommand' to run commands after the container is created. 23 | ,"postCreateCommand": "go get", 24 | 25 | // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. 26 | // "remoteUser": "vscode" 27 | } -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/oracle 2 | 3 | go 1.14 4 | 5 | require github.com/godror/godror v0.17.0 6 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write/go.sum: -------------------------------------------------------------------------------- 1 | github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= 2 | github.com/godror/godror v0.17.0 h1:ClVuKG0qCj6f182BruhwgHS8xCgfpQp35pTrefSq4NE= 3 | github.com/godror/godror v0.17.0/go.mod h1:DE94Br7LXn4dQGCexePriVrKotR9GkVzPPT5nnm8dj0= 4 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 5 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 6 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 7 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 8 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection-write/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "log" 6 | "time" 7 | 8 | _ "github.com/godror/godror" 9 | ) 10 | 11 | type Angestellter struct { 12 | id int 13 | Name string 14 | Einstellungsdatum time.Time 15 | } 16 | 17 | func main() { 18 | db, err := sql.Open("godror", "app/app@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=172.17.0.1)(PORT=1521)))(CONNECT_DATA=(SID=ORCLCDB)))") 19 | if err != nil { 20 | log.Fatal(err) 21 | } 22 | db.SetMaxOpenConns(0) 23 | db.SetMaxIdleConns(0) 24 | defer db.Close() 25 | res, err := db.Exec("INSERT INTO EMP (EMPNO, ENAME, HIREDATE) VALUES(:id1, :name1, :date1)", 1, "WURST", time.Now()) 26 | if err != nil { 27 | log.Fatalf("Fehler bei Exec: %v", err) 28 | } 29 | count, _ := res.RowsAffected() 30 | log.Printf("Updated %v row", count) 31 | 32 | stmt, err := db.Prepare("INSERT INTO EMP (EMPNO, ENAME, HIREDATE) VALUES(:id1, :name2, :date3)") 33 | if err != nil { 34 | log.Fatalf("Fehler bei Statement: %v", err) 35 | } 36 | for i := 10; i < 100; i++ { 37 | _, err = stmt.Exec( 38 | sql.Named("id1", i), 39 | sql.Named("name2", "JOE"), 40 | sql.Named("date3", time.Now())) 41 | 42 | if err != nil { 43 | log.Fatalf("Fehler bei Exec: %v", err) 44 | } 45 | } 46 | defer stmt.Close() 47 | 48 | } 49 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/go 3 | { 4 | "name": "Go", 5 | "dockerFile": "Dockerfile", 6 | "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], 7 | 8 | // Set *default* container specific settings.json values on container create. 9 | "settings": { 10 | "terminal.integrated.shell.linux": "/bin/bash", 11 | "go.gopath": "/go" 12 | }, 13 | 14 | // Add the IDs of extensions you want installed when the container is created. 15 | "extensions": [ 16 | "golang.Go" 17 | ] 18 | 19 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 20 | // "forwardPorts": [], 21 | 22 | // Use 'postCreateCommand' to run commands after the container is created. 23 | ,"postCreateCommand": "go get", 24 | 25 | // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. 26 | // "remoteUser": "vscode" 27 | } -------------------------------------------------------------------------------- /microservices/oracle-db-connection/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/oracle 2 | 3 | go 1.14 4 | 5 | require github.com/godror/godror v0.17.0 6 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection/go.sum: -------------------------------------------------------------------------------- 1 | github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= 2 | github.com/godror/godror v0.17.0 h1:ClVuKG0qCj6f182BruhwgHS8xCgfpQp35pTrefSq4NE= 3 | github.com/godror/godror v0.17.0/go.mod h1:DE94Br7LXn4dQGCexePriVrKotR9GkVzPPT5nnm8dj0= 4 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 5 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 6 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 7 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 8 | -------------------------------------------------------------------------------- /microservices/oracle-db-connection/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "log" 6 | "time" 7 | 8 | _ "github.com/godror/godror" 9 | ) 10 | 11 | type Angestellter struct { 12 | id int 13 | Name string 14 | Einstellungsdatum time.Time 15 | } 16 | 17 | func main() { 18 | db, err := sql.Open("godror", "app/app@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=172.17.0.1)(PORT=1521)))(CONNECT_DATA=(SID=ORCLCDB)))") 19 | if err != nil { 20 | log.Fatal(err) 21 | } 22 | defer db.Close() 23 | rows, err := db.Query("select EMPNO, ENAME, HIREDATE from EMP where EMPNO = :0", 7499) 24 | if err != nil { 25 | log.Fatalf("Fehler bei Query: %v", err) 26 | } 27 | defer rows.Close() 28 | for rows.Next() { 29 | angestellter := &Angestellter{} 30 | err = rows.Scan(&angestellter.id, 31 | &angestellter.Name, 32 | &angestellter.Einstellungsdatum) 33 | if err != nil { 34 | log.Fatalf("Fehler beim Laden: %v", err) 35 | } 36 | log.Println(angestellter) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /microservices/prometheus-metrics/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/prometheusmetric 2 | 3 | go 1.14 4 | 5 | require github.com/prometheus/client_golang v1.7.1 6 | -------------------------------------------------------------------------------- /microservices/prometheus-metrics/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | 7 | "github.com/prometheus/client_golang/prometheus" 8 | "github.com/prometheus/client_golang/prometheus/promauto" 9 | "github.com/prometheus/client_golang/prometheus/promhttp" 10 | ) 11 | 12 | var ( 13 | postCallsCounter = promauto.NewCounter(prometheus.CounterOpts{ 14 | Name: "myapp_processed_post_total", 15 | Help: "The total number of POST requests", 16 | }) 17 | getCallsCounter = promauto.NewCounter(prometheus.CounterOpts{ 18 | Name: "myapp_processed_get_total", 19 | Help: "The total number of GET requests", 20 | }) 21 | ) 22 | 23 | func main() { 24 | http.HandleFunc("/do", func(rs http.ResponseWriter, rq *http.Request) { 25 | 26 | if rq.Method == http.MethodPost { 27 | postCallsCounter.Add(1) 28 | } else if rq.Method == http.MethodGet { 29 | getCallsCounter.Add(1) 30 | } 31 | 32 | fmt.Fprintln(rs, "Hello World") 33 | }) 34 | http.Handle("/metrics", promhttp.Handler()) 35 | http.ListenAndServe(":8080", nil) 36 | } 37 | -------------------------------------------------------------------------------- /microservices/prometheus/application/Dockerfile: -------------------------------------------------------------------------------- 1 | from golang:latest 2 | 3 | RUN mkdir /application 4 | ADD . /application 5 | WORKDIR /application 6 | RUN go build -o application . 7 | 8 | CMD ["/application/application"] 9 | -------------------------------------------------------------------------------- /microservices/prometheus/application/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/prometheus 2 | 3 | go 1.14 4 | 5 | require github.com/prometheus/client_golang v1.7.1 6 | -------------------------------------------------------------------------------- /microservices/prometheus/application/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/prometheus/client_golang/prometheus/promhttp" 7 | ) 8 | 9 | func main() { 10 | http.HandleFunc("/", func(rs http.ResponseWriter, rq *http.Request) { 11 | rs.WriteHeader(http.StatusOK) 12 | }) 13 | http.Handle("/metrics", promhttp.Handler()) 14 | http.ListenAndServe(":8080", nil) 15 | } 16 | -------------------------------------------------------------------------------- /microservices/prometheus/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | prometheus: 4 | image: prom/prometheus 5 | ports: 6 | - "9090:9090" 7 | volumes: 8 | - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml 9 | application: 10 | build: application 11 | ports: 12 | - "8080:8080" 13 | grafana: 14 | image: grafana/grafana 15 | ports: 16 | - "3000:3000" 17 | volumes: 18 | - ./grafana/grafana.ini:/etc/grafana/grafana.ini -------------------------------------------------------------------------------- /microservices/prometheus/prometheus/prometheus.yml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 5s 3 | external_labels: 4 | monitor: 'sample-monitor' 5 | scrape_configs: 6 | - job_name: 'application' 7 | static_configs: 8 | - targets: ['application:8080'] -------------------------------------------------------------------------------- /microservices/rfc7808/client/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/rfc7808/server 2 | 3 | go 1.13 4 | 5 | require github.com/moogar0880/problems v0.1.1 6 | -------------------------------------------------------------------------------- /microservices/rfc7808/client/go.sum: -------------------------------------------------------------------------------- 1 | github.com/moogar0880/problems v0.1.1 h1:bktLhq8NDG/czU2ZziYNigBFksx13RaYe5AVdNmHDT4= 2 | github.com/moogar0880/problems v0.1.1/go.mod h1:5Dxrk2sD7BfBAgnOzQ1yaTiuCYdGPUh49L8Vhfky62c= 3 | -------------------------------------------------------------------------------- /microservices/rfc7808/client/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "log" 7 | "net/http" 8 | 9 | "github.com/moogar0880/problems" 10 | ) 11 | 12 | func main() { 13 | res, err := http.Get("http://localhost:8080/a") 14 | if err != nil { 15 | log.Fatal("could not request") 16 | } 17 | defer res.Body.Close() 18 | if res.StatusCode != http.StatusOK { 19 | problem := &problems.DefaultProblem{} 20 | bites, _ := ioutil.ReadAll(res.Body) 21 | json.Unmarshal(bites, problem) 22 | log.Println(problem.ProblemTitle()) 23 | } else { 24 | log.Println("ok") 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /microservices/rfc7808/server/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/rfc7808/server 2 | 3 | go 1.13 4 | 5 | require github.com/moogar0880/problems v0.1.1 // indirect 6 | -------------------------------------------------------------------------------- /microservices/rfc7808/server/go.sum: -------------------------------------------------------------------------------- 1 | github.com/moogar0880/problems v0.1.1 h1:bktLhq8NDG/czU2ZziYNigBFksx13RaYe5AVdNmHDT4= 2 | github.com/moogar0880/problems v0.1.1/go.mod h1:5Dxrk2sD7BfBAgnOzQ1yaTiuCYdGPUh49L8Vhfky62c= 3 | -------------------------------------------------------------------------------- /microservices/rfc7808/server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | 7 | "github.com/moogar0880/problems" 8 | ) 9 | 10 | func handleHttp(res http.ResponseWriter, req *http.Request) { 11 | NotFound := problems.NewStatusProblem(http.StatusNotFound) 12 | bites, _ := json.Marshal(NotFound) 13 | res.WriteHeader(http.StatusNotFound) 14 | res.Header().Add("Content-Type", "application/json") 15 | res.Write(bites) 16 | 17 | } 18 | 19 | func main() { 20 | http.HandleFunc("/a/", handleHttp) 21 | http.ListenAndServe(":8080", nil) 22 | } 23 | -------------------------------------------------------------------------------- /microservices/simple-http/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/simplehttp 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /microservices/simple-http/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "net/http" 4 | 5 | func handleHttp(res http.ResponseWriter, req *http.Request) { 6 | res.Write([]byte("Hello World")) 7 | } 8 | 9 | func main() { 10 | http.HandleFunc("/", handleHttp) 11 | http.ListenAndServe(":8080", nil) 12 | } 13 | -------------------------------------------------------------------------------- /module-logrus/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/modulelogrus 2 | 3 | go 1.13 4 | 5 | require github.com/sirupsen/logrus v1.6.0 6 | -------------------------------------------------------------------------------- /module-logrus/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= 3 | github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= 6 | github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= 7 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 8 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 9 | -------------------------------------------------------------------------------- /module-logrus/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | log "github.com/sirupsen/logrus" 5 | ) 6 | 7 | func main() { 8 | log.Println("Hello World") 9 | } 10 | -------------------------------------------------------------------------------- /quality/http-server-test.v1/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/http-server-test 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /quality/http-server-test.v1/main.go: -------------------------------------------------------------------------------- 1 | /*Package main zeigt einen einfachen HTTP Service, der getestet werden soll. 2 | 3 | Das Abschalten des Power-Safe Modus unter Linux hilft folgendes Script: 4 | 5 | for i in /sys/devices/system/cpu/cpu[0-7] 6 | do 7 | echo performance > $i/cpufreq/scaling_governor 8 | done 9 | 10 | Die Laufzeitgeschwindigkeit kann zusätzlich über 'ab' (Apache HTTP server benchmarking tool - 11 | https://httpd.apache.org/docs/2.4/programs/ab.html) betestet werden. 12 | */ 13 | package main 14 | 15 | import ( 16 | "encoding/json" 17 | "net/http" 18 | _ "net/http/pprof" 19 | "regexp" 20 | ) 21 | 22 | type Result struct { 23 | Name string 24 | } 25 | 26 | func myHandler(writer http.ResponseWriter, req *http.Request) { 27 | path := req.URL.Path 28 | re := regexp.MustCompile("^/json/(.*)$") 29 | 30 | if !re.MatchString(path) { 31 | writer.WriteHeader(401) 32 | return 33 | } 34 | 35 | parts := re.FindStringSubmatch(path) 36 | res := &Result{Name: parts[1]} 37 | bites, err := json.Marshal(*res) 38 | if err != nil { 39 | writer.WriteHeader(500) 40 | return 41 | } 42 | writer.Header().Add("Content-Type", "application/json") 43 | writer.Write(bites) 44 | } 45 | 46 | func main() { 47 | http.HandleFunc("/", myHandler) 48 | http.ListenAndServe(":8080", nil) 49 | } 50 | 51 | -------------------------------------------------------------------------------- /quality/http-server-test.v1/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "strings" 7 | "testing" 8 | ) 9 | 10 | func Test_myHandler(t *testing.T) { 11 | type args struct { 12 | url string 13 | expectedName string 14 | } 15 | tests := []struct { 16 | name string 17 | args args 18 | }{ 19 | {"simple URL", args{"/json/testing", "testing"}}, 20 | {"simple URL", args{"/json/test/2", "test"}}, 21 | } 22 | for _, tt := range tests { 23 | t.Run(tt.name, func(t *testing.T) { 24 | request, err := http.NewRequest(http.MethodGet, tt.args.url, nil) 25 | if err != nil { 26 | t.Errorf("could not create request for url: %v", tt.args.url) 27 | } 28 | recorder := httptest.NewRecorder() 29 | myHandler(recorder, request) 30 | if !strings.Contains(recorder.Body.String(), tt.args.expectedName) { 31 | t.Errorf("The response does't contain %v", tt.args.expectedName) 32 | } 33 | }) 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /quality/http-server-test.v2/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/http-server-test 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /quality/http-server-test.v2/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | _ "net/http/pprof" 7 | "regexp" 8 | ) 9 | 10 | var re = regexp.MustCompile("^/json/(.*)$") 11 | 12 | type Result struct { 13 | Name string 14 | } 15 | 16 | func myHandler(writer http.ResponseWriter, req *http.Request) { 17 | path := req.URL.Path 18 | 19 | if !re.MatchString(path) { 20 | writer.WriteHeader(http.StatusNotImplemented) 21 | return 22 | } 23 | parts := re.FindStringSubmatch(path) 24 | res := &Result{Name: parts[1]} 25 | bites, err := json.Marshal(*res) 26 | if err != nil { 27 | writer.WriteHeader(500) 28 | return 29 | } 30 | writer.Header().Add("Content-Type", "application/json") 31 | writer.Write(bites) 32 | } 33 | 34 | func main() { 35 | http.HandleFunc("/", myHandler) 36 | http.ListenAndServe(":8080", nil) 37 | } 38 | -------------------------------------------------------------------------------- /quality/http-server-test.v2/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "strings" 7 | "testing" 8 | ) 9 | 10 | func Test_myHandler(t *testing.T) { 11 | type args struct { 12 | url string 13 | expectedName string 14 | status int 15 | } 16 | tests := []struct { 17 | name string 18 | args args 19 | }{ 20 | {"t1", args{"/json/test", "test", http.StatusOK}}, 21 | {"t2", args{"/x/y", "", http.StatusNotImplemented}}, 22 | } 23 | for _, tt := range tests { 24 | t.Run(tt.name, func(t *testing.T) { 25 | url := tt.args.url 26 | req, err := http.NewRequest(http.MethodGet, url, nil) 27 | if err != nil { 28 | t.Errorf("could't create request: %v", tt.args.url) 29 | } 30 | recorder := httptest.NewRecorder() 31 | myHandler(recorder, req) 32 | if recorder.Result().StatusCode != tt.args.status { 33 | t.Errorf("Wrong status code %v, expected %v", recorder.Result().StatusCode, tt.args.status) 34 | } 35 | if !strings.Contains(recorder.Body.String(), tt.args.expectedName) { 36 | t.Errorf("The response does't contain '%v': '%v'", tt.args.expectedName, recorder.Body.String()) 37 | } 38 | }) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /quality/http-server-test.v3/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/http-server-test 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /quality/http-server-test.v3/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | _ "net/http/pprof" 7 | ) 8 | 9 | type Result struct { 10 | Name string 11 | } 12 | 13 | func myHandler(writer http.ResponseWriter, req *http.Request) { 14 | path := req.URL.Path 15 | 16 | if len(path) < 6 || path[:6] != "/json/" { 17 | writer.WriteHeader(http.StatusNotImplemented) 18 | return 19 | } 20 | res := &Result{Name: path[6:]} 21 | bites, err := json.Marshal(*res) 22 | if err != nil { 23 | writer.WriteHeader(500) 24 | return 25 | } 26 | writer.Header().Add("Content-Type", "application/json") 27 | writer.Write(bites) 28 | } 29 | 30 | func main() { 31 | http.HandleFunc("/", myHandler) 32 | http.ListenAndServe(":8080", nil) 33 | } 34 | -------------------------------------------------------------------------------- /quality/http-server-test.v3/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "strings" 7 | "testing" 8 | ) 9 | 10 | func Test_myHandler(t *testing.T) { 11 | type args struct { 12 | url string 13 | expectedName string 14 | status int 15 | } 16 | tests := []struct { 17 | name string 18 | args args 19 | }{ 20 | {"t1", args{"/json/test", "test", http.StatusOK}}, 21 | {"t2", args{"/x/y", "", http.StatusNotImplemented}}, 22 | } 23 | for _, tt := range tests { 24 | t.Run(tt.name, func(t *testing.T) { 25 | url := tt.args.url 26 | req, err := http.NewRequest(http.MethodGet, url, nil) 27 | if err != nil { 28 | t.Errorf("could't create request: %v", tt.args.url) 29 | } 30 | recorder := httptest.NewRecorder() 31 | myHandler(recorder, req) 32 | if recorder.Result().StatusCode != tt.args.status { 33 | t.Errorf("Wrong status code %v, expected %v", recorder.Result().StatusCode, tt.args.status) 34 | } 35 | if !strings.Contains(recorder.Body.String(), tt.args.expectedName) { 36 | t.Errorf("The response does't contain '%v': '%v'", tt.args.expectedName, recorder.Body.String()) 37 | } 38 | }) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /quality/http-server-test/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/http-server-test 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /quality/http-server-test/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | ) 6 | 7 | var jsonDoc1 = []byte("{\"Name\":\"") 8 | var jsonDoc2 = []byte("\"}") 9 | 10 | func myHandler(writer http.ResponseWriter, req *http.Request) { 11 | path := req.URL.Path 12 | 13 | if len(path) < 6 || path[:6] != "/json/" { 14 | writer.WriteHeader(http.StatusNotImplemented) 15 | return 16 | } 17 | 18 | writer.Header().Add("Content-Type", "application/json") 19 | bites := make([]byte, 20) 20 | bites = append(bites, jsonDoc1...) 21 | bites = append(bites, path[6:]...) 22 | bites = append(bites, jsonDoc2...) 23 | writer.Write(bites) 24 | } 25 | 26 | func main() { 27 | http.HandleFunc("/", myHandler) 28 | http.ListenAndServe(":8080", nil) 29 | } 30 | -------------------------------------------------------------------------------- /quality/http-server-test/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "strings" 7 | "testing" 8 | ) 9 | 10 | func Test_myHandler(t *testing.T) { 11 | type args struct { 12 | url string 13 | expectedName string 14 | status int 15 | } 16 | tests := []struct { 17 | name string 18 | args args 19 | }{ 20 | {"t1", args{"/json/test", "test", http.StatusOK}}, 21 | {"t2", args{"/x/y", "", http.StatusNotImplemented}}, 22 | } 23 | for _, tt := range tests { 24 | t.Run(tt.name, func(t *testing.T) { 25 | url := tt.args.url 26 | req, err := http.NewRequest(http.MethodGet, url, nil) 27 | if err != nil { 28 | t.Errorf("could't create request: %v", tt.args.url) 29 | } 30 | recorder := httptest.NewRecorder() 31 | myHandler(recorder, req) 32 | if recorder.Result().StatusCode != tt.args.status { 33 | t.Errorf("Wrong status code %v, expected %v", recorder.Result().StatusCode, tt.args.status) 34 | } 35 | if !strings.Contains(recorder.Body.String(), tt.args.expectedName) { 36 | t.Errorf("The response does't contain '%v': '%v'", tt.args.expectedName, recorder.Body.String()) 37 | } 38 | }) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /quality/unittest/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "golang.source-fellows.com/samples/unittest/math" 7 | ) 8 | 9 | func main() { 10 | res := math.Add(1, 2) 11 | fmt.Printf("Das Ergbnis ist %v", res) 12 | } 13 | -------------------------------------------------------------------------------- /quality/unittest/go.mod: -------------------------------------------------------------------------------- 1 | module golang.source-fellows.com/samples/unittest 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /quality/unittest/math/add.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // Add adds two integer values. 4 | func Add(a int, b int) int { 5 | return a + b 6 | } 7 | -------------------------------------------------------------------------------- /quality/unittest/math/add_test.go: -------------------------------------------------------------------------------- 1 | package math_test 2 | 3 | import ( 4 | "testing" 5 | "time" 6 | 7 | "golang.source-fellows.com/samples/unittest/math" 8 | ) 9 | 10 | func TestAdd(t *testing.T) { 11 | res := math.Add(1, 2) 12 | if res != 3 { 13 | t.Fatalf("Not the expected result %v", res) 14 | } 15 | 16 | } 17 | 18 | var testdata = []struct { 19 | name string 20 | param1 int 21 | param2 int 22 | result int 23 | }{ 24 | {"positiv1", 1, 2, 3}, 25 | {"positiv2", 2, 3, 5}, 26 | {"negativ1", -2, 3, 5}, //stimmt nicht! - zur Demo von Testfehlern 27 | {"negativ2", 2, -3, 7}, //stimmt nicht! - zur Demo von Testfehlern 28 | } 29 | 30 | func TestTableAdd(t *testing.T) { 31 | for _, tt := range testdata { 32 | t.Run(tt.name, func(t *testing.T) { 33 | tt := tt 34 | t.Parallel() 35 | result := math.Add(tt.param1, tt.param2) 36 | time.Sleep(time.Second * 1) 37 | if result != tt.result { 38 | t.Errorf("%v not expected. Expected: %v", result, tt.result) 39 | } 40 | }) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /somelib/README.md: -------------------------------------------------------------------------------- 1 | Sample Golang Project - Version 2 2 | --------------------------------- 3 | 4 | This is a simple Golang library in version 2 to show how to import a library to other projects. 5 | 6 | You can use the library for example in our own application like this: 7 | ``` 8 | package main 9 | 10 | import somelib "github.com/sourcefellows/somelib/v2" 11 | 12 | func main() { 13 | 14 | somelib.CallLibV2() 15 | 16 | } 17 | ``` 18 | 19 | visit: https://www.source-fellows.com -------------------------------------------------------------------------------- /somelib/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/SourceFellows/somelib 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /somelib/somelib.go: -------------------------------------------------------------------------------- 1 | // Package somelib shows how to build a simple golang library. 2 | package somelib 3 | 4 | import "fmt" 5 | 6 | // CallLib simple writes a string to the console. 7 | func CallLib() { 8 | fmt.Println("Here is somelib - version 1.0.0") 9 | } 10 | -------------------------------------------------------------------------------- /somelib/v2/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/SourceFellows/somelib/v2 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /somelib/v2/somelib.go: -------------------------------------------------------------------------------- 1 | // Package somelib shows how to build a simple golang library. 2 | package somelib 3 | 4 | import "fmt" 5 | 6 | // CallLibV2 simple writes a string to the console. 7 | func CallLibV2() { 8 | fmt.Println("Here is somelib - version 2.0.0") 9 | } 10 | -------------------------------------------------------------------------------- /vscode-first/go.mod: -------------------------------------------------------------------------------- 1 | module source-fellows.com/first-vscode 2 | 3 | go 1.13 4 | 5 | require github.com/sirupsen/logrus v1.4.2 6 | -------------------------------------------------------------------------------- /vscode-first/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import log "github.com/sirupsen/logrus" 4 | 5 | func main() { 6 | log.Println("Hello World") 7 | } 8 | --------------------------------------------------------------------------------