feat: api structure
This commit is contained in:
parent
4d3422e1fe
commit
83c88e87be
63
backend/services/controller/internal/api/api.go
Normal file
63
backend/services/controller/internal/api/api.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/leandrofars/oktopus/internal/db"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Api struct {
|
||||
Port string
|
||||
Db db.Database
|
||||
}
|
||||
|
||||
func NewApi(port string, db db.Database) Api {
|
||||
return Api{
|
||||
Port: port,
|
||||
Db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func StartApi(a Api) {
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
})
|
||||
r.HandleFunc("/devices", a.retrieveDevices)
|
||||
//r.HandleFunc("/devices/{sn}", a.devicesMessaging)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: "0.0.0.0:" + a.Port,
|
||||
// Good practice to set timeouts to avoid Slowloris attacks.
|
||||
WriteTimeout: time.Second * 15,
|
||||
ReadTimeout: time.Second * 15,
|
||||
IdleTimeout: time.Second * 60,
|
||||
Handler: r, // Pass our instance of gorilla/mux in.
|
||||
}
|
||||
|
||||
// Run our server in a goroutine so that it doesn't block.
|
||||
go func() {
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *Api) retrieveDevices(w http.ResponseWriter, r *http.Request) {
|
||||
devices, err := a.Db.RetrieveDevices()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = json.NewEncoder(w).Encode(devices)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user