Skip to content

mat-ryer #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package main

import (
"github.com/simonhammes/getting-started-with-go/pkg"
"log"
"net/http"

"github.com/simonhammes/getting-started-with-go/pkg/handlers"
)

/* type server struct {
// db *someDatabase
router *mux.Router
// cache
}*/

func main() {
port := ":8080"
http.HandleFunc("/users", handlers.UsersHandler)
log.Fatal(http.ListenAndServe(port, nil))
err := run()

if err != nil {
log.Fatal(err)
}
}

func run() error {
s := pkg.NewServer()

err := http.ListenAndServe(":8080", s)

return err
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/simonhammes/getting-started-with-go

go 1.18

require github.com/go-redis/redis/v8 v8.11.5
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/gorilla/mux v1.8.0
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
Expand Down
41 changes: 41 additions & 0 deletions pkg/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pkg

import (
"github.com/simonhammes/getting-started-with-go/pkg/cache"
"io/ioutil"
"net/http"
"time"
)

func (s *server) handleGetCacheItem() http.HandlerFunc {
// thing := prepareThing()
return func(w http.ResponseWriter, r *http.Request) {
connection := cache.GetRedisConnection()
result, err := connection.Get(r.Context(), "user").Result()
if err != nil {
http.Error(w, "Error!", 404)
return
}
w.Write([]byte(result))
}
}

func (s server) handleSetCacheItem() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Internal server error", 500)
return
}
defer r.Body.Close()

connection := cache.GetRedisConnection()
_, err = connection.SetNX(r.Context(), "user", body, 300*time.Second).Result()
if err != nil {
http.Error(w, "Internal server error", 500)
return

}
w.Write(body)
}
}
6 changes: 6 additions & 0 deletions pkg/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pkg

func (s *server) routes() {
s.router.HandleFunc("/cache", s.handleGetCacheItem()).Methods("GET")
s.router.HandleFunc("/cache", s.handleSetCacheItem()).Methods("POST")
}
27 changes: 27 additions & 0 deletions pkg/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pkg

import (
"github.com/gorilla/mux"
"net/http"
)

type server struct {
// db *someDatabase
router *mux.Router
// cache
}

func NewServer() *server {
s := &server{
router: mux.NewRouter(),
}

// Setup routing
s.routes()

return s
}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}