Last active
February 3, 2022 15:41
-
-
Save naijab/0af91c2ec0910400779dde299c679219 to your computer and use it in GitHub Desktop.
Basic Docker with Golang (iris web framework)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3" | |
services: | |
go-dev: | |
container_name: go-dev | |
build: ./ | |
volumes: | |
- ./src:/usr/src/myapp | |
stdin_open: true | |
tty: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:latest | |
RUN apt-get update && apt-get install -y vim | |
WORKDIR /usr/src/myapp | |
ENV GOPATH=/usr | |
ENV GO111MODULE=on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/kataras/iris" | |
"github.com/thoas/go-funk" | |
) | |
type AccountRequest struct { | |
id int | |
FName string | |
LName string | |
} | |
var accountList = []AccountRequest{} | |
func main() { | |
app := iris.Default() | |
app.Get("/users", GetAllUser) | |
app.Get("/user/{id:int}", GetUserByID) | |
app.Post("/user", CreateUser) | |
app.Run(iris.Addr(":8080")) | |
} | |
func GetAllUser(ctx iris.Context) { | |
ctx.JSON(iris.Map{"data": accountList}) | |
} | |
func GetUserByID(ctx iris.Context) { | |
id := ctx.Params().GetIntDefault("id", 0) | |
user := funk.Find(accountList, func(a AccountRequest) bool { | |
return id == a.id | |
}) | |
if user == nil { | |
ctx.JSON(iris.Map{"error": "user not found!"}) | |
return | |
} | |
ctx.JSON(user) | |
} | |
func CreateUser(ctx iris.Context) { | |
var userInput AccountRequest | |
ctx.ReadJSON(&userInput) | |
userInput.id = len(accountList) + 1 | |
accountList = append(accountList, userInput) | |
user := accountList[len(accountList)-1] | |
ctx.JSON(iris.Map{ | |
"status": "success", | |
"ID": user.id, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment