Clean way for same endpoints but with different API versions - rest

I'm looking for a clean way to maintain two versions of an API that have the same endpoints.
Right now, the easiest way but seems excessive is to have something like
r := chi.NewRouter()
r.Get("/test", func(w http.ResponseWriter, r *http.Request) {
version := r.Header.Get("Accept-version")
if version == "v1" {
w.Write([]byte("version 1 of api"))
} else {
w.Write([]byte("other version of api"))
}
})
but when you have a couple dozen+ or so endpoints... can get messy
The way that I would like to have it is have a middleware that will fallthrough to the next defined route. So something like
r := chi.NewRouter()
r.With(UseVersion1).Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("version 1 of api"))
}
})
r.With(UseVersion2).Get("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("other version of api"))
}
})
Is this even possible? Or can someone suggest a better route (pun not intended)
EDIT: I know that prefixing the path is a viable option. I would like to avoid that

alexmac is right Usually we uses prefix, also, you can uses midleware if you do't want to add prefix(ps:Not recommended)
your code have a problem, one url can't use many midleware
follow is a example in gin, in chi , you can write yourself code:
package main
import (
"github.com/gin-gonic/gin"
)
func Version() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.RequestURI[:2] != "/v" {
version := c.GetHeader("Accept-version")
if version == ""{
version = "v2"
}
c.Redirect(302, "/" + version + c.Request.RequestURI)
c.Abort()
}else{
c.Next()
}
}
}
func main() {
r := gin.New()
r.Use(Version())
v1 := r.Group("v1")
v2 := r.Group("v2")
v1.GET("/test", func (c *gin.Context){
c.String(200, "v1 test")
})
v2.GET("/test", func (c *gin.Context){
c.String(200, "v2 test")
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
if you don't want to chagne the url you can:
package main
import (
"github.com/gin-gonic/gin"
)
var(
r *gin.Engine
)
func Version() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.URL.Path[:2] != "/v" {
version := c.GetHeader("Accept-version")
if version == ""{
version = "v2"
}
c.Request.URL.Path = "/" + version + c.Request.RequestURI
r.HandleContext(c)
c.Abort()
}
c.Next()
}
}
func main() {
r = gin.New()
r.Use(Version())
v1 := r.Group("v1")
v2 := r.Group("v2")
v1.GET("/test", func (c *gin.Context){
c.String(200, "v1/test")
})
v2.GET("/test", func (c *gin.Context){
c.String(200, "v2/test")
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}

Related

How to use an optional query parameter in Go gorilla/mux?

Probably there is already a solution here to my problem, but I couldn't find it anywhere. I tried a bunch of stuff, but nothing worked so far.
I have something like this:
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func HealthCheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "Healthy")
// Also print the value of 'foo'
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/health-check", HealthCheck).Methods("GET").Queries("foo", "{foo}").Name("HealthCheck")
r.HandleFunc("/health-check", HealthCheck).Methods("GET")
http.ListenAndServe(":8080", r)
}
What I'm trying to achieve:
curl http://localhost:8080/health-check
Should respond with: Healthy <foo> ( -> the default value of foo)
And also the following:
curl http://localhost:8080/health-check?foo=bar
Should respond with: Healthy bar
One solution if to simply handle the query params in your handler:
package main
import (
"net/http"
"github.com/gorilla/mux"
)
func HealthCheckHandler(w http.ResponseWriter, req *http.Request) {
values := req.URL.Query()
foo := values.Get("foo")
if foo != "" {
w.Write([]byte("Healthy " + foo))
} else {
w.Write([]byte("Healthy <foo>"))
}
w.WriteHeader(http.StatusOK)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/health-check", HealthCheckHandler)
http.ListenAndServe(":8080", r)
}
according to the gorilla/mux documentation, the Queries method is meant to match your handler to specific functions, akin to a regular expression.

How do I place the AVG of a row into a new column?

Hey everyone I am using golang to build a super simple API. I have this json data being passed from a POST request and being stored in my DB. I would like to take the tts data which is an integer array and average that array and place it in the ttc column and return that number on the json response. I am having a hard time doing that any help would be greatly appreciated. My source code is below as well as my DB Model. I know I would have to use the AVG() function somehow in postgres but I am brand new to postgres so I am super confused.
main.go
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"github.com/rs/cors"
"log"
"net/http"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type Resource struct {
gorm.Model
Name string
TTS pq.Int64Array `gorm:"type:integer[]"`
TTC int
}
var db *gorm.DB
var err error
func main() {
router := mux.NewRouter()
db, err = gorm.Open(
"postgres",
"host=localhost"+" user=postgres"+
" dbname=Shoes"+" sslmode=disable password=root")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
db.AutoMigrate(&Resource{})
router.HandleFunc("/resources", GetResources).Methods("GET")
router.HandleFunc("/resources/{id}", GetResource).Methods("GET")
router.HandleFunc("/resources", CreateResource).Methods("POST")
router.HandleFunc("/resources/{id}", DeleteResource).Methods("DELETE")
handler := cors.Default().Handler(router)
log.Fatal(http.ListenAndServe(":8080", handler))
}
func GetResources(w http.ResponseWriter, r *http.Request) {
var resources []Resource
db.Find(&resources)
json.NewEncoder(w).Encode(&resources)
}
func GetResource(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var resource Resource
db.First(&resource, params["id"])
json.NewEncoder(w).Encode(&resource)
}
func CreateResource(w http.ResponseWriter, r *http.Request) {
var resource Resource
json.NewDecoder(r.Body).Decode(&resource)
db.Create(&resource)
json.NewEncoder(w).Encode(&resource)
}
func DeleteResource(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var resource Resource
db.First(&resource, params["id"])
db.Delete(&resource)
var resources []Resource
db.Find(&resources)
json.NewEncoder(w).Encode(&resources)
}
I am thinking I could do something like
db.Select("AVG(tts)")
I am just not sure how to put that result in the column ttc
Since the json of the post request already contains the tts_data, you can get the average before setting it in the database
sum := 0
for _, i := range tts_data {
sum += i
}
avg := sum / len(tts_data)
// save the data in your db
rs := Ressource{Name: "name", TTS: tts_data, ttc: avg}
b := db.Create(&rs)
if b {
// send all the resource
json.NewEncoder(w).Encode(&rs)
// or send only the avg
json.NewEncoder(w).Encode(struct{avg: int}{avg: avg})
} else {
// handle error
}

http.Request r.FormValue returns nothing/map[]

I have the following Go code:
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"log"
"net/http"
"io/ioutil"
)
type rLog struct {
Method string
URI string
FormParam string
}
func commonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
formBs, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalf("Failed to decode postFormByteSlice: %v", err)
}
rl := rLog{Method: r.Method, URI: r.RequestURI, FormParam: string(formBs)}
log.Printf("%+v", rl)
next.ServeHTTP(w, r)
})
}
func main() {
port := ":3000"
var router = mux.NewRouter()
router.Use(commonMiddleware)
router.HandleFunc("/m/{msg}", handleMessage).Methods("GET")
router.HandleFunc("/n/", handleNumber).Methods("POST")
headersOk := handlers.AllowedHeaders([]string{"Authorization"})
originsOk := handlers.AllowedOrigins([]string{"*"})
methodsOk := handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"})
fmt.Printf("Server is running at http://localhost%s\n", port)
log.Fatal(http.ListenAndServe(port, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
}
func handleMessage(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
message := vars["msg"]
response := map[string]string{"message": message}
json.NewEncoder(w).Encode(response)
}
func handleNumber(w http.ResponseWriter, r *http.Request) {
log.Println(r.FormValue("name")) // this returns nothing
response := map[string]string{"name": "1"} // dummy response
json.NewEncoder(w).Encode(response)
}
What I try to do here is very simple.
I want to log all POST form data (it's done inside the commonMiddleware)
Access the form data inside the handleNumber i.e. name (and many more later) and do some logic with it.
The problem that I have right now is, the log.Println(r.FormValue("name")) returns nothing and I really wonder why that happened.
I've tried adding r.ParseForm() before log.Println(r.FormValue("name")). But it didn't work.
And, when I add log.Println(r.Form) line, it returns map[].
What did I missed here?
You truing to read r.Body twice, first in commonMiddleware with ioutil.ReadAll(r.Body) and then in handleNumber with r.ParseForm(). You can't. It's io.Reader, you can't read it two times. You can instead, for example, r.ParseForm() in middlewear and then use parsed form data. In middlewear r.PosrForm.Encode() to log, and in handler r.FormValue() or r.Form.Get() to extract. I think something like this should do
func commonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
err:=r.ParseForm() //parse in middleware, data will be contained in r.PostForm
if err != nil {
log.Fatalf("Failed to decode postFormByteSlice: %v", err)
}
rl := rLog{Method: r.Method, URI: r.RequestURI, FormParam: r.PostForm.Encode()} //url.Values.Encode() stringifys form data
log.Printf("%+v", rl)
next.ServeHTTP(w, r)
})
}
func main() {
port := ":3000"
var router = mux.NewRouter()
router.Use(commonMiddleware)
router.HandleFunc("/m/{msg}", handleMessage).Methods("GET")
router.HandleFunc("/n/", handleNumber).Methods("POST")
}
func handleNumber(w http.ResponseWriter, r *http.Request) {
log.Println(r.PostForm.Get("name")) // or just r.Form.Get("name") or r.FormValue("name")
response := map[string]string{"name": "1"} // dummy response
json.NewEncoder(w).Encode(response)
}

Generic REST API Golang

Searching SO for Generic REST API Golang gives 0 results. Searching Google gives 2 results. So this question is maybe not correctly formulated or it is impossible to achieve in Golang.
My goal is to avoid repeating similar code over and over again. So I am trying to make the code in Golang as generic as possible. Write once, use many.
This is my first attempt to create a generic REST API for select in Golang. The code below gives almost what I want:
But the result is presented in the Terminal. I have no idea how to redirect the result to the browser.
package main
import (
"fmt"
"log"
"net/http"
"database/sql"
"time"
_ "github.com/lib/pq"
)
var db *sql.DB
func main() {
Connect()
http.HandleFunc("/", Query)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func Connect() {
const (
host = "127.0.0.1"
port = 5432
user = "test"
password = "test"
dbname = "Test")
login := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=require", host, port, user, password, dbname)
var err error
db, err = sql.Open("postgres", login)
if err != nil {
log.Fatalln(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
func Query(w http.ResponseWriter, r *http.Request) {
var query string
switch r.URL.String() {
case "/getuser":
query = "select * from getuser()"
case "/getco":
query = "select * from getco()"
case "/etc"
query = "select * from etc"
default:
query = ""
}
var err error
var rows *sql.Rows
rows, err = db.Query(query)
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
defer rows.Close()
cols, err := rows.Columns()
vals := make([]interface{}, len(cols))
for i := 0; i < len(cols); i++ {
vals[i] = new(interface{})
if i != 0 {
fmt.Print("\t")
}
fmt.Print(cols[i])
}
fmt.Println()
for rows.Next() {
err = rows.Scan(vals...)
if err != nil {
fmt.Println(err)
continue
}
for i := 0; i < len(vals); i++ {
if i != 0 {
fmt.Print("\t")
}
printValue(vals[i].(*interface{}))
}
fmt.Println()
}
func printValue(pval *interface{}) {
switch v := (*pval).(type) {
case nil:
fmt.Print("NULL")
case bool:
if v {
fmt.Print("1")
} else {
fmt.Print("0")
}
case []byte:
fmt.Print(string(v))
case time.Time:
fmt.Print(v.Format("2006-01-02"))
default:
fmt.Print(v)
}
}
Every attempt to write to the browser gives various type of errors:
fmt.Printf("%s\n", vals...)
My questions are
How do I redirect the result to the browser?
Is there any better way to achieve this? (reuse generic code)
My recommendation would be to look at using existing packages like "mux" for calling REST APIs in browser. As a quick demo how you would do it as as follows:
your restapi.go cound have APIs as follows:
func SampleAPI(w http.ResponseWriter, r *http.Request) { //Assuming this is a POST request
var example SomeSruct
_ = json.NewDecoder(r.Body).Decode(&example) //Decode the POST body
result := someLogicFunction(example) //call your generic function
json.NewEncoder(w).Encode(result) //encode the result to pass it back to browser
}
Now say you write a main.go and you are using mux package here is an example of how you would call this
main.go
func main() {
router := mux.NewRouter()
router.HandleFunc("/testFunc",restapi.SampleAPI).Methods("POST") //This creates the route for your http request
handler := cros.Default().Handler(router) //You will need this if you plan to deploy it in a server and call it externally for testing locally you don't need this
log.Fatal(http.ListenAndServe(":8080", handler)) //Port that the router is listening to
}
Now note that you will have to import the "github.com/gorilla/mux" and the "github.com/rs/cors" packages to use these but this way you can create REST APIs whic can be accessed by te browser. Similarly you could create a GET method and use parameters which you can grab in your function and perform any logical step.
If you build and install the above code you can POST to localhost:8080/testFunc over http using any web app and get results i your browser. If you had a GET request you could directly type the Url in the browser and see the result.
write response with appropriahe HTTP hearers && status code
import "net/http"
func writeResponse(w http.ResponseWriter, contents []byte) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, contents)
}
sounds a little unclear, sorry

too many open files in mgo go server

I'm getting these errors in the logs:
Accept error: accept tcp [::]:80: accept4: too many open files;
for a mongodb server on ubuntu, written in go using mgo. They start appearing after it's been running for about a day.
code:
package main
import (
"encoding/json"
"io"
"net/http"
"gopkg.in/mgo.v2/bson"
)
var (
Database *mgo.Database
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello")
}
func setTile(w http.ResponseWriter, r *http.Request) {
var requestJSON map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&requestJSON)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
collection := Database.C("tiles")
if requestJSON["tileId"] != nil {
query := bson.M{"tileId": requestJSON["tileId"]}
collection.RemoveAll(query)
collection.Insert(requestJSON)
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
js, _ := json.Marshal(map[string]string{"result": "ok"})
w.Write(js)
} else {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
}
func getTile(w http.ResponseWriter, r *http.Request) {
var requestJSON map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&requestJSON)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
collection := Database.C("tiles")
var result []map[string]interface{}
if requestJSON["tileId"] != nil {
query := bson.M{"tileId": requestJSON["tileId"]}
collection.Find(query).All(&result)
}
if len(result) > 0 {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
js, _ := json.Marshal(result[0])
w.Write(js)
} else {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
js, _ := json.Marshal(map[string]string{"result": "tile id not found"})
w.Write(js)
}
}
func main() {
session, _ := mgo.Dial("localhost")
Database = session.DB("mapdb")
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
mux.HandleFunc("/setTile", setTile)
mux.HandleFunc("/getTile", getTile)
http.ListenAndServe(":80", mux)
}
Is there something in there that needs closing? Or is it structured wrong in some way?
There seems to be lots of places to set the open file limits, so i'm not sure how to find out what the limits actually are. But it seems like increasing the limit isn't the problem anyway, surely something is being opened on every request and not closed.
This is not how you store and use a MongoDB connection in Go.
You have to store an mgo.Session, not an mgo.Database instance. And whenever you need to interact with the MongoDB, you acquire a copy or a clone of the session (e.g. with Session.Copy() or Session.Clone()), and you close it when you don't need it (preferable using a defer statement). This will ensure you don't leak connections.
You also religiously omit checking for errors, please don't do that. Whatever returns an error, do check it and act on it properly (the least you can do is print / log it).
So basically what you need to do is something like this:
var session *mgo.Session
func init() {
var err error
if session, err = mgo.Dial("localhost"); err != nil {
log.Fatal(err)
}
}
func someHandler(w http.ResponseWriter, r *http.Request) {
sess := session.Copy()
defer sess.Close() // Must close!
c := sess.DB("mapdb").C("tiles")
// Do something with the collection, e.g.
var tile bson.M
if err := c.FindId("someTileID").One(&result); err != nil {
// Tile does not exist, send back error, e.g.:
log.Printf("Tile with ID not found: %v, err: %v", "someTileID", err)
http.NotFound(w, r)
return
}
// Do something with tile
}
See related questions:
mgo - query performance seems consistently slow (500-650ms)
Concurrency in gopkg.in/mgo.v2 (Mongo, Go)
You are missing:
defer r.Body.Close()
Make sure it is used before return statement.