├── .gitignore ├── AI Bot with Wolfram, Wit.ai and Golang ├── go.mod ├── go.sum └── main.go ├── AWS Lambda With Golang ├── function.zip ├── go.mod ├── go.sum ├── main.go ├── output.txt └── trust-policy.json ├── Build A CRUD API With Golang ├── go.mod ├── go.sum └── main.go ├── Build A Simple Web Server With Golang ├── main.go └── static │ ├── form.html │ └── index.html ├── CRM with Golang Fiber ├── database │ └── database.go ├── go.mod ├── go.sum └── lead │ └── lead.go ├── Complete Serverless Stack ├── build │ └── main.zip ├── cmd │ └── main.go ├── go.mod ├── go.sum └── pkg │ ├── handlers │ ├── api_response.go │ └── handlers.go │ ├── user │ └── user.go │ └── validators │ └── is_email_valid.go ├── Email Verifier Tool With Golang └── main.go ├── Golang Slackbot for File Uploading ├── ZIPL.pdf ├── go.mod ├── go.sum └── main.go ├── Golang With MySQL Book Management System ├── cmd │ └── main.go ├── go.mod ├── go.sum └── pkg │ ├── config │ └── app.go │ ├── controllers │ └── book-controller.go │ ├── models │ └── book.go │ ├── routes │ └── book-routes.go │ └── utils │ └── utils.go ├── HRMS with Golang Fiber ├── go.mod ├── go.sum └── main.go ├── README.md ├── Simple SlackBot To Calculate Age ├── go.mod ├── go.sum └── main.go └── golang.gif /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /AI Bot with Wolfram, Wit.ai and Golang/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/nlp-bot 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/gorilla/websocket v1.4.2 // indirect 7 | github.com/joho/godotenv v1.4.0 // indirect 8 | github.com/krognol/go-wolfram v0.0.0-20180610151123-5b91101b92a8 // indirect 9 | github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a // indirect 10 | github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30 // indirect 11 | github.com/shomali11/slacker v1.3.0 // indirect 12 | github.com/slack-go/slack v0.11.2 // indirect 13 | github.com/tidwall/gjson v1.14.4 // indirect 14 | github.com/tidwall/match v1.1.1 // indirect 15 | github.com/tidwall/pretty v1.2.0 // indirect 16 | github.com/wit-ai/wit-go/v2 v2.0.2 // indirect 17 | ) 18 | -------------------------------------------------------------------------------- /AI Bot with Wolfram, Wit.ai and Golang/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= 4 | github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= 5 | github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= 6 | github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 7 | github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= 8 | github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 9 | github.com/krognol/go-wolfram v0.0.0-20180610151123-5b91101b92a8 h1:ioIdH890e/bUyddMA7ImLvi2hAsZ9RcEhmhxNAbRla0= 10 | github.com/krognol/go-wolfram v0.0.0-20180610151123-5b91101b92a8/go.mod h1:Hik8BfD5DuR157I1QSSL0jHduHF2gjhvFDbUr7Fpe9Q= 11 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 12 | github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a h1:NCmAZOmyqKwf+0KzhY6I6CPndU3qkLRp47RwTyLdMW8= 13 | github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a/go.mod h1:bYyJw/Aj9fK+qoFmRbPJeWsDgq7WGO8f/Qof95qPug4= 14 | github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30 h1:56awf1OXG6Jc2Pk1saojpCzpzkoBvlqecCyNLY+wwkc= 15 | github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30/go.mod h1:O723XwIZBX3FR45rBic/Eyp/DKo/YtchYFURzpUWY2c= 16 | github.com/shomali11/slacker v1.3.0 h1:lKpKaQ2xUdCw6725TyUJUQf1drcMDud0SsQIYUJz7EU= 17 | github.com/shomali11/slacker v1.3.0/go.mod h1:5Rxg5zhDke2QxIx/D1Kt0j6IPfUjF9ZOJnCj7tL6Kto= 18 | github.com/slack-go/slack v0.11.2 h1:IWl90Rk+jqPEVyiBytH27CSN/TFAg2vuDDfoPRog/nc= 19 | github.com/slack-go/slack v0.11.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= 20 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 21 | github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 22 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 23 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 24 | github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= 25 | github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= 26 | github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= 27 | github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= 28 | github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= 29 | github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= 30 | github.com/wit-ai/wit-go/v2 v2.0.2 h1:poO2FW2eQqfb3iMf/54KA7nSTYkLlI7n/wu7bYubomY= 31 | github.com/wit-ai/wit-go/v2 v2.0.2/go.mod h1:3uAerZ/h9riAOquOZc5/qPinCXrzeBO3KL/r208FWos= 32 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 33 | -------------------------------------------------------------------------------- /AI Bot with Wolfram, Wit.ai and Golang/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "fmt" 7 | "log" 8 | "os" 9 | 10 | "github.com/joho/godotenv" 11 | "github.com/krognol/go-wolfram" 12 | "github.com/shomali11/slacker" 13 | "github.com/tidwall/gjson" 14 | 15 | witai "github.com/wit-ai/wit-go/v2" 16 | ) 17 | 18 | var wolframClient *wolfram.Client 19 | 20 | func printCommandEvents(analyticsChannel <-chan *slacker.CommandEvent) { 21 | for event := range analyticsChannel { 22 | fmt.Println("Command Events") 23 | fmt.Println(event.Timestamp) 24 | fmt.Println(event.Command) 25 | fmt.Println(event.Parameters) 26 | fmt.Println(event.Event) 27 | fmt.Println() 28 | } 29 | } 30 | 31 | func main() { 32 | godotenv.Load(".env") 33 | 34 | bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN")) 35 | client := witai.NewClient(os.Getenv("WIT_AI_TOKEN")) 36 | wolframClient := &wolfram.Client{AppID: os.Getenv("WOLFRAM_APP_ID")} 37 | go printCommandEvents(bot.CommandEvents()) 38 | 39 | bot.Command("query for bot - ", &slacker.CommandDefinition{ 40 | Description: "send any question to wolfram", 41 | Example: "who is the president of india", 42 | Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) { 43 | query := request.Param("message") 44 | 45 | msg, _ := client.Parse(&witai.MessageRequest{ 46 | Query: query, 47 | }) 48 | data, _ := json.MarshalIndent(msg, "", " ") 49 | rough := string(data[:]) 50 | value := gjson.Get(rough, "entities.wit$wolfram_search_query:wolfram_search_query.0.value") 51 | answer := value.String() 52 | res, err := wolframClient.GetSpokentAnswerQuery(answer, wolfram.Metric, 1000) 53 | if err != nil { 54 | fmt.Println("there is an error") 55 | } 56 | fmt.Println(value) 57 | response.Reply(res) 58 | }, 59 | }) 60 | 61 | ctx, cancel := context.WithCancel(context.Background()) 62 | defer cancel() 63 | 64 | err := bot.Listen(ctx) 65 | 66 | if err != nil { 67 | log.Fatal(err) 68 | } 69 | } -------------------------------------------------------------------------------- /AWS Lambda With Golang/function.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeHaGiaBao/Eleven-Golang-Projects/b2332b74e9f346d5b6cd8e7b3863526159122c63/AWS Lambda With Golang/function.zip -------------------------------------------------------------------------------- /AWS Lambda With Golang/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/go-lambda-function 2 | 3 | go 1.19 4 | 5 | require github.com/aws/aws-lambda-go v1.37.0 // indirect 6 | -------------------------------------------------------------------------------- /AWS Lambda With Golang/go.sum: -------------------------------------------------------------------------------- 1 | github.com/aws/aws-lambda-go v1.37.0 h1:WXkQ/xhIcXZZ2P5ZBEw+bbAKeCEcb5NtiYpSwVVzIXg= 2 | github.com/aws/aws-lambda-go v1.37.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM= 3 | -------------------------------------------------------------------------------- /AWS Lambda With Golang/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/aws/aws-lambda-go/lambda" 7 | ) 8 | 9 | type MyEvent struct { 10 | Name string `json:"what is your name?"` 11 | Age int `json:"How old are you"` 12 | } 13 | 14 | type MyResponse struct { 15 | Message string `json:"Answer:"` 16 | } 17 | 18 | func HandleLambdaEvent(event MyEvent) (MyResponse, error) { 19 | return MyResponse{Message: fmt.Sprintf("%s is %d years old!", event.Name, event.Age)}, nil 20 | } 21 | 22 | func main() { 23 | lambda.Start(HandleLambdaEvent) 24 | } 25 | -------------------------------------------------------------------------------- /AWS Lambda With Golang/output.txt: -------------------------------------------------------------------------------- 1 | {"Answer:":"Bao is 20 years old!"} -------------------------------------------------------------------------------- /AWS Lambda With Golang/trust-policy.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Effect": "Allow", 6 | "Principal": { 7 | "Service": "lambda.amazonaws.com" 8 | }, 9 | "Action": "sts:AssumeRole" 10 | } 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /Build A CRUD API With Golang/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/crud 2 | 3 | go 1.19 4 | 5 | require github.com/gorilla/mux v1.8.0 // indirect 6 | -------------------------------------------------------------------------------- /Build A CRUD API With Golang/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 2 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /Build A CRUD API With Golang/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "log" 7 | "math/rand" 8 | "net/http" 9 | "strconv" 10 | 11 | "github.com/gorilla/mux" 12 | ) 13 | 14 | type Movie struct { 15 | ID string `json: "id"` 16 | Isbn string `json: "isbn"` 17 | Title string `json: "title"` 18 | Director *Director `json: "director"` 19 | } 20 | 21 | type Director struct { 22 | Firstname string `json: "firstname"` 23 | Lastname string `json: "Lastname` 24 | } 25 | 26 | var movies []Movie 27 | 28 | func getMovies(w http.ResponseWriter, r *http.Request) { 29 | w.Header().Set("Content-Type", "application/json") 30 | json.NewEncoder(w).Encode(movies) 31 | } 32 | 33 | func getMovie(w http.ResponseWriter, r *http.Request) { 34 | w.Header().Set("Content-Type", "application/json") 35 | params := mux.Vars(r) 36 | for _, item := range movies { 37 | if item.ID == params["id"] { 38 | json.NewEncoder(w).Encode(item) 39 | return 40 | } 41 | } 42 | } 43 | 44 | func createMovie(w http.ResponseWriter, r *http.Request) { 45 | w.Header().Set("Content-Type", "application/json") 46 | var movie Movie 47 | _ = json.NewDecoder(r.Body).Decode(&movie) 48 | movie.ID = strconv.Itoa(rand.Intn(100000000)) 49 | movies = append(movies, movie) 50 | json.NewEncoder(w).Encode(movie) 51 | } 52 | 53 | func updateMovie(w http.ResponseWriter, r *http.Request) { 54 | w.Header().Set("Content-Type", "application/json") 55 | params := mux.Vars(r) 56 | for index, item := range movies { 57 | if item.ID == params["id"] { 58 | movies = append(movies[:index], movies[index+1:]...) 59 | var movie Movie 60 | _ = json.NewDecoder(r.Body).Decode(&movie) 61 | movie.ID = params["id"] 62 | movies = append(movies, movie) 63 | json.NewEncoder(w).Encode(movie) 64 | return 65 | } 66 | } 67 | } 68 | 69 | func deleteMovie(w http.ResponseWriter, r *http.Request) { 70 | w.Header().Set("Content-Type", "application/json") 71 | params := mux.Vars(r) 72 | for index, item := range movies { 73 | if item.ID == params["id"] { 74 | movies = append(movies[:index], movies[index+1:]...) 75 | break 76 | } 77 | } 78 | json.NewEncoder(w).Encode(movies) 79 | } 80 | 81 | func main() { 82 | r := mux.NewRouter() 83 | 84 | movies = append(movies, Movie{ID: "1", Isbn: "438227", Title: "Movie One", Director: &Director{Firstname: "John", Lastname: "Doe"}}) 85 | movies = append(movies, Movie{ID: "2", Isbn: "45455", Title: "Movie Two", Director: &Director{Firstname: "Steve", Lastname: "Smith"}}) 86 | 87 | r.HandleFunc("/moives", getMovies).Methods("GET") 88 | r.HandleFunc("/moives/{id}", getMovie).Methods("GET") 89 | r.HandleFunc("/moives", createMovie).Methods("POST") 90 | r.HandleFunc("/moives/{id}", updateMovie).Methods("PUT") 91 | r.HandleFunc("/moives/{id}", deleteMovie).Methods("DELETE") 92 | 93 | fmt.Printf("Starting server at port 8080\n") 94 | log.Fatal(http.ListenAndServe(":8080", r)) 95 | } 96 | -------------------------------------------------------------------------------- /Build A Simple Web Server With Golang/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | ) 8 | 9 | func formHandler(w http.ResponseWriter, r *http.Request) { 10 | if err := r.ParseForm(); err != nil { 11 | fmt.Fprintf(w, "ParseForm() err: %v", err) 12 | return 13 | } 14 | fmt.Fprintf(w, "POST request successful") 15 | name := r.FormValue("name") 16 | address := r.FormValue("address") 17 | fmt.Fprintf(w, "Name = %s\n", name) 18 | fmt.Fprintf(w, "Address = %s\n", address) 19 | } 20 | 21 | func helloHandler(w http.ResponseWriter, r *http.Request) { 22 | if r.URL.Path != "/hello" { 23 | http.Error(w, "404 not found", http.StatusNotFound) 24 | return 25 | } 26 | if r.Method != "GET" { 27 | http.Error(w, "method is not supported", http.StatusNotFound) 28 | return 29 | } 30 | fmt.Fprintf(w, "hello!") 31 | } 32 | 33 | func main() { 34 | fileServer := http.FileServer(http.Dir("./static")) 35 | http.Handle("/", fileServer) 36 | http.HandleFunc("/form", formHandler) 37 | http.HandleFunc("/hello", helloHandler) 38 | 39 | fmt.Printf("Starting server at port 8080\n") 40 | if err := http.ListenAndServe(":8080", nil); err != nil { 41 | log.Fatal(err) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Build A Simple Web Server With Golang/static/form.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Form 8 | 9 | 10 |
11 |
12 | 13 | 14 | 15 |
16 |
17 | 18 | -------------------------------------------------------------------------------- /Build A Simple Web Server With Golang/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Static Website 8 | 9 | 10 |

Static Website

11 | 12 | -------------------------------------------------------------------------------- /CRM with Golang Fiber/database/database.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "github.com/jinzhu/gorm" 5 | _ "github.com/jinzhu/gorm/dialects/sqlite" 6 | ) 7 | 8 | var ( 9 | DBConn *gorm.DB 10 | ) 11 | -------------------------------------------------------------------------------- /CRM with Golang Fiber/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/go-fiber-crm-basic 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/andybalholm/brotli v1.0.0 // indirect 7 | github.com/gofiber/fiber v1.14.6 // indirect 8 | github.com/gofiber/utils v0.0.10 // indirect 9 | github.com/gorilla/schema v1.1.0 // indirect 10 | github.com/jinzhu/gorm v1.9.16 // indirect 11 | github.com/jinzhu/inflection v1.0.0 // indirect 12 | github.com/klauspost/compress v1.10.7 // indirect 13 | github.com/mattn/go-colorable v0.1.7 // indirect 14 | github.com/mattn/go-isatty v0.0.12 // indirect 15 | github.com/valyala/bytebufferpool v1.0.0 // indirect 16 | github.com/valyala/fasthttp v1.16.0 // indirect 17 | github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a // indirect 18 | golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect 19 | ) 20 | -------------------------------------------------------------------------------- /CRM with Golang Fiber/go.sum: -------------------------------------------------------------------------------- 1 | github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= 2 | github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4= 3 | github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= 4 | github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= 5 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= 6 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= 7 | github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 8 | github.com/gofiber/fiber v1.14.6 h1:QRUPvPmr8ijQuGo1MgupHBn8E+wW0IKqiOvIZPtV70o= 9 | github.com/gofiber/fiber v1.14.6/go.mod h1:Yw2ekF1YDPreO9V6TMYjynu94xRxZBdaa8X5HhHsjCM= 10 | github.com/gofiber/utils v0.0.10 h1:3Mr7X7JdCUo7CWf/i5sajSaDmArEDtti8bM1JUVso2U= 11 | github.com/gofiber/utils v0.0.10/go.mod h1:9J5aHFUIjq0XfknT4+hdSMG6/jzfaAgCu4HEbWDeBlo= 12 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= 13 | github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= 14 | github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= 15 | github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= 16 | github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= 17 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= 18 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 19 | github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 20 | github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg= 21 | github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= 22 | github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 23 | github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw= 24 | github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= 25 | github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= 26 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 27 | github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= 28 | github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= 29 | github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= 30 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 31 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 32 | github.com/valyala/fasthttp v1.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ= 33 | github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA= 34 | github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc= 35 | github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= 36 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 37 | golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 38 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 39 | golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 40 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 41 | golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 42 | golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 43 | golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 44 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 45 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 46 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 47 | golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 48 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 49 | golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= 50 | golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 51 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 52 | -------------------------------------------------------------------------------- /CRM with Golang Fiber/lead/lead.go: -------------------------------------------------------------------------------- 1 | package lead 2 | 3 | import ( 4 | "github.com/appak21/11-projects/go-fiber-crm-basic/database" 5 | "github.com/gofiber/fiber" 6 | "github.com/jinzhu/gorm" 7 | _ "github.com/jinzhu/gorm/dialects/sqlite" 8 | ) 9 | 10 | type Lead struct { 11 | gorm.Model 12 | Name string `json:"name"` 13 | Company string `json:"company"` 14 | Email string `json:"email"` 15 | Phone int `json:"phone"` 16 | } 17 | 18 | func GetLeads(c *fiber.Ctx) { 19 | db := database.DBConn 20 | var leads []Lead 21 | db.Find(&leads) 22 | c.JSON(leads) 23 | } 24 | 25 | func GetLead(c *fiber.Ctx) { 26 | id := c.Params("id") 27 | db := database.DBConn 28 | var lead Lead 29 | db.Find(&lead, id) 30 | c.JSON(lead) 31 | } 32 | 33 | func NewLead(c *fiber.Ctx) { 34 | db := database.DBConn 35 | lead := new(Lead) 36 | if err := c.BodyParser(lead); err != nil { 37 | c.Status(503).Send(err) 38 | return 39 | } 40 | db.Create(&lead) 41 | c.JSON(lead) 42 | } 43 | 44 | func DeleteLead(c *fiber.Ctx) { 45 | id := c.Params("id") 46 | db := database.DBConn 47 | var lead Lead 48 | db.First(&lead, id) 49 | if lead.Name == "" { 50 | c.Status(500).Send("No lead found with ID") 51 | return 52 | } 53 | db.Delete(&lead) 54 | c.Send("Lead successfully deleted") 55 | } 56 | -------------------------------------------------------------------------------- /Complete Serverless Stack/build/main.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeHaGiaBao/Eleven-Golang-Projects/b2332b74e9f346d5b6cd8e7b3863526159122c63/Complete Serverless Stack/build/main.zip -------------------------------------------------------------------------------- /Complete Serverless Stack/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/LeHaGiaBao/go-serverless/pkg/handlers" 7 | "github.com/aws/aws-lambda-go/events" 8 | "github.com/aws/aws-lambda-go/lambda" 9 | "github.com/aws/aws-sdk-go/aws" 10 | "github.com/aws/aws-sdk-go/aws/session" 11 | "github.com/aws/aws-sdk-go/service/dynamodb" 12 | "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" 13 | ) 14 | 15 | var( 16 | dynaClient dynamodbiface.DynamoDBAPI 17 | ) 18 | 19 | func main(){ 20 | region := os.Getenv("AWS_REGION") 21 | awsSession, err := session.NewSession(&aws.Config{ 22 | Region: aws.String(region)},) 23 | 24 | if err!=nil{ 25 | return 26 | } 27 | dynaClient = dynamodb.New(awsSession) 28 | lambda.Start(handler) 29 | } 30 | 31 | 32 | const tableName = "go-serverless-yt" 33 | 34 | func handler(req events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error){ 35 | switch req.HTTPMethod{ 36 | case "GET": 37 | return handlers.GetUser(req, tableName, dynaClient) 38 | case "POST": 39 | return handlers.CreateUser(req, tableName, dynaClient) 40 | case "PUT": 41 | return handlers.UpdateUser(req, tableName, dynaClient) 42 | case "DELETE": 43 | return handlers.DeleteUser(req, tableName, dynaClient) 44 | default: 45 | return handlers.UnhandledMethod() 46 | } 47 | } -------------------------------------------------------------------------------- /Complete Serverless Stack/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/go-serverless 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/aws/aws-lambda-go v1.37.0 // indirect 7 | github.com/aws/aws-sdk-go v1.44.191 // indirect 8 | github.com/jmespath/go-jmespath v0.4.0 // indirect 9 | ) 10 | -------------------------------------------------------------------------------- /Complete Serverless Stack/go.sum: -------------------------------------------------------------------------------- 1 | github.com/aws/aws-lambda-go v1.37.0 h1:WXkQ/xhIcXZZ2P5ZBEw+bbAKeCEcb5NtiYpSwVVzIXg= 2 | github.com/aws/aws-lambda-go v1.37.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM= 3 | github.com/aws/aws-sdk-go v1.44.191 h1:GnbkalCx/AgobaorDMFCa248acmk+91+aHBQOk7ljzU= 4 | github.com/aws/aws-sdk-go v1.44.191/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= 5 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 6 | github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= 7 | github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= 8 | github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= 9 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 10 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 11 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 12 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 13 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 14 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 15 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 16 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 17 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 18 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 19 | golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= 20 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 21 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 22 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 23 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 24 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 25 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 26 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 27 | golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 28 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 29 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 30 | golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 31 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 32 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 33 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 34 | golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 35 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 36 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 37 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 38 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 39 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 40 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 41 | -------------------------------------------------------------------------------- /Complete Serverless Stack/pkg/handlers/api_response.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import( 4 | "encoding/json" 5 | "github.com/aws/aws-lambda-go/events" 6 | ) 7 | 8 | func apiResponse(status int, body interface{})(*events.APIGatewayProxyResponse, error){ 9 | resp := events.APIGatewayProxyResponse{Headers: map[string]string{"Content-Type":"application/json"}} 10 | resp.StatusCode = status 11 | 12 | stringBody, _ := json.Marshal(body) 13 | resp.Body = string(stringBody) 14 | return &resp, nil 15 | } -------------------------------------------------------------------------------- /Complete Serverless Stack/pkg/handlers/handlers.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/LeHaGiaBao/go-serverless/pkg/user" 7 | "github.com/aws/aws-lambda-go/events" 8 | "github.com/aws/aws-sdk-go/aws" 9 | "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" 10 | ) 11 | 12 | var ErrorMethodNotAllowed = "method not allowed" 13 | 14 | type ErrorBody struct{ 15 | ErrorMsg *string `json:"error,omitempty"` 16 | } 17 | 18 | func GetUser(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI)( 19 | *events.APIGatewayProxyResponse, error, 20 | ){ 21 | 22 | email := req.QueryStringParameters["email"] 23 | if len(email) > 0 { 24 | result, err := user.FetchUser(email, tableName, dynaClient) 25 | if err!= nil { 26 | return apiResponse(http.StatusBadRequest, ErrorBody{aws.String(err.Error())}) 27 | } 28 | return apiResponse(http.StatusOK, result) 29 | } 30 | 31 | result, err := user.FetchUsers(tableName, dynaClient) 32 | if err!= nil { 33 | return apiResponse(http.StatusBadRequest, ErrorBody{ 34 | aws.String(err.Error()), 35 | }) 36 | } 37 | return apiResponse(http.StatusOK, result) 38 | 39 | } 40 | 41 | func CreateUser(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI)( 42 | *events.APIGatewayProxyResponse, error, 43 | ){ 44 | result, err := user.CreateUser(req, tableName, dynaClient) 45 | if err!=nil { 46 | return apiResponse(http.StatusBadRequest, ErrorBody{ 47 | aws.String(err.Error()), 48 | }) 49 | } 50 | return apiResponse(http.StatusCreated, result) 51 | } 52 | 53 | func UpdateUser(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI)( 54 | *events.APIGatewayProxyResponse, error, 55 | ){ 56 | result, err := user.UpdateUser(req, tableName, dynaClient) 57 | if err!= nil { 58 | return apiResponse(http.StatusBadRequest, ErrorBody{ 59 | aws.String(err.Error()), 60 | }) 61 | } 62 | return apiResponse(http.StatusOK, result) 63 | } 64 | 65 | func DeleteUser(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI)( 66 | *events.APIGatewayProxyResponse, error, 67 | ){ 68 | err := user.DeleteUser(req, tableName, dynaClient) 69 | 70 | if err!= nil { 71 | return apiResponse(http.StatusBadRequest, ErrorBody{ 72 | aws.String(err.Error()), 73 | }) 74 | } 75 | return apiResponse(http.StatusOK, nil) 76 | } 77 | 78 | func UnhandledMethod()(*events.APIGatewayProxyResponse, error){ 79 | return apiResponse(http.StatusMethodNotAllowed, ErrorMethodNotAllowed) 80 | } -------------------------------------------------------------------------------- /Complete Serverless Stack/pkg/user/user.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | 7 | "github.com/LeHaGiaBao/go-serverless/pkg/validators" 8 | "github.com/aws/aws-lambda-go/events" 9 | "github.com/aws/aws-sdk-go/aws" 10 | "github.com/aws/aws-sdk-go/service/dynamodb" 11 | "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" 12 | "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" 13 | ) 14 | 15 | var( 16 | ErrorFailedToUnmarshalRecord = "failed to unmarshal record" 17 | ErrorFailedToFetchRecord = "failed to fetch record" 18 | ErrorInvalidUserData = "invalid user data" 19 | ErrorInvalidEmail = "invalid email" 20 | ErrorCouldNotMarshalItem = "could not marshal item" 21 | ErrorCouldNotDeleteItem = "could not delete item" 22 | ErrorCouldNotDynamoPutItem = "could not dynamo put item" 23 | ErrorUserAlreadyExists = "user.User already exists" 24 | ErrorUserDoesNotExist = "user.User does not exist" 25 | ) 26 | 27 | type User struct{ 28 | Email string `json:"email"` 29 | FirstName string `json:"firstName"` 30 | LastName string `json:"lastName"` 31 | } 32 | 33 | func FetchUser(email, tableName string, dynaClient dynamodbiface.DynamoDBAPI)(*User, error){ 34 | 35 | input := &dynamodb.GetItemInput{ 36 | Key: map[string]*dynamodb.AttributeValue{ 37 | "email":{ 38 | S: aws.String(email), 39 | }, 40 | }, 41 | TableName: aws.String(tableName), 42 | } 43 | 44 | result, err := dynaClient.GetItem(input) 45 | if err!= nil { 46 | return nil, errors.New(ErrorFailedToFetchRecord) 47 | } 48 | 49 | item := new(User) 50 | err = dynamodbattribute.UnmarshalMap(result.Item, item) 51 | if err != nil { 52 | return nil, errors.New(ErrorFailedToUnmarshalRecord) 53 | } 54 | return item, nil 55 | } 56 | 57 | func FetchUsers(tableName string, dynaClient dynamodbiface.DynamoDBAPI)(*[]User, error){ 58 | input := &dynamodb.ScanInput{ 59 | TableName: aws.String(tableName), 60 | } 61 | 62 | result, err := dynaClient.Scan(input) 63 | if err!= nil { 64 | return nil, errors.New(ErrorFailedToFetchRecord) 65 | } 66 | item := new([]User) 67 | err = dynamodbattribute.UnmarshalListOfMaps(result.Items, item) 68 | return item, nil 69 | } 70 | 71 | func CreateUser(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI)( 72 | *User, 73 | error, 74 | ){ 75 | var u User 76 | 77 | if err := json.Unmarshal([]byte(req.Body), &u); err!=nil { 78 | return nil, errors.New(ErrorInvalidUserData) 79 | } 80 | if !validators.IsEmailValid(u.Email){ 81 | return nil, errors.New(ErrorInvalidEmail) 82 | } 83 | 84 | currentUser, _ := FetchUser(u.Email, tableName, dynaClient) 85 | if currentUser != nil && len(currentUser.Email) != 0 { 86 | return nil, errors.New(ErrorUserAlreadyExists) 87 | } 88 | 89 | av, err := dynamodbattribute.MarshalMap(u) 90 | 91 | if err != nil { 92 | return nil, errors.New(ErrorCouldNotMarshalItem) 93 | } 94 | 95 | input := &dynamodb.PutItemInput{ 96 | Item: av, 97 | TableName: aws.String(tableName), 98 | } 99 | 100 | _, err = dynaClient.PutItem(input) 101 | if err != nil { 102 | return nil, errors.New(ErrorCouldNotDynamoPutItem) 103 | } 104 | return &u, nil 105 | } 106 | 107 | func UpdateUser(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI)( 108 | *User, 109 | error, 110 | ){ 111 | var u User 112 | if err := json.Unmarshal([]byte(req.Body), &u); err !=nil { 113 | return nil, errors.New(ErrorInvalidEmail) 114 | } 115 | 116 | currentUser, _ := FetchUser(u.Email, tableName, dynaClient) 117 | if currentUser !=nil && len (currentUser.Email) == 0{ 118 | return nil, errors.New(ErrorUserDoesNotExist) 119 | } 120 | 121 | av, err := dynamodbattribute.MarshalMap(u) 122 | if err != nil { 123 | return nil, errors.New(ErrorCouldNotMarshalItem) 124 | } 125 | 126 | input := &dynamodb.PutItemInput{ 127 | Item: av, 128 | TableName: aws.String(tableName), 129 | } 130 | 131 | _, err = dynaClient.PutItem(input) 132 | if err!=nil { 133 | return nil, errors.New(ErrorCouldNotDynamoPutItem) 134 | } 135 | return &u, nil 136 | } 137 | 138 | func DeleteUser(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI) error{ 139 | 140 | 141 | email := req.QueryStringParameters["email"] 142 | input := &dynamodb.DeleteItemInput{ 143 | Key: map[string]*dynamodb.AttributeValue{ 144 | "email":{ 145 | S: aws.String(email), 146 | }, 147 | }, 148 | TableName: aws.String(tableName), 149 | } 150 | _, err := dynaClient.DeleteItem(input) 151 | if err != nil { 152 | return errors.New(ErrorCouldNotDeleteItem) 153 | } 154 | 155 | return nil 156 | } -------------------------------------------------------------------------------- /Complete Serverless Stack/pkg/validators/is_email_valid.go: -------------------------------------------------------------------------------- 1 | package validators 2 | 3 | import "regexp" 4 | 5 | func IsEmailValid(email string) bool { 6 | var rxEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") 7 | 8 | if len(email) < 3 || len(email) > 254 || !rxEmail.MatchString(email){ 9 | return false 10 | } 11 | 12 | return true 13 | } -------------------------------------------------------------------------------- /Email Verifier Tool With Golang/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "log" 7 | "net" 8 | "os" 9 | "strings" 10 | ) 11 | 12 | func main() { 13 | scanner := bufio.NewScanner(os.Stdin) 14 | fmt.Printf("domain,hasMX,hasSPF,sprRecord,hasDMARC,dmarcRecord\n") 15 | 16 | for scanner.Scan() { 17 | checkDomain(scanner.Text()) 18 | } 19 | 20 | if err := scanner.Err(); err != nil { 21 | log.Fatal("Error: could not read from input: %v\n", err) 22 | } 23 | } 24 | 25 | func checkDomain(domain string) { 26 | 27 | var hasMX, hasSPF, hasDMARC bool 28 | var spfRecord, dmarcRecord string 29 | 30 | mxRecords, err := net.LookupMX(domain) 31 | 32 | if err != nil { 33 | log.Printf("Error: %v\n", err) 34 | } 35 | 36 | if len(mxRecords) > 0 { 37 | hasMX = true 38 | } 39 | 40 | txtRecords, err := net.LookupTXT(domain) 41 | 42 | if err != nil { 43 | log.Printf("Error:%v\n", err) 44 | } 45 | 46 | for _, record := range txtRecords { 47 | if strings.HasPrefix(record, "v=spf1") { 48 | hasSPF = true 49 | spfRecord = record 50 | break 51 | } 52 | } 53 | 54 | dmarcRecords, err := net.LookupTXT("_dmarc." + domain) 55 | if err != nil { 56 | log.Printf("Error:%v\n", err) 57 | } 58 | 59 | for _, record := range dmarcRecords { 60 | if strings.HasPrefix(record, "v=DMARC1") { 61 | hasDMARC = true 62 | dmarcRecord = record 63 | break 64 | } 65 | } 66 | 67 | fmt.Printf("%v, %v, %v, %v, %v, %v", domain, hasMX, hasSPF, spfRecord, hasDMARC, dmarcRecord) 68 | } 69 | -------------------------------------------------------------------------------- /Golang Slackbot for File Uploading/ZIPL.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeHaGiaBao/Eleven-Golang-Projects/b2332b74e9f346d5b6cd8e7b3863526159122c63/Golang Slackbot for File Uploading/ZIPL.pdf -------------------------------------------------------------------------------- /Golang Slackbot for File Uploading/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/slack-file-bot 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/gorilla/websocket v1.4.2 // indirect 7 | github.com/slack-go/slack v0.12.1 // indirect 8 | ) 9 | -------------------------------------------------------------------------------- /Golang Slackbot for File Uploading/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= 3 | github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= 4 | github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= 5 | github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 6 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 7 | github.com/slack-go/slack v0.12.1 h1:X97b9g2hnITDtNsNe5GkGx6O2/Sz/uC20ejRZN6QxOw= 8 | github.com/slack-go/slack v0.12.1/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= 9 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 10 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 11 | -------------------------------------------------------------------------------- /Golang Slackbot for File Uploading/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/slack-go/slack" 8 | ) 9 | 10 | func main() { 11 | os.Setenv("SLACK_BOT_TOKEN", "") 12 | os.Setenv("CHANNEL_ID", "") 13 | api := slack.New(os.Getenv("SLACK_BOT_TOKEN")) 14 | channelArr := []string{os.Getenv("CHANNEL_ID")} 15 | fileArr := []string{"ZIPL.pdf"} 16 | 17 | for i := 0; i < len(fileArr); i++ { 18 | params := slack.FileUploadParameters{ 19 | Channels: channelArr, 20 | File: fileArr[i], 21 | } 22 | file, err := api.UploadFile(params) 23 | if err != nil { 24 | fmt.Printf("%s\n", err) 25 | return 26 | } 27 | fmt.Printf("Name: %s, URL:%s\n", file.Name, file.URL) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net/http" 6 | 7 | "github.com/LeHaGiaBao/go-bookstore/pkg/routes" 8 | "github.com/gorilla/mux" 9 | _ "github.com/jinzhu/gorm/dialects/mysql" 10 | ) 11 | 12 | func main() { 13 | r := mux.NewRouter() 14 | routes.RegisterBookStoreRoutes(r) 15 | http.Handle("/", r) 16 | log.Fatal(http.ListenAndServe("localhost:8080", r)) 17 | } 18 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/go-bookstore 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/go-sql-driver/mysql v1.5.0 // indirect 7 | github.com/gorilla/mux v1.8.0 // indirect 8 | github.com/jinzhu/gorm v1.9.16 // indirect 9 | github.com/jinzhu/inflection v1.0.0 // indirect 10 | ) 11 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/go.sum: -------------------------------------------------------------------------------- 1 | github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= 2 | github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= 3 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= 4 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= 5 | github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= 6 | github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 7 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= 8 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 9 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 10 | github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= 11 | github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= 12 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= 13 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 14 | github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 15 | github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 16 | github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= 17 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 18 | golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 19 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 20 | golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 21 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 22 | golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 23 | golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 24 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 25 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 26 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 27 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 28 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/pkg/config/app.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/jinzhu/gorm" 5 | _ "github.com/jinzhu/gorm/dialects/mysql" 6 | ) 7 | 8 | var ( 9 | db *gorm.DB 10 | ) 11 | 12 | func Connect() { 13 | d, err := gorm.Open("mysql", "lehagiabao:1/simplerest?charset=utf8&parseTime=True&loc=Local") 14 | 15 | if err != nil { 16 | panic(err) 17 | } 18 | 19 | db = d 20 | } 21 | 22 | func GetDB() *gorm.DB { 23 | return db 24 | } 25 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/pkg/controllers/book-controller.go: -------------------------------------------------------------------------------- 1 | package controllers 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "net/http" 7 | "strconv" 8 | 9 | "github.com/LeHaGiaBao/go-bookstore/pkg/models" 10 | "github.com/LeHaGiaBao/go-bookstore/pkg/utils" 11 | "github.com/gorilla/mux" 12 | ) 13 | 14 | var NewBook models.Book 15 | 16 | func GetBook(w http.ResponseWriter, r *http.Request) { 17 | newBooks := models.GetAllBooks() 18 | res, _ := json.Marshal(newBooks) 19 | w.Header().Set("Content-Type", "pkglication/json") 20 | w.WriteHeader(http.StatusOK) 21 | w.Write(res) 22 | } 23 | 24 | func GetBookById(w http.ResponseWriter, r *http.Request) { 25 | vars := mux.Vars(r) 26 | bookId := vars["bookId"] 27 | ID, err := strconv.ParseInt(bookId, 0, 0) 28 | if err != nil { 29 | fmt.Println("error while parsing") 30 | } 31 | bookDetails, _ := models.GetBookById(ID) 32 | res, _ := json.Marshal(bookDetails) 33 | w.Header().Set("Content-Type", "pkglication/json") 34 | w.WriteHeader(http.StatusOK) 35 | w.Write(res) 36 | } 37 | 38 | func CreateBook(w http.ResponseWriter, r *http.Request) { 39 | CreateBook := &models.Book{} 40 | utils.ParseBody(r, CreateBook) 41 | b := CreateBook.CreateBook() 42 | res, _ := json.Marshal(b) 43 | w.WriteHeader(http.StatusOK) 44 | w.Write(res) 45 | } 46 | 47 | func DeleteBook(w http.ResponseWriter, r *http.Request) { 48 | vars := mux.Vars(r) 49 | bookId := vars["bookId"] 50 | ID, err := strconv.ParseInt(bookId, 0, 0) 51 | if err != nil { 52 | fmt.Println("error while parsing") 53 | } 54 | book := models.DeleteBook(ID) 55 | res, _ := json.Marshal(book) 56 | w.Header().Set("Content-Type", "pkglication/json") 57 | w.WriteHeader(http.StatusOK) 58 | w.Write(res) 59 | } 60 | 61 | func UpdateBook(w http.ResponseWriter, r *http.Request) { 62 | var updateBook = &models.Book{} 63 | utils.ParseBody(r, updateBook) 64 | vars := mux.Vars(r) 65 | bookId := vars["bookId"] 66 | ID, err := strconv.ParseInt(bookId, 0, 0) 67 | if err != nil { 68 | fmt.Println("error while parsing") 69 | } 70 | bookDetails, db := models.GetBookById(ID) 71 | if updateBook.Name != "" { 72 | bookDetails.Name = updateBook.Name 73 | } 74 | if updateBook.Author != "" { 75 | bookDetails.Author = updateBook.Author 76 | } 77 | if updateBook.Publication != "" { 78 | bookDetails.Publication = updateBook.Publication 79 | } 80 | db.Save(&bookDetails) 81 | res, _ := json.Marshal(bookDetails) 82 | w.Header().Set("Content-Type", "pkglication/json") 83 | w.WriteHeader(http.StatusOK) 84 | w.Write(res) 85 | } 86 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/pkg/models/book.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | "github.com/LeHaGiaBao/go-bookstore/pkg/config" 5 | "github.com/jinzhu/gorm" 6 | ) 7 | 8 | var db *gorm.DB 9 | 10 | type Book struct { 11 | gorm.Model 12 | Name string `gorm:""json:"name"` 13 | Author string `json:"author"` 14 | Publication string `json:"publication"` 15 | } 16 | 17 | func init() { 18 | config.Connect() 19 | db = config.GetDB() 20 | db.AutoMigrate(&Book{}) 21 | } 22 | 23 | func (b *Book) CreateBook() *Book { 24 | db.NewRecord(b) 25 | db.Create(&b) 26 | return b 27 | } 28 | 29 | func GetAllBooks() []Book { 30 | var Books []Book 31 | db.Find(&Books) 32 | return Books 33 | } 34 | 35 | func GetBookById(Id int64) (*Book, *gorm.DB) { 36 | var getBook Book 37 | db := db.Where("ID=?", Id).Find(&getBook) 38 | return &getBook, db 39 | } 40 | 41 | func DeleteBook(ID int64) Book { 42 | var book Book 43 | db.Where("ID=?", ID).Delete(book) 44 | return book 45 | } 46 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/pkg/routes/book-routes.go: -------------------------------------------------------------------------------- 1 | package routes 2 | 3 | import ( 4 | "github.com/LeHaGiaBao/go-bookstore/pkg/controllers" 5 | "github.com/gorilla/mux" 6 | ) 7 | 8 | var RegisterBookStoreRoutes = func(router *mux.Router) { 9 | router.HandleFunc("/book/", controllers.CreateBook).Methods("POST") 10 | router.HandleFunc("/book/", controllers.GetBook).Methods("GET") 11 | router.HandleFunc("/book/{bookId}", controllers.GetBookById).Methods("GET") 12 | router.HandleFunc("/book/{bookId}", controllers.UpdateBook).Methods("PUT") 13 | router.HandleFunc("/book/{bookId}", controllers.DeleteBook).Methods("DELETE") 14 | } 15 | -------------------------------------------------------------------------------- /Golang With MySQL Book Management System/pkg/utils/utils.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "net/http" 7 | ) 8 | 9 | func ParseBody(r *http.Request, x interface{}) { 10 | if body, err := ioutil.ReadAll(r.Body); err == nil { 11 | if err := json.Unmarshal([]byte(body), x); err != nil { 12 | return 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HRMS with Golang Fiber/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/go-fiber-mongo-hrms 2 | 3 | go 1.19 4 | 5 | 6 | require ( 7 | github.com/gofiber/fiber/v2 v2.36.0 8 | go.mongodb.org/mongo-driver v1.10.1 9 | github.com/andybalholm/brotli v1.0.4 // indirect 10 | github.com/golang/snappy v0.0.1 // indirect 11 | github.com/klauspost/compress v1.15.0 // indirect 12 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect 13 | github.com/pkg/errors v0.9.1 // indirect 14 | github.com/valyala/bytebufferpool v1.0.0 // indirect 15 | github.com/valyala/fasthttp v1.38.0 // indirect 16 | github.com/valyala/tcplisten v1.0.0 // indirect 17 | github.com/xdg-go/pbkdf2 v1.0.0 // indirect 18 | github.com/xdg-go/scram v1.1.1 // indirect 19 | github.com/xdg-go/stringprep v1.0.3 // indirect 20 | github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect 21 | golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect 22 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect 23 | golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect 24 | golang.org/x/text v0.3.7 // indirect 25 | ) -------------------------------------------------------------------------------- /HRMS with Golang Fiber/go.sum: -------------------------------------------------------------------------------- 1 | github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= 2 | github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 3 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/gofiber/fiber/v2 v2.41.0 h1:YhNoUS/OTjEz+/WLYuQ01xI7RXgKEFnGBKMagAu5f0M= 6 | github.com/gofiber/fiber/v2 v2.41.0/go.mod h1:RdebcCuCRFp4W6hr3968/XxwJVg0K+jr9/Ae0PFzZ0Q= 7 | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 8 | github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 9 | github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= 10 | github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= 11 | github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= 12 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 13 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 14 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 15 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 16 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 17 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 18 | github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= 19 | github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 20 | github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= 21 | github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 22 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= 23 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 24 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 25 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= 26 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 27 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 28 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 29 | github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= 30 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 31 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 32 | github.com/valyala/fasthttp v1.43.0 h1:Gy4sb32C98fbzVWZlTM1oTMdLWGyvxR03VhM6cBIU4g= 33 | github.com/valyala/fasthttp v1.43.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY= 34 | github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= 35 | github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= 36 | github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= 37 | github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= 38 | github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= 39 | github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= 40 | go.mongodb.org/mongo-driver v1.11.1 h1:QP0znIRTuL0jf1oBQoAoM0C6ZJfBK4kx0Uumtv1A7w8= 41 | go.mongodb.org/mongo-driver v1.11.1/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8= 42 | golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 43 | golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 44 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 45 | golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= 46 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 47 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 48 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 49 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 50 | golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 51 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= 52 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 53 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 54 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 55 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 56 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 57 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 58 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 59 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 60 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 61 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 62 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 63 | -------------------------------------------------------------------------------- /HRMS with Golang Fiber/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "time" 7 | 8 | "github.com/gofiber/fiber/v2" 9 | "go.mongodb.org/mongo-driver/bson" 10 | "go.mongodb.org/mongo-driver/bson/primitive" 11 | "go.mongodb.org/mongo-driver/mongo" 12 | "go.mongodb.org/mongo-driver/mongo/options" 13 | ) 14 | 15 | type MongoInstance struct { 16 | Client *mongo.Client 17 | Db *mongo.Database 18 | } 19 | 20 | var mg MongoInstance 21 | 22 | const dbName = "fiber-hrms" 23 | const mongoURI = "mongodb://localhost:27017/" + dbName 24 | 25 | type Employee struct { 26 | ID string `json:"id,omitempty" bson:"_id,omitempty"` 27 | Name string `json:"name"` 28 | Salary float64 `json:"salary"` 29 | Age float64 `json:"age"` 30 | } 31 | 32 | func Connect() error { 33 | client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI)) 34 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 35 | defer cancel() 36 | 37 | err = client.Connect(ctx) 38 | db := client.Database(dbName) 39 | 40 | if err != nil { 41 | return err 42 | } 43 | 44 | mg = MongoInstance{ 45 | Client: client, 46 | Db: db, 47 | } 48 | return nil 49 | } 50 | 51 | func main() { 52 | 53 | if err := Connect(); err != nil { 54 | log.Fatal(err) 55 | } 56 | app := fiber.New() 57 | 58 | app.Get("/employee", func(c *fiber.Ctx) error { 59 | 60 | query := bson.D{{}} 61 | 62 | cursor, err := mg.Db.Collection("employees").Find(c.Context(), query) 63 | if err != nil { 64 | return c.Status(500).SendString(err.Error()) 65 | } 66 | 67 | var employees []Employee = make([]Employee, 0) 68 | 69 | if err := cursor.All(c.Context(), &employees); err != nil { 70 | return c.Status(500).SendString(err.Error()) 71 | } 72 | 73 | return c.JSON(employees) 74 | }) 75 | 76 | app.Post("/employee", func(c *fiber.Ctx) error { 77 | collection := mg.Db.Collection("employees") 78 | 79 | employee := new(Employee) 80 | 81 | if err := c.BodyParser(employee); err != nil { 82 | return c.Status(400).SendString(err.Error()) 83 | } 84 | 85 | employee.ID = "" 86 | 87 | insertionResult, err := collection.InsertOne(c.Context(), employee) 88 | 89 | if err != nil { 90 | return c.Status(500).SendString(err.Error()) 91 | } 92 | 93 | filter := bson.D{{Key: "_id", Value: insertionResult.InsertedID}} 94 | createdRecord := collection.FindOne(c.Context(), filter) 95 | 96 | createdEmployee := &Employee{} 97 | createdRecord.Decode(createdEmployee) 98 | 99 | return c.Status(201).JSON(createdEmployee) 100 | 101 | }) 102 | 103 | app.Put("/employee/:id", func(c *fiber.Ctx) error { 104 | idParam := c.Params("id") 105 | 106 | employeeID, err := primitive.ObjectIDFromHex(idParam) 107 | 108 | if err != nil { 109 | return c.SendStatus(400) 110 | } 111 | 112 | employee := new(Employee) 113 | 114 | if err := c.BodyParser(employee); err != nil { 115 | return c.Status(400).SendString(err.Error()) 116 | } 117 | 118 | query := bson.D{{Key: "_id", Value: employeeID}} 119 | update := bson.D{ 120 | {Key: "$set", 121 | Value: bson.D{ 122 | {Key: "name", Value: employee.Name}, 123 | {Key: "age", Value: employee.Age}, 124 | {Key: "salary", Value: employee.Salary}, 125 | }, 126 | }, 127 | } 128 | 129 | err = mg.Db.Collection("employees").FindOneAndUpdate(c.Context(), query, update).Err() 130 | 131 | if err != nil { 132 | if err == mongo.ErrNoDocuments { 133 | return c.SendStatus(400) 134 | } 135 | return c.SendStatus(500) 136 | } 137 | 138 | employee.ID = idParam 139 | 140 | return c.Status(200).JSON(employee) 141 | 142 | }) 143 | 144 | app.Delete("/employee/:id", func(c *fiber.Ctx) error { 145 | 146 | employeeID, err := primitive.ObjectIDFromHex(c.Params("id")) 147 | 148 | if err != nil { 149 | return c.SendStatus(400) 150 | } 151 | 152 | query := bson.D{{Key: "_id", Value: employeeID}} 153 | result, err := mg.Db.Collection("employees").DeleteOne(c.Context(), &query) 154 | 155 | if err != nil { 156 | return c.SendStatus(500) 157 | } 158 | 159 | if result.DeletedCount < 1 { 160 | return c.SendStatus(404) 161 | } 162 | 163 | return c.Status(200).JSON("record deleted") 164 | 165 | }) 166 | 167 | log.Fatal(app.Listen(":3000")) 168 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Go Programming by Building 11 Projects 2 | 3 |
4 | 5 |
6 | 7 | ### The Go programming language is an open-source and compiled language designed by Google. Go is popular for cloud-based or server-side applications. 8 | 9 | ## Here are the projects built using Golang: 10 | 11 | - Build A Simple Web Server With Golang 12 | - Build A CRUD API With Golang 13 | - Golang With MySQL Book Management System 14 | - Simple SlackBot To Calculate Age 15 | - Golang Slackbot for File Uploading 16 | - Email Verifier Tool With Golang 17 | - AWS Lambda With Golang 18 | - CRM with Golang Fiber 19 | - HRMS with Golang Fiber 20 | - Complete Serverless Stack 21 | - AI Bot with Wolfram, Wit.ai and Golang 22 | -------------------------------------------------------------------------------- /Simple SlackBot To Calculate Age/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/LeHaGiaBao/slack-age-bot 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/gorilla/websocket v1.4.2 // indirect 7 | github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a // indirect 8 | github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30 // indirect 9 | github.com/shomali11/slacker v1.3.0 // indirect 10 | github.com/slack-go/slack v0.11.2 // indirect 11 | ) 12 | -------------------------------------------------------------------------------- /Simple SlackBot To Calculate Age/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= 4 | github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= 5 | github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= 6 | github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 7 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 8 | github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a h1:NCmAZOmyqKwf+0KzhY6I6CPndU3qkLRp47RwTyLdMW8= 9 | github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a/go.mod h1:bYyJw/Aj9fK+qoFmRbPJeWsDgq7WGO8f/Qof95qPug4= 10 | github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30 h1:56awf1OXG6Jc2Pk1saojpCzpzkoBvlqecCyNLY+wwkc= 11 | github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30/go.mod h1:O723XwIZBX3FR45rBic/Eyp/DKo/YtchYFURzpUWY2c= 12 | github.com/shomali11/slacker v1.3.0 h1:lKpKaQ2xUdCw6725TyUJUQf1drcMDud0SsQIYUJz7EU= 13 | github.com/shomali11/slacker v1.3.0/go.mod h1:5Rxg5zhDke2QxIx/D1Kt0j6IPfUjF9ZOJnCj7tL6Kto= 14 | github.com/slack-go/slack v0.11.2 h1:IWl90Rk+jqPEVyiBytH27CSN/TFAg2vuDDfoPRog/nc= 15 | github.com/slack-go/slack v0.11.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw= 16 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 17 | github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 18 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 19 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 20 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 21 | -------------------------------------------------------------------------------- /Simple SlackBot To Calculate Age/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "os" 8 | "strconv" 9 | "time" 10 | 11 | "github.com/shomali11/slacker" 12 | ) 13 | 14 | func printCommandEvents(analyticsChannel <-chan *slacker.CommandEvent) { 15 | for event := range analyticsChannel { 16 | fmt.Println("Command Evenets") 17 | fmt.Println(event.Timestamp) 18 | fmt.Println(event.Command) 19 | fmt.Println(event.Parameters) 20 | fmt.Println(event.Event) 21 | fmt.Println() 22 | } 23 | } 24 | 25 | func main() { 26 | os.Setenv("SLACK_BOT_TOKEN", "") 27 | os.Setenv("SLACK_APP_TOKEN", "") 28 | 29 | bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN")) 30 | 31 | go printCommandEvents(bot.CommandEvents()) 32 | 33 | bot.Command("my yob is ", &slacker.CommandDefinition{ 34 | Description: "yob calculator", 35 | Examples: []string{"my yob is 1996"}, 36 | Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) { 37 | year := request.Param("year") 38 | yob, err := strconv.Atoi(year) 39 | if err != nil { 40 | println("error") 41 | } 42 | age := time.Now().Year() - yob 43 | r := fmt.Sprintf("age is %d", age) 44 | response.Reply(r) 45 | }, 46 | }) 47 | 48 | ctx, cancel := context.WithCancel(context.Background()) 49 | defer cancel() 50 | 51 | err := bot.Listen(ctx) 52 | if err != nil { 53 | log.Fatal(err) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /golang.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeHaGiaBao/Eleven-Golang-Projects/b2332b74e9f346d5b6cd8e7b3863526159122c63/golang.gif --------------------------------------------------------------------------------