This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (106 loc) · 2.62 KB
/
main.go
File metadata and controls
120 lines (106 loc) · 2.62 KB
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
package main
import (
"database/sql"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
. "github.com/Heart-plus-N/habitica-multi-bot/bot"
log "github.com/amoghe/distillog"
"github.com/go-chi/chi"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus/promhttp"
. "gitlab.com/bfcarpio/gabit"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Warningln("No .env file found")
}
}
func main() {
// Ensure we have a port
portNum := os.Getenv("PORT")
if portNum == "" {
log.Infoln("Setting port to default :8080")
portNum = "8080"
}
portStr := ":" + portNum
databaseUrl := os.Getenv("DATABASE_URL")
if databaseUrl == "" {
databaseUrl = fmt.Sprintf(
"%s:%s@tcp(%s)/%s",
os.Getenv("HMB_USERNAME"),
os.Getenv("HMB_PASSWORD"),
os.Getenv("HMB_DB_HOST"),
os.Getenv("HMB_DB_NAME"),
)
}
connConfig, _ := pgx.ParseConfig(databaseUrl)
connStr := stdlib.RegisterConnConfig(connConfig)
db, dbErr := sql.Open("pgx", connStr)
if dbErr != nil {
panic(dbErr)
}
defer db.Close()
habiticaUsername := os.Getenv("HMB_USERNAME")
habiticaPassword := os.Getenv("HMB_PASSWORD")
// Connect to habitica
hapi := NewHabiticaAPI(nil, "", nil)
_, authErr := hapi.Authenticate(habiticaUsername, habiticaPassword)
if authErr != nil {
panic(authErr)
}
log.Infoln("Logged into Habitica as: ", habiticaUsername)
sc := SharedConfig{
Api: hapi,
Db: db,
}
// Open routes
r := chi.NewRouter()
r.Handle("/metrics", promhttp.Handler())
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Bot up as of: %s", time.Now().String())
})
r.Post("/task", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorln(err)
} else {
go reporter.Notify(op.TaskEvent, body)
}
w.WriteHeader(200)
})
r.Post("/chat", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorln(err)
} else {
go reporter.Notify(op.GroupChatEvent, body)
}
w.WriteHeader(200)
})
r.Post("/user", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorln(err)
} else {
go reporter.Notify(op.UserEvent, body)
}
w.WriteHeader(200)
})
r.Post("/quest", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorln(err)
} else {
go reporter.Notify(op.QuestEvent, body)
}
w.WriteHeader(200)
})
log.Infoln("Listening on", portStr)
serverErr := http.ListenAndServe(portStr, r)
log.Errorln(serverErr)
}