internal/api.go (view raw)
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 |
package internal import ( "fmt" "net/http" "strings" "time" "watchman/database" "watchman/middleware" "watchman/schema" "watchman/utils" ) type Server struct { db database.DBService } func NewServer() *http.Server { NewServer := &Server{ db: database.New(), } server := &http.Server{ Addr: fmt.Sprintf(":%d", utils.ReadConfig().Port), Handler: middleware.ApplyMiddleware(NewServer.RegisterRoutes()), IdleTimeout: time.Minute, ReadTimeout: 10 * time.Second, WriteTimeout: 30 * time.Second, } return server } func (s *Server) RegisterRoutes() http.Handler { multiplexer := http.NewServeMux() multiplexer.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { utils.SendResponse(w, r, schema.ResponseType{ Status: "OK", Message: "Health check successful", RequestID: r.Context().Value(schema.RequestIDKey{}).(string), Data: s.db.Health(), }) }) multiplexer.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { utils.HandleError(w, r, http.StatusNotFound, "Sorry, ", fmt.Errorf("api route "+r.URL.Path+" could not be found")) }) multiplexer.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: s.CreateProjectHandler(w, r) case http.MethodGet: s.ListAllProjectsHandler(w, r) default: utils.MethodNotAllowedHandler(w, r) } }) multiplexer.HandleFunc("/projects/", func(w http.ResponseWriter, r *http.Request) { url := strings.TrimPrefix(r.URL.Path, "/projects/") projectID := url if projectID == "" { utils.HandleError(w, r, http.StatusBadRequest, "Project ID not provided", fmt.Errorf("project ID not provided")) } switch r.Method { case http.MethodGet: s.GetProjectByIDHandler(w, r, projectID) case http.MethodPut: s.UpdateProjectByIDHandler(w, r, projectID) case http.MethodDelete: s.DeleteProjectByIDHandler(w, r, projectID) default: utils.MethodNotAllowedHandler(w, r) } }) multiplexer.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: s.BatchInsertLogsHandler(w, r) case http.MethodGet: s.GetLogsHandler(w, r) case http.MethodDelete: s.DeleteLogsHandler(w, r) default: utils.MethodNotAllowedHandler(w, r) } }) multiplexer.HandleFunc("/login", AdminLogin) return multiplexer } |