diff --git a/backend/go_proto_buffer/main.go b/backend/go_proto_buffer/main.go index 8f3de03..e83c3da 100644 --- a/backend/go_proto_buffer/main.go +++ b/backend/go_proto_buffer/main.go @@ -5,6 +5,15 @@ package main // go get github.com/golang/protobuf //[!] błąd [!] can't load package: package github.com/golang/protobuf: no Go files in C:\Users\dp\go\src\github.com\golang\protobuf //go get github.com/golang/protobuf/proto + +//https://github.com/golang/protobuf +//go get -u github.com/golang/protobuf/protoc-gen-go dziala +//The compiler plugin, protoc-gen-go, will be installed in $GOBIN, +//defaulting to $GOPATH/bin. It must be in your $PATH for the protocol compiler, protoc, to find it. + +//protoc +// 'protoc' is not recognized as an internal or external command, +// operable program or batch file. import "fmt" func main() { diff --git a/backend/go_proto_buffer/person.json b/backend/go_proto_buffer/person.json index efa94fe..a3d2971 100644 --- a/backend/go_proto_buffer/person.json +++ b/backend/go_proto_buffer/person.json @@ -1,4 +1,4 @@ { - "name": "elliot", + "name": "Elliot", "age": 24 } \ No newline at end of file diff --git a/backend/go_proto_buffer/person.proto b/backend/go_proto_buffer/person.proto new file mode 100644 index 0000000..3b3b036 --- /dev/null +++ b/backend/go_proto_buffer/person.proto @@ -0,0 +1,8 @@ +syntax="proto3" + +package main; + +message Person { + string name =1; + int32 age = 2; +} \ No newline at end of file diff --git a/backend/golang-gin/main.go b/backend/golang-gin/main.go new file mode 100644 index 0000000..29e3429 --- /dev/null +++ b/backend/golang-gin/main.go @@ -0,0 +1,81 @@ +package main + +// go get -u github.com/gin-gonic/gin +// go get -u github.com/gin-gonic/contrib/static +// go get "github.com/gin-gonic/contrib/static" +// go get "github.com/gin-gonic/gin" + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/contrib/static" + "github.com/gin-gonic/gin" +) + +// Let's create our Jokes struct. This will contain information about a Joke + +// Joke contains information about a single Joke +type Joke struct { + ID int `json:"id" binding:"required"` + Likes int `json:"likes"` + Joke string `json:"joke" binding:"required"` +} + +// We'll create a list of jokes +var jokes = []Joke{ + Joke{0, 0, "How does a penguin build it's house? Igloos it together."}, + Joke{1, 0, "Did you hear about the restaurant on the moon? Great food, no atmosphere."}, + Joke{2, 0, "What do you call a fake noodle? An Impasta."}, + Joke{3, 0, "How many apples grow on a tree? All of them."}, + Joke{4, 0, "Want to hear a joke about paper? Nevermind it's tearable."}, + Joke{5, 0, "I just watched a program about beavers. It was the best dam program I've ever seen."}, + Joke{6, 0, "Why did the coffee file a police report? It got mugged."}, +} + +func main() { + // Set the router as the default one shipped with Gin + router := gin.Default() + + // Serve frontend static files + router.Use(static.Serve("/", static.LocalFile("./views", true))) + + // Setup route group for the API + api := router.Group("/api") + { + api.GET("/", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "pong", + }) + }) + } + + // Our API will consit of just two routes + // /jokes - which will retrieve a list of jokes a user can see + // /jokes/like/:jokeID - which will capture likes sent to a particular joke + api.GET("/jokes", JokeHandler) + api.POST("/jokes/like/:jokeID", LikeJoke) + + // Start and run the server + router.Run(":3000") +} + +// JokeHandler retrieves a list of available jokes +func JokeHandler(c *gin.Context) { + c.Header("Content-Type", "application/json") + c.JSON(http.StatusOK, jokes) +} + +// LikeJoke increments the likes of a particular joke Item +func LikeJoke(c *gin.Context) { + // confirm Joke ID sent is valid + // remember to import the `strconv` package + //jokeid - wartosc z url'a + if jokeid, err := strconv.Atoi(c.Param("jokeID")); err == nil { + // return a pointer to the updated jokes list + c.JSON(http.StatusOK, &jokes[jokeid]) + } else { + // Joke ID is invalid + c.AbortWithStatus(http.StatusNotFound) + } +} diff --git a/backend/golang-gin/views/app.jsx b/backend/golang-gin/views/app.jsx new file mode 100644 index 0000000..e69de29 diff --git a/backend/golang-gin/views/index.html b/backend/golang-gin/views/index.html new file mode 100644 index 0000000..41b7749 --- /dev/null +++ b/backend/golang-gin/views/index.html @@ -0,0 +1,21 @@ + + + +
+ + +