new: feat: add lambdas

This commit is contained in:
2022-04-21 15:14:49 -03:00
commit 32dc9ed3e7
7 changed files with 165 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.vercel

16
api/index.go Normal file
View File

@@ -0,0 +1,16 @@
package handler
import (
"fmt"
"net/http"
"time"
)
func HandlerIndex(w http.ResponseWriter, r *http.Request) {
currentTime := time.Now().Format(time.RFC850)
fmt.Fprintf(w, currentTime)
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `
Github OAuth2 => <a href="https://github.com/xjh22222228/github-oauth2" target="_blank">https://github.com/xjh22222228/github-oauth2</a>
`)
}

View File

@@ -0,0 +1,39 @@
package statusinvest
import (
"io/ioutil"
"net/http"
)
func HandlerListCompanies(w http.ResponseWriter, r *http.Request) {
client := http.Client{}
req, err := http.NewRequest("GET", "https://statusinvest.com.br/acao/companiesnavigation?page=1&size=500", nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36")
req.Header.Set("Accept", "application/json, text/plain, */*")
resp, err := client.Do(req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(body)
}

View File

@@ -0,0 +1,85 @@
package statusinvest
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type Response struct {
Success bool `json:"success"`
Data map[string][]map[string]interface{} `json:"data"`
}
type ParsedResponse map[string]interface{}
func HandlerIndicators(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
return
}
time := r.URL.Query().Get("time")
if time == "" {
time = "7"
}
ticker := r.URL.Query().Get("ticker")
if ticker == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Ticker is required"))
return
}
data := url.Values{
"codes[]": strings.Split(ticker, ","),
"time": {time},
"byQuarter": {"false"},
"futureData": {"false"},
}
resp, err := http.PostForm("https://statusinvest.com.br/acao/indicatorhistoricallist", data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
var indicators Response
err = json.Unmarshal(out, &indicators)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
parsedResponse := ParsedResponse{}
d := indicators.Data[ticker]
for _, v := range d {
v := v
parsedResponse[v["key"].(string)] = v
}
out, err = json.Marshal(parsedResponse)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(out)
}

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/felipemarinho97/vercel-lambdas
go 1.17

18
main.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"net/http"
handler "github.com/felipemarinho97/vercel-lambdas/api"
"github.com/felipemarinho97/vercel-lambdas/api/statusinvest"
)
func main() {
http.HandleFunc("/", handler.HandlerIndex)
http.HandleFunc("/statusinvest/companies", statusinvest.HandlerListCompanies)
err := http.ListenAndServe(":7006", nil)
if err != nil {
panic(err)
}
}

3
vercel.json Normal file
View File

@@ -0,0 +1,3 @@
{
}