Skip to content

Commit 60b8924

Browse files
committed
basic structure
1 parent d8dcde3 commit 60b8924

File tree

14 files changed

+248
-142
lines changed

14 files changed

+248
-142
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.22.2-alpine
2+
WORKDIR /app
3+
COPY go.mod go.sum ./
4+
RUN go mod tidy
5+
COPY . .
6+
RUN go build -o main ./main.go
7+
RUN chmod +x main
8+
EXPOSE 4040
9+
CMD [ "./main" ]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# go-app

go.mod

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
1-
module snipd-server/web-service-gin
1+
module github.com/integrationninjas/go-app
22

3-
go 1.22.4
3+
go 1.22.2
44

5-
require github.com/gin-gonic/gin v1.10.0
6-
7-
require (
8-
github.com/bytedance/sonic v1.11.6 // indirect
9-
github.com/bytedance/sonic/loader v0.1.1 // indirect
10-
github.com/cloudwego/base64x v0.1.4 // indirect
11-
github.com/cloudwego/iasm v0.2.0 // indirect
12-
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
13-
github.com/gin-contrib/sse v0.1.0 // indirect
14-
github.com/go-playground/locales v0.14.1 // indirect
15-
github.com/go-playground/universal-translator v0.18.1 // indirect
16-
github.com/go-playground/validator/v10 v10.20.0 // indirect
17-
github.com/goccy/go-json v0.10.2 // indirect
18-
github.com/json-iterator/go v1.1.12 // indirect
19-
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
20-
github.com/leodido/go-urn v1.4.0 // indirect
21-
github.com/mattn/go-isatty v0.0.20 // indirect
22-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
23-
github.com/modern-go/reflect2 v1.0.2 // indirect
24-
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
25-
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
26-
github.com/ugorji/go/codec v1.2.12 // indirect
27-
golang.org/x/arch v0.8.0 // indirect
28-
golang.org/x/crypto v0.23.0 // indirect
29-
golang.org/x/net v0.25.0 // indirect
30-
golang.org/x/sys v0.20.0 // indirect
31-
golang.org/x/text v0.15.0 // indirect
32-
google.golang.org/protobuf v1.34.1 // indirect
33-
gopkg.in/yaml.v3 v3.0.1 // indirect
34-
)
5+
require github.com/gofor-little/env v1.0.17

go.sum

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,10 @@
1-
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
2-
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
3-
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
4-
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
5-
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
6-
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
7-
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
8-
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
102
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11-
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
12-
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
13-
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
14-
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
15-
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
16-
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
17-
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
18-
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
19-
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
20-
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
21-
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
22-
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
23-
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
24-
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
25-
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
26-
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
27-
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
28-
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
29-
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
30-
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
31-
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
32-
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
33-
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
34-
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
35-
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
36-
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
37-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
38-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
39-
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
40-
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
41-
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
42-
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
3+
github.com/gofor-little/env v1.0.17 h1:TAB4jwL11nH59mg+4+6uycWY8Wc7MVe7Ip2uSmT0v0Q=
4+
github.com/gofor-little/env v1.0.17/go.mod h1:2BE2i6c9e/C6EaGnfhpqzfNERUqkzJ+s/ApnRyl+588=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
436
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
44-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
45-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
46-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
47-
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
48-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
49-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
50-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
51-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
52-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
53-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
548
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
55-
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
56-
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
57-
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
58-
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
59-
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
60-
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
61-
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
62-
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
63-
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
64-
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
65-
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
66-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
67-
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
68-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
69-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
70-
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
71-
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
72-
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
73-
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
74-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
75-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
769
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7710
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
78-
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
79-
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

handlers/hello.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
8+
"github.com/integrationninjas/go-app/models"
9+
)
10+
11+
func HelloHandler(w http.ResponseWriter, r *http.Request) {
12+
message := models.Message{Text: "Hello from GoLang API!"}
13+
encodeJSON(w, message)
14+
}
15+
16+
func encodeJSON(w http.ResponseWriter, data interface{}) {
17+
w.Header().Set("Content-Type", "application/json")
18+
err := json.NewEncoder(w).Encode(data)
19+
if err != nil {
20+
log.Println("Error encoding data:", err)
21+
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
22+
return
23+
}
24+
}

handlers/items.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/integrationninjas/go-app/models"
7+
)
8+
9+
func ItemsHandler(w http.ResponseWriter, r *http.Request) {
10+
items := []models.Item{
11+
{ID: 1, Name: "Book", Price: 10.99},
12+
{ID: 2, Name: "Pen", Price: 2.50},
13+
{ID: 3, Name: "Headphones", Price: 49.99},
14+
}
15+
encodeJSON(w, items)
16+
}

handlers/randomuser.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
9+
"github.com/integrationninjas/go-app/models"
10+
)
11+
12+
const randomUserAPIURL = "https://randomuser.me/api/"
13+
14+
func GetRandomUser(w http.ResponseWriter, r *http.Request) {
15+
userData, err := fetchRandomUser()
16+
if err != nil {
17+
w.WriteHeader(http.StatusInternalServerError)
18+
fmt.Fprintf(w, "Error fetching user data: %v", err)
19+
return
20+
}
21+
22+
encodeJSON(w, userData.Results[0]) // Encode and return the first user data
23+
}
24+
25+
func fetchRandomUser() (models.UserData, error) {
26+
resp, err := http.Get(randomUserAPIURL)
27+
if err != nil {
28+
return models.UserData{}, err
29+
}
30+
defer resp.Body.Close()
31+
32+
body, err := io.ReadAll(resp.Body)
33+
if err != nil {
34+
return models.UserData{}, err
35+
}
36+
37+
var userData models.UserData
38+
err = json.Unmarshal(body, &userData)
39+
if err != nil {
40+
return models.UserData{}, err
41+
}
42+
43+
return userData, nil
44+
}

main

7.76 MB
Binary file not shown.

main.go

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
11
package main
22

33
import (
4-
"net/http"
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
58

6-
"github.com/gin-gonic/gin"
9+
"github.com/gofor-little/env"
10+
"github.com/integrationninjas/go-app/handlers"
711
)
812

913
func main() {
10-
router := gin.Default()
11-
router.GET("/albums", getAlbums)
12-
router.POST("/albums", postAlbums)
13-
14-
router.Run("localhost:8080")
14+
// load .env file
15+
// godotenv package
16+
port := goDotEnvVariable("PORT")
17+
if port == "" {
18+
port = "8080" // Default port
19+
}
20+
21+
// Initialize logging
22+
log.SetOutput(os.Stdout)
23+
log.SetFlags(log.LstdFlags | log.Lshortfile)
24+
25+
// Register API handlers
26+
http.HandleFunc("/", handlers.HelloHandler)
27+
http.HandleFunc("/items", handlers.ItemsHandler)
28+
http.HandleFunc("/randomuser", handlers.GetRandomUser)
29+
30+
fmt.Println("Server listening on port", port)
31+
log.Fatal(http.ListenAndServe(":"+port, nil))
1532
}
1633

17-
type album struct {
18-
ID string `json:"id"`
19-
Title string `json:"title"`
20-
Artist string `json:"artist"`
21-
Price float64 `json:"price"`
22-
}
34+
// use godot package to load/read the .env file and
35+
// return the value of the key
36+
func goDotEnvVariable(key string) string {
2337

24-
var albums = []album{
25-
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
26-
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
27-
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
28-
}
38+
// load .env file
39+
err := env.Load(".env")
2940

30-
// getAlbums responds with the list of all albums as JSON.
31-
func getAlbums(c *gin.Context) {
32-
c.IndentedJSON(http.StatusOK, albums)
33-
}
41+
if err != nil {
42+
log.Fatalf("Error loading .env file")
43+
}
3444

35-
// postAlbums adds an album from JSON received in the request body.
36-
func postAlbums(c *gin.Context) {
37-
var newAlbum album
38-
39-
// Call BindJSON to bind the received JSON to
40-
// newAlbum.
41-
if err := c.BindJSON(&newAlbum); err != nil {
42-
return
43-
}
44-
45-
// Add the new album to the slice.
46-
albums = append(albums, newAlbum)
47-
c.IndentedJSON(http.StatusCreated, newAlbum)
45+
return os.Getenv(key)
4846
}
49-

0 commit comments

Comments
 (0)