Skip to content
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

TarsGo Can not implement RESTful API #190

Closed
somnmos opened this issue Apr 22, 2020 · 1 comment
Closed

TarsGo Can not implement RESTful API #190

somnmos opened this issue Apr 22, 2020 · 1 comment

Comments

@somnmos
Copy link

somnmos commented Apr 22, 2020

I found TarsGo Use the http package to dispatch request, but the http package can not implement RESTfull API. I want to make a integration with "github.com/gin-gonic/gin" or "github.com/julienschmidt/httprouter", did you have some other suggestion.

@defool
Copy link
Collaborator

defool commented Apr 22, 2020

Of course it is easy to be integrated with some web frameworks.
Here is an example:

package main

import (
	"fmt"
	"net/http"

	"github.com/TarsCloud/TarsGo/tars"
	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "Home")
	})
	r.HandleFunc("/products/{key}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "product: %v\n", vars["key"])
	})

	cfg := tars.GetServerConfig()
	tarsMux := tars.TarsHttpMux{}
	tarsMux.Handle("/", r)
	tars.AddHttpServant(&tarsMux, cfg.App+"."+cfg.Server+".MainObj") // use  `not TARS` protocol

	tars.Run()
}

@defool defool closed this as completed Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants