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

any api interface?eg:restfull API #334

Open
xinmans opened this issue Apr 17, 2023 · 1 comment
Open

any api interface?eg:restfull API #334

xinmans opened this issue Apr 17, 2023 · 1 comment

Comments

@xinmans
Copy link

xinmans commented Apr 17, 2023

post,get etc

@gocs
Copy link

gocs commented Jan 23, 2025

I usually reference this method:

func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
//handle realtime client library
if r.URL.Path == "/js/velox.js" {
velox.JS.ServeHTTP(w, r)
return
}
//handle realtime client connections
if r.URL.Path == "/sync" {
conn, err := velox.Sync(&s.state, w, r)
if err != nil {
log.Printf("sync failed: %s", err)
return
}
s.state.Users[conn.ID()] = r.RemoteAddr
s.state.Push()
conn.Wait()
delete(s.state.Users, conn.ID())
s.state.Push()
return
}
//search
if strings.HasPrefix(r.URL.Path, "/search") {
s.scraperh.ServeHTTP(w, r)
return
}
//api call
if strings.HasPrefix(r.URL.Path, "/api/") {
//only pass request in, expect error out
if err := s.api(r); err == nil {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
return
}
//no match, assume static file
s.files.ServeHTTP(w, r)
}

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