-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (125 loc) · 3.68 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"path"
"text/template"
)
var (
clientID string
clientSecret string
)
//go:embed files/static/*
var staticFiles embed.FS
func main() {
clientID = os.Getenv("CLIENT_ID")
if clientID == "" {
log.Fatal("CLIENT_ID env variable is not set")
}
clientSecret = os.Getenv("CLIENT_SECRET")
if clientSecret == "" {
log.Fatal("CLIENT_SECRET env variable is not set")
}
var staticFS = fs.FS(staticFiles)
static, err := fs.Sub(staticFS, "files/static")
if err != nil {
log.Fatal(err)
}
// http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./files/static"))))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(static))))
http.HandleFunc("/", index)
http.HandleFunc("/get_access_token", getAccessToken)
log.Printf("Listening on port %d", 9000)
log.Fatal(http.ListenAndServe("0.0.0.0:9000", nil))
}
// index - render the index.html file
func index(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles(path.Join("./files/index.html"))
t.Execute(w, "")
}
// getAccessToken - get access token from github, using the code from the frontend.
func getAccessToken(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
postURL := fmt.Sprintf("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", clientID, clientSecret, code)
r, err := http.NewRequest("POST", postURL, nil)
if err != nil {
RespondJSON(w, struct {
Error string `json:"error"`
}{
Error: err.Error(),
}, http.StatusInternalServerError)
return
}
r.Header.Add("Content-Type", "application/json")
r.Header.Add("Accept", "application/json")
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
RespondJSON(w, struct {
Error string `json:"error"`
}{
Error: err.Error(),
}, http.StatusInternalServerError)
return
}
defer res.Body.Close()
fmt.Println("response Status:", res.Status)
fmt.Println("response Status:", res.Body)
type AccessToken struct {
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
}
at := &AccessToken{}
err = json.NewDecoder(res.Body).Decode(at)
if err != nil {
RespondJSON(w, struct {
Error string `json:"error"`
}{
Error: err.Error(),
}, http.StatusInternalServerError)
return
}
RespondJSON(w, at, http.StatusOK)
}
// RespondJSON - translate an interface to json for response
func RespondJSON(w http.ResponseWriter, resp any, statusCode int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
resJSON, err := json.Marshal(resp)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
message := fmt.Sprintf(`{"error marshaling response": "%s"}`, err.Error())
resJSON = []byte(message)
}
_, _ = w.Write(resJSON)
}
// func upload() {
// TOKEN := ""
// ctx := context.Background()
// ts := oauth2.StaticTokenSource(
// &oauth2.Token{AccessToken: TOKEN},
// )
// tc := oauth2.NewClient(ctx, ts)
// client := github.NewClient(tc)
// contentResp, resp, err := client.Repositories.CreateFile(context.Background(), "martinsaporiti", "service_A", "schemas/abc3.txt", &github.RepositoryContentFileOptions{
// Message: github.String("my commit message"),
// Content: []byte("Hello World"),
// Author: &github.CommitAuthor{
// Name: github.String("martinsaporiti"),
// Email: github.String("[email protected]"),
// },
// })
// if err != nil {
// panic(err)
// }
// defer resp.Body.Close()
// fmt.Println("response Status:", resp.Status)
// fmt.Println("Content Response:", contentResp.GetHTMLURL())
// }