siga

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit ff873f9e56a89e0255c89573b4601f17731245c5
parent fe350a7cbf959513378a05be6dd8926c28eab32e
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date:   Wed,  4 Nov 2026 19:18:14 -0300

Create auth microsservice.

Diffstat:
A.gitignore | 3+++
Aauth/credentials.go | 29+++++++++++++++++++++++++++++
Aauth/go.mod | 16++++++++++++++++
Aauth/go.sum | 20++++++++++++++++++++
Aauth/main.go | 20++++++++++++++++++++
Aauth/repository/repository.go | 43+++++++++++++++++++++++++++++++++++++++++++
Aauth/router.go | 10++++++++++
7 files changed, 141 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +auth/db.db +auth/auth +\ No newline at end of file diff --git a/auth/credentials.go b/auth/credentials.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "github.com/gboncoffee/siga/auth/repository" + "net/http" +) + +type CredentialsRequest struct { + Username string + Password string +} + +func credentials(w http.ResponseWriter, r *http.Request) { + var req CredentialsRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "invalid JSON", http.StatusBadRequest) + return + } + + c := repository.Credential{ + Username: req.Username, + Password: req.Password, + } + if err := repository.NewCredential(c); err != nil { + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } +} diff --git a/auth/go.mod b/auth/go.mod @@ -0,0 +1,16 @@ +module github.com/gboncoffee/siga/auth + +go 1.27.3 + +require ( + github.com/dustin/go-humanize v1.1.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/ncruces/go-strftime v1.1.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/sys v0.49.0 // indirect + modernc.org/libc v1.79.0 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.12.1 // indirect + modernc.org/sqlite v1.61.1 // indirect +) diff --git a/auth/go.sum b/auth/go.sum @@ -0,0 +1,20 @@ +github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= +github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk= +github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +modernc.org/libc v1.79.0 h1:9Dvvv++bAMSkSuBzuOFAatbTV5OIoq6Y6FohmD3e7q8= +modernc.org/libc v1.79.0/go.mod h1:uT3kR61NsbeKJSLqq83GJwSnZMVLlSjyAz4DfTaBy1M= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g= +modernc.org/memory v1.12.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/sqlite v1.61.1 h1:TBEE49wz7VlxS+qXRXMQiPUZYOXHJ4ku83daYv1fe04= +modernc.org/sqlite v1.61.1/go.mod h1:f4gr5tbpx2umklaT/QVtbi/50hNdM+4ng+2zMo8KXJ0= diff --git a/auth/main.go b/auth/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "flag" + "log" + "net/http" +) + +func main() { + var port string + var addr string + + flag.StringVar(&port, "port", "8080", "port to listen to") + flag.StringVar(&addr, "address", "localhost", "address to listen to") + + flag.Parse() + + router() + log.Fatal(http.ListenAndServe(addr+":"+port, nil)) +} diff --git a/auth/repository/repository.go b/auth/repository/repository.go @@ -0,0 +1,43 @@ +package repository + +import ( + "database/sql" + "errors" + + _ "modernc.org/sqlite" +) + +type Credential struct { + Username string + Password string +} + +var ErrNotFound = errors.New("not found") + +var db *sql.DB + +func init() { + db, _ = sql.Open("sqlite", "db.db") + db.Exec(` + CREATE TABLE IF NOT EXISTS credentials ( + username TEXT NOT NULL UNIQUE, + password TEXT NOT NULL + )`) +} + +func NewCredential(c Credential) error { + _, err := db.Exec( + `INSERT INTO credentials (username, password) VALUES (?, ?)`, + c.Username, c.Password, + ) + return err +} + +func GetByUsername(username string) (Credential, error) { + var c Credential + err := db.QueryRow( + `SELECT username, password FROM credentials WHERE username = ?`, + username, + ).Scan(&c.Username, &c.Password) + return c, err +} diff --git a/auth/router.go b/auth/router.go @@ -0,0 +1,10 @@ +package main + +import "net/http" + +func router() { + // http.HandleFunc("POST /login", login) + // http.HandleFunc("POST /logout", logout) + http.HandleFunc("POST /credentials", credentials) + // http.HandleFunc("GET /{uuid}", authenticated) +}