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 8 months ago
parent
commit
d5946a060d
No known key found for this signature in database
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 (
14 14
 )
15 15
 
16 16
 func main() {
17
-	fmt.Printf("GitHookParser v.%s \n", config.Get("APP_VERSION"))
18 17
 	log.Info(logrus.Fields{
19 18
 		"bootTime": time.Now(),
20
-		"version":  config.Get("APP_VERSION"),
21 19
 	}, "Booting up...")
22 20
 
23
-	server := server.server{
24
-		env: config.New("$HOME/.config/git-hook-parser"),
21
+	server := server.Server{
22
+		Env: config.New("$HOME/.config/git-hook-parser/config.yml"),
25 23
 	}
26 24
 
27
-	server.routes()
25
+	fmt.Printf("GitHookParser v.%s \n", server.Env.Get("APP_VERSION"))
26
+	fmt.Printf("Listening on port " + fmt.Sprint(server.Env.Get("HANDLER_PORT")))
27
+	server.Routes()
28 28
 
29
-	http.ListenAndServe(config.Get("HANDLER_PORT"), server.router)
29
+	http.ListenAndServe(fmt.Sprint(server.Env.Get("HANDLER_PORT")), server.Router)
30 30
 }

+ 4
- 4
server/database.go View File

@@ -15,20 +15,20 @@ import (
15 15
 // startDB initializes a database in a separate directory.
16 16
 //
17 17
 // This should only be run once.
18
-func (s *server) startDB() {
18
+func (s *Server) startDB() {
19 19
 	db, err := sql.Open("sqlite3", "./db/hooks.db")
20 20
 	if err != nil {
21 21
 		log.Fatal(logrus.Fields{}, fmt.Errorf("Error starting up database: %v", err))
22 22
 	}
23
-	s.db = db
23
+	s.DB = db
24 24
 
25 25
 }
26 26
 
27 27
 // addHook adds a webhook to the database in the correct category.
28 28
 //
29 29
 // An error is returned if the database throws an error.
30
-func (s *server) addHook(hook []byte, category string) error {
31
-	statement, err := s.db.Prepare("INSERT INTO hooks(hook, category, created) VALUES(?, ?, ?)")
30
+func (s *Server) addHook(hook []byte, category string) error {
31
+	statement, err := s.DB.Prepare("INSERT INTO hooks(hook, category, created) VALUES(?, ?, ?)")
32 32
 	if err != nil {
33 33
 		return err
34 34
 	}

+ 3
- 3
server/routes.go View File

@@ -15,7 +15,7 @@ import (
15 15
 	"github.com/stormfirefox1/GitHookParser/log"
16 16
 )
17 17
 
18
-func (s *server) handleGitHubHook() http.HandlerFunc {
18
+func (s *Server) handleGitHubHook() http.HandlerFunc {
19 19
 	var (
20 20
 		init sync.Once
21 21
 	)
@@ -69,7 +69,7 @@ func (s *server) handleGitHubHook() http.HandlerFunc {
69 69
 				}, "handleGitHook hit")
70 70
 				return
71 71
 			}
72
-			hookBody, err := webhook.Body(s.env.Get("API_TOKEN").(string), s.env.Get("USER_KEY").(string))
72
+			hookBody, err := webhook.Body(s.Env.Get("API_TOKEN").(string), s.Env.Get("USER_KEY").(string))
73 73
 			if err != nil {
74 74
 				w.WriteHeader(http.StatusInternalServerError)
75 75
 				fmt.Fprint(w, "Can't parse webhook")
@@ -86,7 +86,7 @@ func (s *server) handleGitHubHook() http.HandlerFunc {
86 86
 				return
87 87
 			}
88 88
 			hookBodyReader := bytes.NewReader(hookBody)
89
-			_, err = http.Post(s.env.Get("REDIRECT_URL").(string), "application/json", hookBodyReader)
89
+			_, err = http.Post(s.Env.Get("REDIRECT_URL").(string), "application/json", hookBodyReader)
90 90
 			if err != nil {
91 91
 				w.WriteHeader(http.StatusInternalServerError)
92 92
 				fmt.Fprint(w, "Can't redirect webhook")

+ 9
- 7
server/server.go View File

@@ -7,13 +7,15 @@ import (
7 7
 	"github.com/stormfirefox1/GitHookParser/config"
8 8
 )
9 9
 
10
-type server struct {
11
-	router *http.ServeMux
12
-	env    config.Env
13
-	db     *sql.DB
10
+// Server is the struct that will contain all the information the standard library needs in order to run correctly.
11
+type Server struct {
12
+	Router *http.ServeMux
13
+	Env    config.Env
14
+	DB     *sql.DB
14 15
 }
15 16
 
16
-// routes configures all of the functions for the whole server
17
-func (s *server) routes() {
18
-	s.router.HandleFunc("/github-hook", s.handleGitHubHook())
17
+// Routes configures all of the functions for the whole server
18
+func (s *Server) Routes() {
19
+	s.Router = http.NewServeMux()
20
+	s.Router.HandleFunc("/github-hook", s.handleGitHubHook())
19 21
 }

Loading…
Cancel
Save