Brijesh's Git Server — watchman @ 070e9230ef8032862eb09dd029f6d4f39705b547

observability tool, needs to be rewritten once identity is stable

chore: rename variables to follow same pattern and remove unused code
Brijesh ops@brijesh.dev
Tue, 09 Jul 2024 18:30:30 +0530
commit

070e9230ef8032862eb09dd029f6d4f39705b547

parent

2d4643bb2dffa04d3cc31d7ef64331bd819bdd5a

M go.modgo.mod

@@ -5,7 +5,6 @@

require ( github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 - github.com/joho/godotenv v1.5.0 github.com/mattn/go-sqlite3 v1.14.22 golang.org/x/time v0.5.0 gopkg.in/yaml.v3 v3.0.1
M internal/auth.gointernal/auth.go

@@ -13,7 +13,7 @@

func AdminLogin(w http.ResponseWriter, r *http.Request) { utils.HandleMethodNotAllowed(w, r, http.MethodPost) - config := utils.Read_Config() + config := utils.ReadConfig() var user schema.User decoder := json.NewDecoder(r.Body)

@@ -23,7 +23,7 @@ utils.HandleError(w, r, http.StatusBadRequest, "Error decoding JSON: ", nil)

} if user.Username != config.Admin.Username || user.Password != config.Admin.Password { - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "ERROR", Message: "Invalid credentials", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -54,7 +54,7 @@ Value: tokenString,

Expires: expirationTime, }) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Login successful", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
M internal/logs.gointernal/logs.go

@@ -30,7 +30,7 @@ _, err = stmt.Exec(log.Time, log.Level, log.Message, log.Subject, log.UserID, log.ProjectID)

utils.HandleError(w, r, http.StatusInternalServerError, "Error inserting into database: ", err) } - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Logs created successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -84,7 +84,7 @@ logs = append(logs, log)

} w.Header().Set("Content-Type", "application/json") - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Logs retrieved successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -135,7 +135,7 @@

_, err = stmt.Exec() utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Logs deleted successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
M internal/projectKeys.gointernal/projectKeys.go

@@ -49,7 +49,7 @@

_, err = stmt.Exec(ProjectID, AccessKey, SecretKey, ProjectKey.Expiry) utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Project key created successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -70,7 +70,7 @@

_, err = stmt.Exec(ProjectKey.AccessKey, ProjectKey.SecretKey) utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Project key deleted successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
M internal/projects.gointernal/projects.go

@@ -6,6 +6,8 @@ "encoding/json"

"net/http" "watchman/schema" "watchman/utils" + + "github.com/google/uuid" ) func CreateProject(w http.ResponseWriter, r *http.Request, db *sql.DB) {

@@ -17,7 +19,7 @@ err := decoder.Decode(&project)

utils.HandleError(w, r, http.StatusBadRequest, "Error decoding JSON: ", err) if project.ID == "" { - project.ID = utils.Generate_UUID() + project.ID = uuid.New().String() } stmt, err := db.Prepare("INSERT INTO Projects (ID, Name) VALUES (?, ?)")

@@ -27,7 +29,7 @@

_, err = stmt.Exec(project.ID, project.Name) utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Project created successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -43,7 +45,7 @@ var projectByID schema.Project

err := row.Scan(&projectByID.ID, &projectByID.Name) utils.HandleError(w, r, http.StatusInternalServerError, "Error querying database: ", err) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Project retrieved successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -66,7 +68,7 @@ err := rows.Scan(&project.ID, &project.Name)

utils.HandleError(w, r, http.StatusInternalServerError, "Error scanning row: ", err) projects = append(projects, project) } - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Projects retrieved successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -90,7 +92,7 @@

_, err = stmt.Exec(project.Name, project.ID, projectID) utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Project updated successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -108,7 +110,7 @@

_, err = stmt.Exec(projectID) utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "OK", Message: "Project deleted successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
M main.gomain.go

@@ -17,26 +17,26 @@ _ "github.com/mattn/go-sqlite3"

) func main() { - config := utils.Read_Config() + config := utils.ReadConfig() - db_connection, err := sql.Open("sqlite3", "./watchman.db") + dbConnection, err := sql.Open("sqlite3", "./watchman.db") if err != nil { panic(err) } - defer db_connection.Close() + defer dbConnection.Close() multiplexer := http.NewServeMux() - multiplexer.HandleFunc("/health", utils.Health_Check_Handler) + multiplexer.HandleFunc("/health", utils.HealthCheckHandler) multiplexer.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: - internal.CreateProject(w, r, db_connection) + internal.CreateProject(w, r, dbConnection) case http.MethodGet: - internal.ListAllProjects(w, r, db_connection) + internal.ListAllProjects(w, r, dbConnection) default: - utils.Method_Not_Allowed_Handler(w, r) + utils.MethodNotAllowedHandler(w, r) } })

@@ -45,7 +45,7 @@ url := strings.TrimPrefix(r.URL.Path, "/projects/")

projectID := url if projectID == "" { - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "ERROR", Message: "Project ID not provided", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),

@@ -61,26 +61,26 @@ }

switch r.Method { case http.MethodGet: - internal.GetProjectByID(w, r, db_connection, projectID) + internal.GetProjectByID(w, r, dbConnection, projectID) case http.MethodPut: - internal.UpdateProjectByID(w, r, db_connection, projectID) + internal.UpdateProjectByID(w, r, dbConnection, projectID) case http.MethodDelete: - internal.DeleteProjectByID(w, r, db_connection, projectID) + internal.DeleteProjectByID(w, r, dbConnection, projectID) default: - utils.Method_Not_Allowed_Handler(w, r) + utils.MethodNotAllowedHandler(w, r) } }) multiplexer.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: - internal.BatchInsertLogs(w, r, db_connection) + internal.BatchInsertLogs(w, r, dbConnection) case http.MethodGet: - internal.GetLogs(w, r, db_connection) + internal.GetLogs(w, r, dbConnection) case http.MethodDelete: - internal.DeleteLogs(w, r, db_connection) + internal.DeleteLogs(w, r, dbConnection) default: - utils.Method_Not_Allowed_Handler(w, r) + utils.MethodNotAllowedHandler(w, r) } })
M middleware/rate_limiting.gomiddleware/rate_limiting.go

@@ -69,7 +69,7 @@ }

limiter := getVisitor(ip, config) if !limiter.Allow() { - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "ERROR", Message: "You made too many requests", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
M schema/types.goschema/types.go

@@ -2,7 +2,7 @@ package schema

import "github.com/golang-jwt/jwt/v5" -type Response_Type struct { +type ResponseType struct { Data interface{} `json:"data,omitempty"` Status string `json:"status"` Message string `json:"message"`

@@ -20,7 +20,7 @@ Password string `yaml:"password"`

} `yaml:"admin"` JwtKey string `yaml:"jwt_key"` Port int `yaml:"port"` - RateLimitRequestsPerSecond int "yaml:\"rate_limit_req_per_sec\"" + RateLimitRequestsPerSecond int `yaml:"rate_limit_req_per_sec"` } type User struct {
M utils/common_handlers.goutils/common_handlers.go

@@ -6,12 +6,12 @@ "net/http"

"watchman/schema" ) -func Health_Check_Handler(w http.ResponseWriter, r *http.Request) { +func HealthCheckHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("OK")) } -func Method_Not_Allowed_Handler(w http.ResponseWriter, r *http.Request) { - response := schema.Response_Type{ +func MethodNotAllowedHandler(w http.ResponseWriter, r *http.Request) { + response := schema.ResponseType{ Status: "ERROR", Message: "Method Not Allowed", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
M utils/config.goutils/config.go

@@ -8,7 +8,7 @@

"gopkg.in/yaml.v3" ) -func Read_Config() schema.ConfigType { +func ReadConfig() schema.ConfigType { var Config schema.ConfigType yamlFile, err := os.ReadFile("config.yaml")
D utils/env_vars.go

@@ -1,28 +0,0 @@

-package utils - -import ( - "log" - "os" - - "github.com/joho/godotenv" -) - -func Load_ENV(env_var string) string { - err := godotenv.Load() - if err != nil { - log.Fatal("Error loading .env file") - } - - return os.Getenv(env_var) -} - -func Verify_ENV_Exists(env_var string) bool { - err := godotenv.Load() - if err != nil { - log.Fatal("Error loading .env file") - } - if os.Getenv(env_var) == "" { - return false - } - return true -}
M utils/errors.goutils/errors.go

@@ -7,7 +7,7 @@ )

func HandleError(w http.ResponseWriter, r *http.Request, statusCode int, message string, err error) { w.WriteHeader(statusCode) - response := schema.Response_Type{ + response := schema.ResponseType{ Status: "ERROR", Message: message + err.Error(), RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
M utils/response.goutils/response.go

@@ -6,14 +6,14 @@ "net/http"

"watchman/schema" ) -func SendResponse(w http.ResponseWriter, r *http.Request, response schema.Response_Type) { +func SendResponse(w http.ResponseWriter, r *http.Request, response schema.ResponseType) { w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(response) if err != nil { w.WriteHeader(http.StatusInternalServerError) - json.NewEncoder(w).Encode(schema.Response_Type{ + json.NewEncoder(w).Encode(schema.ResponseType{ Status: "ERROR", Message: "Error encoding response", RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
D utils/uuid.go

@@ -1,7 +0,0 @@

-package utils - -import "github.com/google/uuid" - -func Generate_UUID() string { - return uuid.New().String() -}