docs: add installation instructions for server Still need to add instructions for frontend, and show a simple usage example of client library.
Brijesh ops@brijesh.dev
Sat, 13 Jul 2024 03:42:39 +0530
5 files changed,
154 insertions(+),
32 deletions(-)
A
Installation.adoc
@@ -0,0 +1,132 @@
+= Installation instructions + +This guide assumes you are using Linux for deploying Watchman and are +comfortable with using the command line. + +____ +If you are using Linux with OpenRC, or other OS like BSD distributions you should be +following same steps changing service manager with rcctl, OpenRC or whatever +service manager your OS uses. I will be using Systemctl in this +guide. +____ + + +== System Requirements + +Watchman is designed for usage with side projects and so it is important +that it is lightweight. + +While it is possible to run Watchman on most computers, these are +recommended system requirements: + +- 512 MB RAM +- 1 CPU Core +- 500 MB Disk Space + +== Pre-requisites + +Watchman requires these dependencies to be installed on your computer: + +- Go 1.22 or later +- SQLite3 + +== Installation + +Follow these steps to install Watchman: + +=== Create watchman group and user: + +[source,bash] +---- +sudo groupadd watchman +sudo useradd -m -d /home/watchman -s /bin/bash -g watchman watchman + +# Set password for the new user +sudo passwd watchman + +# Change ownership of the home directory +sudo chown -R watchman:watchman /home/watchman + +# Switch to the new user +su - watchman + +# Create bin for this user and add it to PATH +mkdir /home/watchman/bin +echo 'export PATH=$PATH:/home/watchman/bin' >> ~/.bashrc +source ~/.bashrc +---- + +=== Build watchman from source: + +[source,bash] +---- +# Clone the repository +git clone <git@brijesh.dev>/watchman.git +cd watchman + +# Build the binary +go mod tidy +go build -o watchman main.go + +# Move the binary to the bin directory +mv watchman /home/watchman/bin +---- + +=== Create systemd config file: + +[source,bash] +---- +touch /etc/systemd/system/watchman.service +mkdir -p ~/.config/watchman +cp /home/watchman/watchman/config.yaml ~/.config/watchman/config.yaml +---- + +Add the following content to the file: +.... +[Unit] +Description="Watchman Service" +User=watchman +Group=watchman +Documentation=<https://brijesh.dev/watchman> +Requires=network-online.target +After=network-online.target + +[Service] +Type=simple +Restart=always +RestartSec=1 +ExecStart=/home/watchman/bin/watchman + +[Install] +WantedBy=multi-user.target +.... + +=== Start and enable the service: + +[source,bash] +---- +sudo systemctl daemon-reload +sudo systemctl start watchman.service +sudo systemctl enable watchman.service +---- +--- + +== Server Hardening + +If you’ve followed the steps so far, you’re already done installing +Watchman and can start using it. However, I recommend you to follow +these additional steps to make your server slightly more secure: + +____ +I have not included detailed instructions for these steps, as it would +make this guide too long. You can find detailed instructions for each +step on the internet. +____ + + + +. Disable root login +. Replace password authentication with SSH key authentication +. Use `ufw` or another firewall to block unwanted traffic +. Use `fail2ban` to block brute-force attacks +. Write a cron job to update the system packages regularly
M
README.adoc
→
README.adoc
@@ -11,23 +11,10 @@
== Current limitations: -* Incomplete client (no authentication) +* Incomplete client library (missing authentication) * Missing log file rotation and log expiration -== System Requirements +== Installation Instructions: -Watchman has a small footprint and should run on most systems, but here are suggested requirements: - -* 256 MB Memory -* 1 vCPU -* 500 MB Free Disk Space -* Any GNU/Linux distribution with systemd -* Go lang 1.22 or higher -* sqlite3 3.46.0 or higher - -> Above requirements assume that you are only running watchman's server on the server and running frontend on a seperate server. - -=== API Documentation - -API Endpoints along with usage examples are documented in link:./API.adoc[API.adoc] file. - +Running Watchman can be as simple as running "go run main.go", but if you want to +run it as intended, you should follow the instructions at link:./Installation.adoc[Installation.adoc] file.
M
internal/app_server.go
→
internal/api.go
@@ -12,19 +12,16 @@ "watchman/utils"
) type Server struct { - db database.DBService - port int + db database.DBService } func NewServer() *http.Server { NewServer := &Server{ - port: utils.ReadConfig().Port, - db: database.New(), } server := &http.Server{ - Addr: fmt.Sprintf(":%d", NewServer.port), + Addr: fmt.Sprintf(":%d", utils.ReadConfig().Port), Handler: middleware.ApplyMiddleware(NewServer.RegisterRoutes()), IdleTimeout: time.Minute, ReadTimeout: 10 * time.Second,@@ -37,7 +34,18 @@
func (s *Server) RegisterRoutes() http.Handler { multiplexer := http.NewServeMux() - multiplexer.HandleFunc("/health", s.healthHandler) + 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 {@@ -87,12 +95,3 @@ multiplexer.HandleFunc("/login", AdminLogin)
return multiplexer } - -func (s *Server) healthHandler(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(), - }) -}