Browse Source

Fix main.go file in order to actually work

Working with VS Code on Windows isn't such a smart idea
when literally nothing is reported as a problem or error, not
even basic stuff like "go vet".
I'll likely switch to an actual IDE soon, even though I like
VS Code.
master
Matei-Alexandru Gardus 10 months ago
parent
commit
d5946a060d
Signed by: StormFireFox1 <matei@gardus.eu> GPG Key ID: F3D2B830AB9B94AA
4 changed files with 22 additions and 20 deletions
  1. 6
    6
      main.go
  2. 4
    4
      server/database.go
  3. 3
    3
      server/routes.go
  4. 9
    7
      server/server.go

+ 6
- 6
main.go View File

@@ -14,17 +14,17 @@ import (
)

func main() {
fmt.Printf("GitHookParser v.%s \n", config.Get("APP_VERSION"))
log.Info(logrus.Fields{
"bootTime": time.Now(),
"version": config.Get("APP_VERSION"),
}, "Booting up...")

server := server.server{
env: config.New("$HOME/.config/git-hook-parser"),
server := server.Server{
Env: config.New("$HOME/.config/git-hook-parser/config.yml"),
}

server.routes()
fmt.Printf("GitHookParser v.%s \n", server.Env.Get("APP_VERSION"))
fmt.Printf("Listening on port " + fmt.Sprint(server.Env.Get("HANDLER_PORT")))
server.Routes()

http.ListenAndServe(config.Get("HANDLER_PORT"), server.router)
http.ListenAndServe(fmt.Sprint(server.Env.Get("HANDLER_PORT")), server.Router)
}

+ 4
- 4
server/database.go View File

@@ -15,20 +15,20 @@ import (
// startDB initializes a database in a separate directory.
//
// This should only be run once.
func (s *server) startDB() {
func (s *Server) startDB() {
db, err := sql.Open("sqlite3", "./db/hooks.db")
if err != nil {
log.Fatal(logrus.Fields{}, fmt.Errorf("Error starting up database: %v", err))
}
s.db = db
s.DB = db

}

// addHook adds a webhook to the database in the correct category.
//
// An error is returned if the database throws an error.
func (s *server) addHook(hook []byte, category string) error {
statement, err := s.db.Prepare("INSERT INTO hooks(hook, category, created) VALUES(?, ?, ?)")
func (s *Server) addHook(hook []byte, category string) error {
statement, err := s.DB.Prepare("INSERT INTO hooks(hook, category, created) VALUES(?, ?, ?)")
if err != nil {
return err
}

+ 3
- 3
server/routes.go View File

@@ -15,7 +15,7 @@ import (
"github.com/stormfirefox1/GitHookParser/log"
)

func (s *server) handleGitHubHook() http.HandlerFunc {
func (s *Server) handleGitHubHook() http.HandlerFunc {
var (
init sync.Once
)
@@ -69,7 +69,7 @@ func (s *server) handleGitHubHook() http.HandlerFunc {
}, "handleGitHook hit")
return
}
hookBody, err := webhook.Body(s.env.Get("API_TOKEN").(string), s.env.Get("USER_KEY").(string))
hookBody, err := webhook.Body(s.Env.Get("API_TOKEN").(string), s.Env.Get("USER_KEY").(string))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Can't parse webhook")
@@ -86,7 +86,7 @@ func (s *server) handleGitHubHook() http.HandlerFunc {
return
}
hookBodyReader := bytes.NewReader(hookBody)
_, err = http.Post(s.env.Get("REDIRECT_URL").(string), "application/json", hookBodyReader)
_, err = http.Post(s.Env.Get("REDIRECT_URL").(string), "application/json", hookBodyReader)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Can't redirect webhook")

+ 9
- 7
server/server.go View File

@@ -7,13 +7,15 @@ import (
"github.com/stormfirefox1/GitHookParser/config"
)

type server struct {
router *http.ServeMux
env config.Env
db *sql.DB
// Server is the struct that will contain all the information the standard library needs in order to run correctly.
type Server struct {
Router *http.ServeMux
Env config.Env
DB *sql.DB
}

// routes configures all of the functions for the whole server
func (s *server) routes() {
s.router.HandleFunc("/github-hook", s.handleGitHubHook())
// Routes configures all of the functions for the whole server
func (s *Server) Routes() {
s.Router = http.NewServeMux()
s.Router.HandleFunc("/github-hook", s.handleGitHubHook())
}

Loading…
Cancel
Save