-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.go
executable file
·98 lines (82 loc) · 2.61 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
)
type APIApp struct {
Router *mux.Router
Port uint
}
// mostly used for debugging right now
func (a *App) initializeRoutes() {
a.Router = mux.NewRouter()
a.Router.HandleFunc("/test", TestHandler).Methods("GET")
a.Router.HandleFunc("/candles", TestHandler).Methods("GET")
// a.Router.HandleFunc("/exchanges", a.GetExchangeHandler).Methods("GET")
// a.Router.HandleFunc("/exchange", a.AddExchangeHandler).Methods("PUT")
a.Router.HandleFunc("/airdrop", a.RequestAirdropHandler).Methods("GET")
a.Router.HandleFunc("/candles/ftx/{symbol}/{resolution}", a.GetSymbolCandlesHandler).Methods("GET").Queries("start", "{start}", "end", "{end}")
a.Router.HandleFunc("/solana/balance", a.GetSolanaAccountBalanceHandler).Methods("GET")
a.Router.HandleFunc("/google/{keyword}", a.GetGoogleSearchTrends).Methods("GET")
http.Handle("/", a.Router)
a.Router.Use(mux.CORSMethodMiddleware(a.Router))
a.Router.Use(loggingMiddleware)
}
func (a *App) RunAPI(done <-chan struct{}) (err error) {
if a.cfg.API.Port == 0 {
a.cfg.API.Port = 8000
}
addr := fmt.Sprintf("localhost:%d", a.cfg.API.Port)
srv := &http.Server{
Addr: addr,
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: a.Router, // Pass our instance of gorilla/mux in.
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen:%+s\n", err)
}
}()
log.Printf("API: server started at %s\n", addr)
<-done
log.Printf("API: server stopped\n")
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
if err = srv.Shutdown(ctxShutDown); err != nil {
log.Fatalf("API: server Shutdown Failed:%+s\n", err)
}
log.Printf("API: server exited properly\n")
if err == http.ErrServerClosed {
err = nil
}
return
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RequestURI)
next.ServeHTTP(w, r)
})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
if code == http.StatusOK {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(response)
} else {
w.WriteHeader(code)
}
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}