-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (41 loc) · 1 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
loadEnv()
http.HandleFunc("/", hello)
http.HandleFunc("/commit-github", handleCommit)
p := os.Getenv("PORT")
fmt.Printf("Starting server at port %s\n", p)
if err := http.ListenAndServe("0.0.0.0:"+p, nil); err != nil {
log.Fatal(err)
}
fmt.Printf("Server is running on %s", p)
}
func handleCommit(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
gc := createGithubClient(ctx)
err := checkIfRepoExists(ctx, gc)
if err != nil {
fmt.Printf("Repo is not found %+v\n", err)
return
}
commit := "Daily automated commit"
file := "commit.txt"
content := fmt.Sprintf("Committed on: %+v\n", time.Now().Format("2006-1-2 15:4"))
err = createCommit(ctx, gc, file, content, commit)
if err != nil {
fmt.Printf("Error occurred while creating a commit: %+v\n", err)
return
}
w.Write([]byte("Success!"))
}
func hello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello There!"))
}