Browse Source

feat: made db path independent from base content dir

master
parent
commit
604f9a8420
  1. 4
      src/conf/conf.go
  2. 70
      src/server/api.go
  3. 53
      src/server/api_test.go
  4. 4
      src/server/server.go

4
src/conf/conf.go

@ -11,7 +11,7 @@ type Conf struct {
CertFilePath string `json:"cert_file_path"` CertFilePath string `json:"cert_file_path"`
KeyFilePath string `json:"key_file_path"` KeyFilePath string `json:"key_file_path"`
BaseContentDir string `json:"base_content_dir"` BaseContentDir string `json:"base_content_dir"`
ProdDBName string `json:"production_db"` ProdDBPath string `json:"production_db"`
} }
// Creates a default server configuration // Creates a default server configuration
@ -21,7 +21,7 @@ func Default() Conf {
CertFilePath: "", CertFilePath: "",
KeyFilePath: "", KeyFilePath: "",
BaseContentDir: ".", BaseContentDir: ".",
ProdDBName: "dela.db", ProdDBPath: "dela.db",
} }
} }

70
src/server/api.go

@ -231,41 +231,41 @@ func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
w.Write(todosBytes) w.Write(todosBytes)
case http.MethodPatch: // case http.MethodPatch:
// Change TODO due date and text // // Change TODO due date and text
// Check authentication information // // Check authentication information
if !IsRequestAuthValid(req, s.db) { // if !IsRequestAuthValid(req, s.db) {
http.Error(w, "Invalid user auth data", http.StatusForbidden) // http.Error(w, "Invalid user auth data", http.StatusForbidden)
return // return
} // }
// Read body // // Read body
body, err := io.ReadAll(req.Body) // body, err := io.ReadAll(req.Body)
if err != nil { // if err != nil {
logger.Warning("[Server] Failed to read request body to possibly update a TODO: %s", err) // logger.Warning("[Server] Failed to read request body to possibly update a TODO: %s", err)
http.Error(w, "Failed to read body", http.StatusInternalServerError) // http.Error(w, "Failed to read body", http.StatusInternalServerError)
return // return
} // }
// Unmarshal JSON // // Unmarshal JSON
var todo db.Todo // var todo db.Todo
err = json.Unmarshal(body, &todo) // err = json.Unmarshal(body, &todo)
if err != nil { // if err != nil {
logger.Warning("[Server] Received invalid TODO JSON in order to update: %s", err) // logger.Warning("[Server] Received invalid TODO JSON in order to update: %s", err)
http.Error(w, "Invalid TODO JSON", http.StatusBadRequest) // http.Error(w, "Invalid TODO JSON", http.StatusBadRequest)
return // return
} // }
// TODO // // TODO
err = s.db.UpdateTodo(todo.ID, todo) // err = s.db.UpdateTodo(todo.ID, todo)
if err != nil { // if err != nil {
logger.Warning("[Server] Failed to update TODO: %s", err) // logger.Warning("[Server] Failed to update TODO: %s", err)
http.Error(w, "Failed to update", http.StatusBadRequest) // http.Error(w, "Failed to update", http.StatusBadRequest)
return // return
} // }
w.WriteHeader(http.StatusOK) // w.WriteHeader(http.StatusOK)
default: default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
} }

53
src/server/api_test.go

@ -18,12 +18,12 @@ func TestApi(t *testing.T) {
// Create a new server // Create a new server
config := conf.Default() config := conf.Default()
config.BaseContentDir = "../../" config.BaseContentDir = "../../"
config.ProdDBName = filepath.Join(os.TempDir(), "dela_test_db.db") config.ProdDBPath = filepath.Join(os.TempDir(), "dela_test_db.db")
server, err := New(config) server, err := New(config)
if err != nil { if err != nil {
t.Fatalf("failed to create a new server: %s", err) t.Fatalf("failed to create a new server: %s", err)
} }
defer os.Remove(config.ProdDBName) defer os.Remove(config.ProdDBPath)
go func() { go func() {
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
@ -59,28 +59,28 @@ func TestApi(t *testing.T) {
} }
resp.Body.Close() resp.Body.Close()
// Create a new TODO group and a TODO // Create a new TODO
newGroup := db.TodoGroup{ // newGroup := db.TodoGroup{
Name: "group1", // Name: "group1",
TimeCreatedUnix: 13524534, // TimeCreatedUnix: 13524534,
OwnerUsername: newUser.Username, // OwnerUsername: newUser.Username,
} // }
newGroupBytes, err := json.Marshal(&newGroup) // newGroupBytes, err := json.Marshal(&newGroup)
if err != nil { // if err != nil {
t.Fatalf("could not marshal new user JSON: %s", err) // t.Fatalf("could not marshal new user JSON: %s", err)
} // }
req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/api/groups", config.Port), bytes.NewBuffer(newGroupBytes)) // req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/api/groups", config.Port), bytes.NewBuffer(newGroupBytes))
if err != nil { // if err != nil {
t.Fatalf("failed to create a new POST request to create a new TODO group: %s", err) // t.Fatalf("failed to create a new POST request to create a new TODO group: %s", err)
} // }
req.Header.Add(RequestHeaderAuthKey, fmt.Sprintf("%s%s%s", newUser.Username, RequestHeaderAuthSeparator, newUser.Password)) // req.Header.Add(RequestHeaderAuthKey, fmt.Sprintf("%s%s%s", newUser.Username, RequestHeaderAuthSeparator, newUser.Password))
req.Header.Add(RequestHeaderEncodedB64, "false") // req.Header.Add(RequestHeaderEncodedB64, "false")
resp, err = http.DefaultClient.Do(req) // resp, err = http.DefaultClient.Do(req)
if err != nil { // if err != nil {
t.Fatalf("failed to post a new TODO group: %s", err) // t.Fatalf("failed to post a new TODO group: %s", err)
} // }
body, err = io.ReadAll(resp.Body) body, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
@ -94,7 +94,8 @@ func TestApi(t *testing.T) {
// TODO creation // TODO creation
var newTodo db.Todo = db.Todo{ var newTodo db.Todo = db.Todo{
GroupID: newGroup.ID, // GroupID: newGroup.ID,
GroupID: 0,
Text: "Do the dishes", Text: "Do the dishes",
TimeCreatedUnix: uint64(time.Now().UnixMicro()), TimeCreatedUnix: uint64(time.Now().UnixMicro()),
DueUnix: uint64(time.Now().Add(time.Hour * 5).UnixMicro()), DueUnix: uint64(time.Now().Add(time.Hour * 5).UnixMicro()),
@ -106,7 +107,7 @@ func TestApi(t *testing.T) {
t.Fatalf("could not marshal new Todo: %s", err) t.Fatalf("could not marshal new Todo: %s", err)
} }
req, err = http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/api/todo", config.Port), bytes.NewBuffer(newTodoBytes)) req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/api/todo", config.Port), bytes.NewBuffer(newTodoBytes))
if err != nil { if err != nil {
t.Fatalf("failed to create a new POST request to create a new TODO: %s", err) t.Fatalf("failed to create a new POST request to create a new TODO: %s", err)
} }

4
src/server/server.go

@ -49,10 +49,10 @@ func New(config conf.Conf) (*Server, error) {
} }
// get database working // get database working
serverDB, err := db.FromFile(filepath.Join(config.BaseContentDir, config.ProdDBName)) serverDB, err := db.FromFile(config.ProdDBPath)
if err != nil { if err != nil {
// Create one then // Create one then
serverDB, err = db.Create(filepath.Join(config.BaseContentDir, config.ProdDBName)) serverDB, err = db.Create(config.ProdDBPath)
if err != nil { if err != nil {
logger.Error("Failed to create a new database: %s", err) logger.Error("Failed to create a new database: %s", err)
return nil, err return nil, err

Loading…
Cancel
Save