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"`
KeyFilePath string `json:"key_file_path"`
BaseContentDir string `json:"base_content_dir"`
ProdDBName string `json:"production_db"`
ProdDBPath string `json:"production_db"`
}
// Creates a default server configuration
@ -21,7 +21,7 @@ func Default() Conf {
CertFilePath: "",
KeyFilePath: "",
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.Write(todosBytes)
case http.MethodPatch:
// Change TODO due date and text
// Check authentication information
if !IsRequestAuthValid(req, s.db) {
http.Error(w, "Invalid user auth data", http.StatusForbidden)
return
}
// Read body
body, err := io.ReadAll(req.Body)
if err != nil {
logger.Warning("[Server] Failed to read request body to possibly update a TODO: %s", err)
http.Error(w, "Failed to read body", http.StatusInternalServerError)
return
}
// Unmarshal JSON
var todo db.Todo
err = json.Unmarshal(body, &todo)
if err != nil {
logger.Warning("[Server] Received invalid TODO JSON in order to update: %s", err)
http.Error(w, "Invalid TODO JSON", http.StatusBadRequest)
return
}
// TODO
err = s.db.UpdateTodo(todo.ID, todo)
if err != nil {
logger.Warning("[Server] Failed to update TODO: %s", err)
http.Error(w, "Failed to update", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
// case http.MethodPatch:
// // Change TODO due date and text
// // Check authentication information
// if !IsRequestAuthValid(req, s.db) {
// http.Error(w, "Invalid user auth data", http.StatusForbidden)
// return
// }
// // Read body
// body, err := io.ReadAll(req.Body)
// if err != nil {
// logger.Warning("[Server] Failed to read request body to possibly update a TODO: %s", err)
// http.Error(w, "Failed to read body", http.StatusInternalServerError)
// return
// }
// // Unmarshal JSON
// var todo db.Todo
// err = json.Unmarshal(body, &todo)
// if err != nil {
// logger.Warning("[Server] Received invalid TODO JSON in order to update: %s", err)
// http.Error(w, "Invalid TODO JSON", http.StatusBadRequest)
// return
// }
// // TODO
// err = s.db.UpdateTodo(todo.ID, todo)
// if err != nil {
// logger.Warning("[Server] Failed to update TODO: %s", err)
// http.Error(w, "Failed to update", http.StatusBadRequest)
// return
// }
// w.WriteHeader(http.StatusOK)
default:
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
config := conf.Default()
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)
if err != nil {
t.Fatalf("failed to create a new server: %s", err)
}
defer os.Remove(config.ProdDBName)
defer os.Remove(config.ProdDBPath)
go func() {
time.Sleep(time.Second * 5)
@ -59,28 +59,28 @@ func TestApi(t *testing.T) {
}
resp.Body.Close()
// Create a new TODO group and a TODO
newGroup := db.TodoGroup{
Name: "group1",
TimeCreatedUnix: 13524534,
OwnerUsername: newUser.Username,
}
newGroupBytes, err := json.Marshal(&newGroup)
if err != nil {
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))
if err != nil {
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(RequestHeaderEncodedB64, "false")
resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("failed to post a new TODO group: %s", err)
}
// Create a new TODO
// newGroup := db.TodoGroup{
// Name: "group1",
// TimeCreatedUnix: 13524534,
// OwnerUsername: newUser.Username,
// }
// newGroupBytes, err := json.Marshal(&newGroup)
// if err != nil {
// 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))
// if err != nil {
// 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(RequestHeaderEncodedB64, "false")
// resp, err = http.DefaultClient.Do(req)
// if err != nil {
// t.Fatalf("failed to post a new TODO group: %s", err)
// }
body, err = io.ReadAll(resp.Body)
if err != nil {
@ -94,7 +94,8 @@ func TestApi(t *testing.T) {
// TODO creation
var newTodo db.Todo = db.Todo{
GroupID: newGroup.ID,
// GroupID: newGroup.ID,
GroupID: 0,
Text: "Do the dishes",
TimeCreatedUnix: uint64(time.Now().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)
}
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 {
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
serverDB, err := db.FromFile(filepath.Join(config.BaseContentDir, config.ProdDBName))
serverDB, err := db.FromFile(config.ProdDBPath)
if err != nil {
// Create one then
serverDB, err = db.Create(filepath.Join(config.BaseContentDir, config.ProdDBName))
serverDB, err = db.Create(config.ProdDBPath)
if err != nil {
logger.Error("Failed to create a new database: %s", err)
return nil, err

Loading…
Cancel
Save